Help With Morse Code

int RecievedData = 0; //recieved data
void setup() {
// put your setup code here, to run once:
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
}

void loop() {
if (Serial.available() > 0) { //only send data if data is recieved
RecievedData = Serial.read(); //read the recieved data
Serial.println(RecievedData); //print data

}
void a();
}
void DOT()
{
digitalWrite(LED_BUILTIN, HIGH);
delay(100);
digitalWrite(LED_BUILTIN, LOW);
delay(100);
}
void DASH()
{
digitalWrite(LED_BUILTIN, HIGH);
delay(300);
digitalWrite(LED_BUILTIN, LOW);
delay(100);
}

void a(){
int x = RecievedData;
switch (x)
{
case 97:
{
DOT();
DASH();
break;
}
case 98:
DASH();
DOT();
DOT();
DOT();
break;
case 99:
DASH();
DOT();
DASH();
DOT();
break;
case 100:
DASH();
DOT();
DOT();
break;
case 32:
delay(200);
break;
}
}

This code should work using serialEvent() :

String inputString = "";         // a string to hold incoming data
volatile boolean stringComplete = false;  // whether the string is complete
volatile uint32_t value;

int RecievedData = 0; //recieved data
void setup() {

  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(250000);
  Serial.println(" Enter morse code ");
  // reserve 4 bytes for the inputString:
  inputString.reserve(4);
}

void loop() {
  // print the string when a newline arrives:
  if (stringComplete) {

    value = inputString.toInt(); // convert to integer
    a();

    Serial.println(value);

    // clear the string:
    inputString = "";

    stringComplete = false;
  }

  /*
    if (Serial.available() > 0) { //only send data if data is recieved
      RecievedData = Serial.read(); //read the recieved data
      Serial.println(RecievedData); //print data

    }

  */
}

void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    }
  }

}


void DOT()
{
  digitalWrite(LED_BUILTIN, HIGH);
  delay(1000);
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);
}


void DASH()
{
  Serial.println("coucou");
  digitalWrite(LED_BUILTIN, HIGH);
  delay(3000);
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);
}

void a() {

  switch (value) {
    case 97 :
      {

        DOT();
        DASH();
        break;
      }

    case 98:
      DASH();
      DOT();
      DOT();
      DOT();
      break;
    case 99:
      DASH();
      DOT();
      DASH();
      DOT();
      break;
    case 100:
      DASH();
      DOT();
      DOT();
      break;
    case 32:
      delay(200);
      break;
  }


}

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Also state what your program should do and what it is doing?

Thanks.. Tom... :slight_smile:

This code should work using serialEvent() :

What does serialEvent() add that a simple if (Serial.available()) in the loop() function could not do ?

This is your loop() function:

void loop() {
       if (Serial.available() > 0) { //only send data if data is recieved
                RecievedData = Serial.read(); //read the recieved data
                Serial.println(RecievedData); //print data
             
}
void a();
}

Regardless of what you receive from the serial port, you do nothing. That is not even a call to a(). That is a function prototype for a function that takes no arguments and returns nothing. It is not even a good name for a function.