Transmitter receiever with two arduino boards

Hello. I am trying to transmit pulses from one arduino to another. For my transmitter program ive defined dot and dash durations and have also similarly defined them in my receiver program. The transmitter program works by converting inputted text in the serial monitor into morse code by blinking an led. I want to use this output as an input to a receiver arduino where itll then interpret the high and low pulses/button presses and their duration to then convert that morse code back into text . I got the programs to work by themselves. so the transmitter program blinks and led in morse and the receiver program decodes morse code according to how the button is pressed. i tried to interlink these programs by simply creating a common ground and connecting the digital pin that the led was connected to to the digitl pin the button was connected to as i though itll be equivalent to button presses. However when i test it no mattter what i type into the serial monitor on the transmitter side, the receiver side always just prints the letter E. does anyone know how i can transmit data from the transmitter using its output pulses reliably. I cant use the Rx and Tx pins because i need the reciever to interpret input signals/voltages for the next part of this project
transmitter code:

String code = ""; //stores what user types in
int len = 0;
char ch;
char new_char;
int unit_delay = 250;

void dot() {
  Serial.print("."); //serial monitor
  digitalWrite(led, HIGH);
  
  delay(400);
  digitalWrite(led, LOW);
 
  delay(unit_delay);
}

void dash() {
  Serial.print("-");
  digitalWrite(led, HIGH);

//  delay(unit_delay * 3);
delay(1200);
  digitalWrite(led, LOW);
//delay(15000);
  delay(unit_delay );
}

void A() {
  dot();
  delay(unit_delay);
  dash();
  delay(unit_delay);
}

void B()
{
  dash();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
}
void C()
{
  dash();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dot();
  delay(unit_delay);
}
void D()
{
  dash();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
}
void E()
{
  dot();
  delay(unit_delay);
}
void f()
{
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dot();
  delay(unit_delay);
}
void G()
{
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dot();
  delay(unit_delay);
}
void H()
{
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
}
void I()
{
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
}
void J()
{
  dot();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
}
void K()
{
  dash();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dash();
  delay(unit_delay);
}
void L()
{
  dot();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
}
void M()
{
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
}
void N()
{
  dash();
  delay(unit_delay);
  dot();
  delay(unit_delay);
}
void O()
{
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
}
void P()
{
  dot();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dot();
}
void Q()
{
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dash();
  delay(unit_delay);
}
void R()
{
  dot();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dot();
  delay(unit_delay);
}
void S()
{
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
}
void T()
{
  dash();
  delay(unit_delay);
}
void U()
{
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dash();
  delay(unit_delay);
}
void V()
{
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dash();
  delay(unit_delay);
}
void W()
{
  dot();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
}
void X()
{
  dash();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dash();
  delay(unit_delay);
}
void Y()
{
  dash();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
}
void Z()
{
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
}
void one()
{
  dot();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
}
void two()
{
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
}
void three()
{
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
}
void four()
{
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dash();
  delay(unit_delay);
}
void five()
{
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
}
void six()
{
  dash();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
}
void seven()
{
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
}
void eight()
{
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
}
void nine()
{
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dot();
  delay(unit_delay);
}
void zero()
{
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
}


void morse() //does the translation
{
  if (ch == 'A' || ch == 'a') {
    A();
    Serial.print(" ");
  }
  else if (ch == 'B' || ch == 'b')
  {
    B();
    Serial.print(" ");
  }
  else if (ch == 'C' || ch == 'c')
  {
    C();
    Serial.print(" ");
  }
  else if (ch == 'D' || ch == 'd')
  {
    D();
    Serial.print(" ");
  }
  else if (ch == 'E' || ch == 'e')
  {
    E();
    Serial.print(" ");
  }
  else if (ch == 'f' || ch == 'f')
  {
    f();
    Serial.print(" ");
  }
  else if (ch == 'G' || ch == 'g')
  {
    G();
    Serial.print(" ");
  }
  else if (ch == 'H' || ch == 'h')
  {
    H();
    Serial.print(" ");
  }
  else if (ch == 'I' || ch == 'i')
  {
    I();
    Serial.print(" ");
  }
  else if (ch == 'J' || ch == 'j')
  {
    J();
    Serial.print(" ");
  }
  else if (ch == 'K' || ch == 'k')
  {
    K();
    Serial.print(" ");
  }
  else if (ch == 'L' || ch == 'l')
  {
    L();
    Serial.print(" ");
  }
  else if (ch == 'M' || ch == 'm')
  {
    M();
    Serial.print(" ");
  }
  else if (ch == 'N' || ch == 'n')
  {
    N();
    Serial.print(" ");
  }
  else if (ch == 'O' || ch == 'o')
  {
    O();
    Serial.print(" ");
  }
  else if (ch == 'P' || ch == 'p')
  {
    P();
    Serial.print(" ");
  }
  else if (ch == 'Q' || ch == 'q')
  {
    Q();
    Serial.print(" ");
  }
  else if (ch == 'R' || ch == 'r')
  {
    R();
    Serial.print(" ");
  }
  else if (ch == 'S' || ch == 's')
  {
    S();
    Serial.print(" ");
  }
  else if (ch == 'T' || ch == 't')
  {
    T();
    Serial.print(" ");
  }
  else if (ch == 'U' || ch == 'u')
  {
    U();
    Serial.print(" ");
  }
  else if (ch == 'V' || ch == 'v')
  {
    V();
    Serial.print(" ");
  }
  else if (ch == 'W' || ch == 'w')
  {
    W();
    Serial.print(" ");
  }
  else if (ch == 'X' || ch == 'x')
  {
    X();
    Serial.print(" ");
  }
  else if (ch == 'Y' || ch == 'y')
  {
    Y();
    Serial.print(" ");
  }
  else if (ch == 'Z' || ch == 'z')
  {
    Z();
    Serial.print(" ");
  }
  else if (ch == '0')
  {
    zero();
    Serial.print(" ");
  }
  else if (ch == '1')
  {
    one();
    Serial.print(" ");
  }
  else if (ch == '2')
  {
    two();
    Serial.print(" ");
  }
  else if (ch == '3')
  {
    three();
    Serial.print(" ");
  }
  else if (ch == '4')
  {
    four();
    Serial.print(" ");
  }
  else if (ch == '5')
  {
    five();
    Serial.print(" ");
  }
  else if (ch == '6')
  {
    six();
    Serial.print(" ");
  }
  else if (ch == '7')
  {
    seven();
    Serial.print(" ");
  }
  else if (ch == '8')
  {
    eight();
    Serial.print(" ");
  }
  else if (ch == '9')
  {
    nine();
    Serial.print(" ");
  }
  

 else if (ch == ' ') {
    // Add a longer delay after each space
    delay(unit_delay * 40); //makes delay 15 seconds between each word
    Serial.print("/ ");
  }
  else if (ch == '\n') {
    Serial.println("");
  }
  else {
    Serial.println("Unknown Character!");
  }

  // Add a delay after each character (except for spaces)
  if (ch != ' ') {
    delay(unit_delay * 20);
  }
}
  
  /*
 else if (ch == ' ') {
    delay(unit_delay * 7);
    Serial.print("/ ");
  }
  else if (ch == '\n') {
    Serial.println("");
  }
  else {
    Serial.println("Unknown Character!");
  }

}
*/

