communication between 2 arduino to save the time when the relay is trigger

hello,
i am working on a project and i am using 2 arduinos one to trigger a relay at a specific time and when the relay is on i want to send the following parameters to the second arduino (minute,hour,day,month and year) in my project i am using a GLCD with the following library

the problem i had is the type of data used by the glcd which is uint8_t
and when i want to send the time parameters (minute , hour,day,month and year) i have random values
and to solve this problem i put them in a char using the following command sprintf as follow:
when the relay is on i use this code to send my parameters
char value[20];

sprintf(value,“1z%d:%dz%d/%d/%di”,minute(),hour(),day(),month(),year());
Serial.write(value);
delay(10);
and in the second arduino i am using the following code :

#include <SPI.h>
#include <SD.h>
File myFile;

char mystr[22]; //Initialized variable to store recieve

int a=0;
void setup() {

// Begin the Serial at 9600 Baud
Serial.begin(9600);

while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print(“Initializing SD card…”);
if (!SD.begin(10)) {
Serial.println(“initialization failed!”);
while (1);
}
Serial.println(“initialization done.”);

}
void loop() {

}

void serialEvent() {

if(Serial.available()>0){
SD.begin(10);
Serial.readBytes(mystr,17); //Read the serial data and
Serial.readStringUntil(‘i’);
Serial.println(mystr);
Serial.println(“RECEIVER”);

myFile = SD.open(“saa.txt”);
if (myFile) {
myFile = SD.open(“saa.txt”, FILE_WRITE);
myFile.print(mystr);
myFile.println(a);
a=a+1;
myFile.close();

} else {
// if the file didn’t open, print an error:
Serial.println(“error opening test.txt”);
}
}

}

the problem i had is when the arduino saves only 20 times and the when the relay is on more than 20 times the values are not saved in my sd card

help me please

hello ,
i am working on a project when i have to send rtc variables (minute ,hour,day, month and year) using serial communication at a specific time (when relay is trigger i want to send my data to the second arduino to save them on an sd card )
the data type i'm trying to send is : uint8_t
anyone help please on how to send these parameters over the serial port?

Have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable. It is what I use for Arduino to Arduino and Arduino to PC communication.

You can send data in a compatible format with code like this (or the equivalent in any other programming language)

Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker

However you may find that it is easier to use I2C. Have a look at this Arduino to Arduino I2C Tutorial

…R

thanks sir
what i did is the following :
putting it in an char array and then use sprintf function to send my data this is the code i’m using
char valeur1[20];
Serial.write(“a”);
sprintf(valeur1,“1z%d:%dz%d/%d/%di”,minute(),hour(),day(),month(),year());
Serial.write(valeur1);
Serial.flush();

and for receiving i’m using this one
#include <SPI.h>
#include <SD.h>
File myFile;
#include <SoftwareSerial.h>
SoftwareSerial mySerial(6, 7);
char mystr[22]; //Initialized variable to store recieve
int Val1;
char mystr1[22];
char mystr2[22];
char mystr3[22];
int a=0;
void setup() {

// Begin the Serial at 9600 Baud
Serial.begin(9600);

while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print(“Initializing SD card…”);
if (!SD.begin(10)) {
Serial.println(“initialization failed!”);
while (1);
}
Serial.println(“initialization done.”);

}
void loop() {

}

void serialEvent() {

if(Serial.available()>0){
if(Serial.read()==‘a’){
SD.begin(10);
Serial.readBytes(mystr,17); //Read the serial data and
Serial.readStringUntil(‘i’);

myFile = SD.open(“saa.txt”, FILE_WRITE);
myFile.print(mystr);
myFile.println(a);
a=a+1;
myFile.close();

}

}

}

but when i test my system it saves worked only for 20 times (save up to 20 ) more than 20 they are not saved and i dunno why ?
Mr robin do you have an explication because i don’t think char valeur1[20]; is the reason why the saving stops at 20

sidalirafa:
thanks sir
what i did is the following :

Did you try what I suggested in Reply #1?

...R

What does the serial output on the receiver say?

Maybe try putting a heartbeat LED on it so you can see if it has crashed.

hello ,
i am working on a project when i have to send rtc variables (minute ,hour,day, month and year) using serial communication at a specific time (when relay is trigger i want to send my data to the second arduino to save them on an sd card )
the data type i'm trying to send is : uint8_t
anyone help please on how to send these parameters over the serial port?

Hi sidalirafa,

Use the Serial.write() function to send the data type or structure holding your RTC data:

Serial.write((uint8_t*)&rtcData, sizeof(rtcData));

Topics on the same subject moved here and merged

Cross-posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting will result in a timeout from the forum.

In the future, please take some time to pick the forum board that best suits the topic of your question and then only post once to that forum board. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum board. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

Another merge

The duplicates in the French section of the forum have been left where they are