This is part of a series of posts on getting started with Arduino. Others in the series include:
- Arduino: Getting Started
- Arduino: Basic Troubleshooting
- Arduino: Understanding the Structure of C
- Arduino: Understanding Blink
- Arduino: Modifying Blink
- Arduino: Functions & Variables
- Arduino: Strings & Serial
- Arduino: If & For
This post modifies our Morse code program to be able to receive messages from the Serial monitor, allowing it to send out any signal, instead of only a preprogrammed one. This is a continuation of the previous post (Arduino: If & For) and you should read that one first.
The Goal
The goal now is to be able to send a message to the Arduino from the computer and have the Arduino blink out the message.
The Modified Program So Far
Here is the full code to repeatedly send out the message “SOS”.
int timeUnit = 500;
String message = "SOS";
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
for (int i = 0; i < message.length(); i++) {
char j = message[i];
if (j == 'O') O();
if (j == 'S') S();
/*
this pattern would need to be extended to include all 26 letters.
*/
spaceBetweenLetters();
}
}
void dot() {
digitalWrite(LED_BUILTIN, HIGH);
delay(timeUnit);
digitalWrite(LED_BUILTIN, LOW);
delay(timeUnit);
}
void dash() {
digitalWrite(LED_BUILTIN, HIGH);
delay(3*timeUnit);
digitalWrite(LED_BUILTIN, LOW);
delay(timeUnit);
}
void spaceBetweenLetters() {
delay(2*timeUnit);
}
void spaceBetweenWords() {
delay(4*timeUnit);
}
void S() {
dot();
dot();
dot();
}
void O() {
dash();
dash();
dash();
}
/*
Functions would need to be added for all 26 letters.
*/
Putting It All Together
We are now ready to combine what we have learned about using the serial monitor with our Morse code program.
int timeUnit = 500;
String message = "";
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
message = Serial.readString();
for (int i = 0; i < message.length(); i++) {
char j = message[i];
if (j == 'O') O();
if (j == 'S') S();
spaceBetweenLetters();
} //end of for
} //end of if
} //end of loop
void dot() {
digitalWrite(LED_BUILTIN, HIGH);
delay(timeUnit);
digitalWrite(LED_BUILTIN, LOW);
delay(timeUnit);
}
void dash() {
digitalWrite(LED_BUILTIN, HIGH);
delay(3*timeUnit);
digitalWrite(LED_BUILTIN, LOW);
delay(timeUnit);
}
void spaceBetweenLetters() {
delay(2*timeUnit);
}
void spaceBetweenWords() {
delay(4*timeUnit);
}
void S() {
dot();
dot();
dot();
}
void O() {
dash();
dash();
dash();
}
/*
Functions would need to be added for all 26 letters.
*/
Unlike the previous programs, this program will only blink out the Morse code one time. It will then wait for you to enter a different text.
If, instead, we wanted the program to repeatedly blink out the message until it receives a new message from the serial monitor, then we can slightly change the program to this:
int timeUnit = 500;
String message = "";
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
message = Serial.readString();
} //end of if
for (int i = 0; i < message.length(); i++) {
char j = message[i];
if (j == 'O') O();
if (j == 'S') S();
spaceBetweenLetters();
} //end of for
} //end of loop
void dot() {
digitalWrite(LED_BUILTIN, HIGH);
delay(timeUnit);
digitalWrite(LED_BUILTIN, LOW);
delay(timeUnit);
}
void dash() {
digitalWrite(LED_BUILTIN, HIGH);
delay(3*timeUnit);
digitalWrite(LED_BUILTIN, LOW);
delay(timeUnit);
}
void spaceBetweenLetters() {
delay(2*timeUnit);
}
void spaceBetweenWords() {
delay(4*timeUnit);
}
void S() {
dot();
dot();
dot();
}
void O() {
dash();
dash();
dash();
}
/*
Functions would need to be added for all 26 letters.
*/
Notice that the only difference in the two programs is the placement of the } that ends the if statement. In the first program, the for loop is contained inside the if statement, so the for loop is only run if the if statement is true. In the second program, the for loop is outside of the if statement, so it is run whether the if statement is true or not.
In the first program, if the program receives a new string of characters (that is, if Serial.avaliable() > 0), then it saves those characters to the variable “message” and then blinks out those characters. Then we reach the end of the if statement.
In the second program, if the program receives a new string of characters, then it saves those characters. That is the end of the if statement, so if the program has not received a new string of characters, then the variable message still contains the old string. Either way, whether new characters were received or not, the variable “message” contains something and the for loop is run after the if statement rather than inside it, so some message is still blinked.
Leave a comment