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