Hey. I'm making a morse code proejct that outputs morse code through an led while printing it in the serial monitor.
I am trying to print the string that I input which will be converted to morse code at the start.
I am having trouble printing the inputted string at the start in which I have in bold.
Such as:
Hello there
H
....
E
.
L
.-..
and so on...
Here's my code!
const unsigned int MAX_MESSAGE_LENGTH = 64;
#define DEBUG_MODE
#define LEDPIN 13 //the led pin
#define TIME_UNIT 1000
#define DOT_L 1
#define DASH_L 3
#define LETTER_SPACE_L 3
#define WORD_SPACE_L 7
#define SYMBOL_L 1
#define DOT_TIME (TIME_UNIT * DOT_L)
#define DASH_TIME (TIME_UNIT * DASH_L)
#define LETTER_SPACE (TIME_UNIT * (LETTER_SPACE_L - SYMBOL_L))
#define WORD_SPACE (TIME_UNIT * (WORD_SPACE_L - (LETTER_SPACE_L)))
#define SYMBOL_DELAY (TIME_UNIT * SYMBOL_L)
char input;// to save the input
void setup () {
pinMode(LEDPIN, OUTPUT);//tell that the 13 pin is an output
Serial.begin(9600);//for the connect with the boared
}
void loop () {
while (Serial.available() > 0)
{
//Create a place to hold the incoming message
static char message[MAX_MESSAGE_LENGTH];
static unsigned int message_pos = 0;
//Read the next available byte in the serial receive buffer
char inByte = Serial.read();
//Message coming in (check not terminating character) and guard for over message size
if ( inByte != '\n' && (message_pos < MAX_MESSAGE_LENGTH - 1) )
{
//Add the incoming byte to our message
message[message_pos] = inByte;
message_pos++;
}
//Full message received...
else
{
//Add null character to string
message[message_pos] = '\0';
//Print the message (or do other things)
Serial.println(message);
//Reset for the next message
message_pos = 0;
}
}
if (Serial.available()) {
input = toUpperCase (Serial.read());//read the input
Serial.println("");
Serial.println (input);//print the latter saved in the input var
if (input == 'A') {
lA(); //if the input is a or A go to function lA
}
if (input == 'B') {
lB(); //same but with b letter
}
if (input == 'C') {
lC();
}
if (input == 'D') {
lD();
}
if (input == 'E') {
lE();
}
if (input == 'F') {
lF();
}
if (input == 'G') {
lG();
}
if (input == 'H') {
lH();
}
if (input == 'I') {
lI();
}
if (input == 'J') {
lJ();
}
if (input == 'K') {
lK();
}
if (input == 'L') {
lL();
}
if (input == 'M') {
lM();
}
if (input == 'N') {
lN();
}
if (input == 'O') {
lO();
}
if (input == 'P') {
lP();
}
if (input == 'Q') {
lQ();
}
if (input == 'R') {
lR();
}
if (input == 'S') {
lS();
}
if (input == 'T') {
lT();
}
if (input == 'U') {
lU();
}
if (input == 'V') {
lV();
}
if (input == 'W') {
lW();
}
if (input == 'X') {
lX();
}
if (input == 'Y') {
lY();
}
if (input == 'Z') {
lZ();
}
if (input == '1') {
n1(); // the numbers
}
if (input == '2') {
n2();
}
if (input == '3') {
n3();
}
if (input == '4') {
n4();
}
if (input == '5') {
n5();
}
if (input == '6') {
n6();
}
if (input == '7') {
n7();
}
if (input == '8') {
n8();
}
if (input == '9') {
n9();
}
if (input == '0') {
n0();
}
if (input == ' ') {
space(); //the space
}
}
}
//fonctions for the letters and the numbers
void lA () {
dot(); //letter A in morse code!
dash();
shortspace();
}
void lB () {
dash(); //same for B
dot();
dot();
dot();
shortspace();
}
void lC () {
dash();
dot();
dash();
dot();
shortspace();
}
void lD () {
dash();
dot();
dot();
shortspace();
}
void lE () {
dot();
shortspace();
}
void lF () {
dot();
dot();
dash();
dot();
shortspace();
}
void lG () {
dash();
dash();
dot();
shortspace();
}
void lH () {
dot();
dot();
dot();
dot();
shortspace();
}
void lI () {
dot();
dot();
shortspace();
}
void lJ () {
dot();
dash();
dash();
dash();
shortspace();
}
void lK () {
dash();
dot();
dash();
shortspace();
}
void lL () {
dot();
dash();
dot();
dot();
shortspace();
}
void lM () {
dash();
dash();
shortspace();
}
void lN () {
dash();
dot();
shortspace();
}
void lO () {
dash();
dash();
dash();
shortspace();
}
void lP () {
dot();
dash();
dash();
dot();
shortspace();
}
void lQ () {
dash();
dash();
dot();
dash();
shortspace();
}
void lR () {
dot();
dash();
dot();
shortspace();
}
void lS () {
dot();
dot();
dot();
shortspace();
}
void lT () {
dash();
shortspace();
}
void lU () {
dot();
dot();
dash();
shortspace();
}
void lV () {
dot();
dot();
dot();
dash();
shortspace();
}
void lW () {
dot();
dash();
dash();
shortspace();
}
void lX () {
dash();
dot();
dot();
dash();
shortspace();
}
void lY () {
dash();
dot();
dash();
dash();
shortspace();
}
void lZ () {
dash();
dash();
dot();
dot();
shortspace();
}
void n1 () {
dot(); //number 1 in morse code
dash();
dash();
dash();
dash();
shortspace();
}
void n2 () {
dot();
dot();
dash();
dash();
dash();
shortspace();
}
void n3 () {
dot();
dot();
dot();
dash();
dash();
shortspace();
}
void n4 () {
dot();
dot();
dot();
dot();
dash();
shortspace();
}
void n5 () {
dot();
dot();
dot();
dot();
dot();
shortspace();
}
void n6 () {
dash();
dot();
dot();
dot();
dot();
shortspace();
}
void n7 () {
dash();
dash();
dot();
dot();
dot();
shortspace();
}
void n8 () {
dash();
dash();
dash();
dot();
dot();
shortspace();
}
void n9 () {
dash();
dash();
dash();
dash();
dot();
shortspace();
}
void n0 () {
dash();
dash();
dash();
dash();
dash();
shortspace();
}
void space () {
delay(WORD_SPACE); //space between words
}
void dot () {
digitalWrite(LEDPIN, HIGH); //the dot this code make the led on for 300 than off for 300
Serial.print('.');
delay(DOT_TIME);
digitalWrite(LEDPIN, LOW);
delay(SYMBOL_DELAY);
}
void dash () {
digitalWrite(LEDPIN, HIGH); //the dash this code make the led on for 900 than off for 300
Serial.print('-');
delay(DASH_TIME);
digitalWrite(LEDPIN, LOW);
delay(SYMBOL_DELAY);
}
void shortspace () {
delay(LETTER_SPACE); //space between letters
}
`