void String2Morse() {
  len = code.length();
  for (int i = 0; i < len; i++) 
  {
    ch = code.charAt(i);
    morse();
  }
}

void setup() {
  Serial.begin(9600);
  pinMode(led, OUTPUT);
 
  Serial.println("Type your message here ");
}

void loop() {
  while (Serial.available())
  
  {
    code = Serial.readString();
    Serial.println(code + " = ");
    String2Morse();
    Serial.println(); 
  }
  delay(1000); // avoid continuous reading
  }


receiver program (i know the space delay is wrong but right now i just want it to interpret dots and dashes and letters corectly)`Use code tags to format code for the forum`
type or pas#include <LiquidCrystal_I2C.h>

String code = "";
String message = "";

const int but = 2;

unsigned long pres_len = 0, rel_time, pres_time = 0, old_pres = 0, space = 0;
int state = 0;
int unit_delay = 250;
int min_delay = 10;

LiquidCrystal_I2C lcd(0x27, 16, 2); // Change the I2C address accordingly

char MakeString()
{
  if (pres_len >= (350) && pres_len <= (500))
  {
    return '.'; // if button press more than 0.4sec and less than 0.5s, it is a dot
  }
  else if (pres_len >= (1050) && pres_len < (1300))
  {
    return '-'; // if button press more than 1.2sec, it is a dash
  }
}

// Define a global variable to track cursor location
int cursorLocation = 0;

void Morse_decod()
{
  static String morse[] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....",
                            "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-",
                            ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "!"
  };
  int i = 0;
  while (morse[i] != "!")
  {
    if (morse[i] == code)
    {
      Serial.print(char('A' + i));

      // Print the character on the LCD
      lcd.print(char('A' + i));

      // Increment the cursor location
      cursorLocation++;

      // Check if the cursor reached the end of the first row
      if (cursorLocation >= 16)
      {
        // Move to the second row
        lcd.setCursor(0, 1);

        // Reset the cursor location
        cursorLocation = 0;
      }

      break;
    }
    i++;
  }
  /*if (morse[i] == "!")
  {
    Serial.println("");
    Serial.println("This code does not exist");
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Error");
  }*/

  code = "";
}

void setup()
{
  Serial.begin(9600);
  lcd.begin();
  pinMode(but, INPUT_PULLUP);
}

void loop()
{
label:
  while (digitalRead(but) == HIGH)
  {
  }
  old_pres = rel_time;
  pres_time = millis();
  while (digitalRead(but) == LOW)
  {
  }
  rel_time = millis();
  pres_len = rel_time - pres_time;
  space = pres_time - old_pres;

  // Check if there is a long enough pause to indicate a space
  if (space >= 60 * unit_delay)
  {
    Serial.print(" ");
    lcd.print(" ");

    // Increment the cursor location
    cursorLocation++;

    // Check if the cursor reached the end of the first row
    if (cursorLocation >= 16)
    {
      // Move to the second row
      lcd.setCursor(0, 1);

      // Reset the cursor location
      cursorLocation = 0;
    }
  }

  if (pres_len > min_delay)
  {
    code += MakeString();
  }
  while ((millis() - rel_time) < (unit_delay)) // the unit delay is 250ms
  {
    if (digitalRead(but) == LOW)
    {
      goto label;
    }
  }

  Morse_decod();
}te code here

what microcontrollers are you using? e.g. UNO, Mega, Nano, ESP32, etc

what is the pulse width of the transmitted signals?

post the complete code?

im using arduino uno

on an oscilloscope i measure the pulse to have a high of around 5V and low of around 0. The pulse lasts for the durations defined in the transmitter code.
this is the complete code. it is two separate programs

The end of the code you posted is incomplete. Do not revise, please

  • go to the IDe
  • fix anything that needs fixing
  • press ctrl-T (standardize format)
  • press shft-ctrl-C (copy for forum)

Then, in a new message, press ctrl-V. That will insert the code, complete, with code tags, all at once, for your convenience.

Separate the transmitting and receiving sketches.

You have a timing issue. E is a single dot. You'll need to determine the length of each signal as well as the time between signals.

Consider Software Serial for an additional serial port between the two Unos.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.