I am trying to build a bidirectional communication channel, using nRF24L01 and two arduinos. The message is introduced using a button (but) in Morse code, decoded and then sent when another button (but_send) is pressed. Although everything seems to work fine, the message is sent multiple times even though I added delays. I am a beginner in Arduino and I have no idea what else i could do. I even tried deleting the string right after radio.write, but it doesn't seem to work either. If anyone has an idea, please help!
These are the two codes. They are quite similar, but i added them both just in case:
#include <LiquidCrystal.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
struct record
{
char mesaj[10];
boolean state = 1;
};
typedef struct record Record;
Record button_state, button_state1 ;
RF24 radio(9, 10); // CE, CSN(pini nrf01)
const byte addresses [][6] = {"00001", "00002"}; //Setting the two addresses. One for transmitting and one for receiving
String code = "";
int len = 0;
char ch;
int ok;
char new_char;
int i1 = 0;
int i2 = 1;
const int but_send = 8;
const int but = 5;
const int led = 4;
const int del = 3;
String a = "";
unsigned long pres_len = 0, rel_time, pres_time = 0, old_time_len = 0, old_pres = 0, space = 0;
int state = 0;
int unit_delay = 250;
int min_delay = 10;
int buttonState = 0;
char MakeString()
{
if (pres_len < (unit_delay * 3) && pres_len > 50)
{
return '.'; //if button press less than 0.6sec, it is a dot
}
else if (pres_len > (unit_delay * 3))
{
return '-'; //if button press more than 0.6sec, it is a dash
}
}
void Morse_decod()
{
static String morse[] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....",
"..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-",
".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "!"
};
int i = 0;
while (morse[i] != "!")
{
if (morse[i] == code)
{
Serial.print(char('A' + i));
a = a + char('A' + i);
Serial.print(" ");
}
if (morse[i] == "!")
{
Serial.println("");
Serial.println("This code is not exist!");
}
code = "";
}
void sendText()
{
// Aici trebuie sa fe codul pentru trimiterea efectiva a msj
if (ok == 1) {
Serial.println("Lavinia:" + a);
}
ok = 0;
delay(100);
}
void setup() {
Serial.begin(9600);
pinMode(but, INPUT_PULLUP);
pinMode(but_send, INPUT_PULLUP);
pinMode(del, INPUT_PULLUP);
pinMode(led, OUTPUT);
radio.begin(); //Starting the radio communication
radio.openWritingPipe(addresses[0]); //Setting the address at which we will send the data
radio.openReadingPipe(1, addresses[1]); //Setting the address at which we will receive the data
radio.setPALevel(RF24_PA_MIN); //You can set it as minimum or maximum depending on the distance between the transmitter and receiver.
}
void loop() {
label:
while (digitalRead(but) == HIGH) {
delay(5);
radio.startListening();
if (radio.available()) //Looking for incoming data
{
radio.read(&button_state, sizeof(button_state));
if (button_state.state == 0)
{
ok = 1;
sendText();
String final = "";
for (int i = 0; i < strlen(button_state.mesaj); i++)
{
final += String(button_state.mesaj[i]);
}
ok = 1;
Serial.println("Iulia: " + final);
button_state.state = 1;
radio.stopListening();
}
else
{
//nu se intampla nimic daca eu nu am trimis mesaj
}
delay(5);
radio.stopListening(); //This sets the module as transmitter
button_state1.state = digitalRead(but_send);
int n = a.length();
char char_array[n + 1];
strcpy(char_array, a.c_str());
strcpy(button_state1.mesaj, char_array);
radio.write(&button_state1, sizeof(button_state1)); //Sending the data
}
if (digitalRead(del) == LOW && a[0] != '\0' ) {
Serial.println("Deleted string");
a = "\0";
Serial.println(a);
}
}
old_pres = rel_time;
pres_time = millis();
digitalWrite(led, HIGH);
while (digitalRead(but) == LOW) {}
rel_time = millis();
digitalWrite(led, LOW);
pres_len = rel_time - pres_time;
space = pres_time - old_pres;
if (pres_len > min_delay)
{
code += MakeString();
}
while ((millis() - rel_time) < (unit_delay * 3))
{
if (digitalRead(but) == LOW)
{
goto label;
}
}
Morse_decod();
delay(1000);
}
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 10); // CE, CSN(pini nrf01)
#include <LiquidCrystal.h>
struct record
{
char mesaj[10];
boolean state = 1;
};
typedef struct record Record;
const byte addresses [][6] = {"00001", "00002"}; //Setting the two addresses. One for transmitting and one for receiving
String code = "";
int len = 0;
char ch;
int ok;
int ok2;
char new_char;
int i1 = 0;
int i2 = 1;
const int but_send = 8;
const int but = 5;
const int led = 4;
const int del = 3;
String a = "";
unsigned long pres_len = 0, rel_time, pres_time = 0, old_time_len = 0, old_pres = 0, space = 0;
int state = 0;
int unit_delay = 250;
int min_delay = 10;
Record button_state, button_state1 ;
char MakeString()
{
if (pres_len < (unit_delay * 3) && pres_len > 50)
{
return '.'; //if button press less than 0.6sec, it is a dot
}
else if (pres_len > (unit_delay * 3))
{
return '-'; //if button press more than 0.6sec, it is a dash
}
}
void Morse_decod()
{
static String morse[] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....",
"..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-",
".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "!"
};
int i = 0;
while (morse[i] != "!")
{
if (morse[i] == code)
{
Serial.print(char('A' + i));
a = a + char('A' + i);
Serial.print(" ");
}
if (morse[i] == "!")
{
Serial.println("");
Serial.println("This code is not exist!");
}
code = "";
}
void sendText()
{
// Aici trebuie sa fe codul pentru trimiterea efectiva a msj
if (ok == 1) {
Serial.println("Iulia:" + a);
}
ok = 0;
delay(100);
}
void setup() {
Serial.begin(9600);
pinMode(but, INPUT_PULLUP);
pinMode(but_send, INPUT_PULLUP);
pinMode(del, INPUT_PULLUP);
pinMode(led, OUTPUT);
radio.begin(); //Starting the radio communication
radio.openWritingPipe(addresses[1]); //Setting the address at which we will send the data
radio.openReadingPipe(1, addresses[0]); //Setting the address at which we will receive the data
radio.setPALevel(RF24_PA_MIN); //You can set it as minimum or maximum depending on the distance between the transmitter and receiver.
}
void loop() {
label:
while (digitalRead(but) == HIGH) {
delay(5);
radio.stopListening(); //This sets the module as transmitter
button_state.state = digitalRead(but_send);
int n = a.length();
char char_array[n + 1];
strcpy(char_array, a.c_str());
strcpy(button_state.mesaj, char_array);
radio.write(&button_state, sizeof(button_state)); //Sending the data
delay(5);
radio.startListening(); //This sets the module as receiver
if (radio.available())
{
radio.read(&button_state1, sizeof(button_state1)); //Reading the data
String final = "";
if (button_state1.state == 0) {
ok = 1;
sendText();
for (int i = 0; i < strlen(button_state1.mesaj); i++)
{
final += String(button_state1.mesaj[i]);
}
Serial.println("Lavinia: " + final);
button_state1.state = 1;
}
}
if (digitalRead(del) == LOW && a[0] != '\0' ) {
Serial.println("Deleted string");
a = "\0";
Serial.println(a);
}
}
old_pres = rel_time;
pres_time = millis();
digitalWrite(led, HIGH);
while (digitalRead(but) == LOW) {}
rel_time = millis();
digitalWrite(led, LOW);
pres_len = rel_time - pres_time;
space = pres_time - old_pres;
if (pres_len > min_delay)
{
code += MakeString();
}
while ((millis() - rel_time) < (unit_delay * 3))
{
if (digitalRead(but) == LOW)
{
goto label;
}
}
Morse_decod();
delay(1000);
}
I believe the relevant part would be:
int n = a.length();
char char_array[n + 1];
strcpy(char_array, a.c_str());
strcpy(button_state.mesaj, char_array);
radio.write(&button_state, sizeof(button_state)); //Sending the data
delay(5);
where a is the message to be sent.