In this sketch I receive at fixed intervals a few byte worth of datastring over a serial link. These datastrings are sent with 2000ms intervals. What should happen at the receiver end is to let output 12 (LED1) go HIGH during interval1 as soon as new data is being received. Output 9 (LED2) needs to go HIGH during interval2 if new data has been received and if a condition on these new data is fulfilled (which for the case of testing is always true).
The issue I have is that these outputs never go HIGH at the same time. Suppose interval1 = 500 and interval2 = 600 then LED1 will light up during 500ms and only after LED1 stops will LED2 light up, and during 100ms only (interval2 - interval1).
First sketch here below is the receiver sketch. Underneath is the transmitter sketch.
// http://forum.arduino.cc/index.php?topic=499210.msg3407044#msg3407044
// http://forum.arduino.cc/index.php?topic=396450.0
// HC12 Communication between Arduinos
/*
This program serves to receive two integers (or byte) from one (later to become two different)
HC12 transmitter.
The data are sent with start- and endmarkers
// RECEIVER PART
*/
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing
// variables to hold the parsed data
char messageFromPC[numChars] = {0};
int integer1FromPC;
int integer2FromPC;
static int previousInteger1FromPC;
static int previousInteger2FromPC;
float floatFromPC = 0.0;
boolean newData = false;
//============
void setup() {
Serial.begin(115200);
// HC12.begin(115200); // Open serial port to HC12
// declare pin 10, 11 & 12 as output
pinMode (10, OUTPUT); // sensor value from A
pinMode (11, OUTPUT); // sensor value from B
pinMode (12, OUTPUT); // data being received
pinMode (9, OUTPUT); // data received is different from previous data
delay(100);
}
void loop() {
recvWithStartEndMarkers();
lightLEDwhenNewMessage();
if (newData == true) {
strcpy(tempChars, receivedChars);
Serial.println(receivedChars); // serial monitor output
// this temporary copy is necessary to protect the original data
// because strtok() used in parseData() replaces the commas with \0
parseData();
useParsedData();
newData = false;
}
}
//============
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
boolean rxStatus = false;
boolean ledState = false;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
//============
void parseData() { // split the data into its parts
previousInteger1FromPC = integer1FromPC;
previousInteger2FromPC = integer2FromPC;
char * strtokIndx; // this is used by strtok() as an index. It creates the variable
// strkIndx as a pointer to a variable of type char. It can hold the address of a
// variable of type char.
/*
strtokIndx = strtok(tempChars, ","); // get the first part - the string
strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
integer1FromPC = atoi(strtokIndx); // convert this part to an integer
/*
strtokIndx = strtok(NULL, ",");
floatFromPC = atof(strtokIndx); // convert this part to a float
*/
strtokIndx = strtok(tempChars, ","); // get the first part - the string
integer1FromPC = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
integer2FromPC = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC
}
//============
void useParsedData() {
if (messageFromPC[0] == 'A') analogWrite (10, integer1FromPC);
if (messageFromPC[0] == 'B') analogWrite (11, integer2FromPC);
}
//============
void lightLEDwhenNewMessage() {
static unsigned long previousMillis1; // timer
static unsigned long previousMillis2;
static const int interval1 = 500;
static const int interval2 = 600;
if (newData == true) {
digitalWrite(12, HIGH); // new data received
previousMillis1 = millis();
}
if (millis() - previousMillis1 >= interval1) {
digitalWrite(12, LOW);
}
if (newData == true && integer1FromPC == integer2FromPC) {
digitalWrite (9, HIGH); // new data different from previous data
previousMillis2 = millis();
}
// if (digitalRead(9) == HIGH) {
if (millis() - previousMillis2 >= interval2) {
digitalWrite (9, LOW);
}
}
Transmitter sketch:
// http://forum.arduino.cc/index.php?topic=499210.msg3407044#msg3407044
// http://forum.arduino.cc/index.php?topic=396450.0
// HC12 Communication between Arduinos
/*
This program serves to send two integers (or byte) from one (later to become two different)
HC12 transmitter.
The data are sent with start- and endmarkers
// TRANSMITTER PART
*/
int analogValue5, val5;
int HC12power = 8;
const int interval1 = 100; // HC12 power-off delay time in milliseconds
const int interval3 = 2000; // HC12 fixed retransmission timings
unsigned long previousMillis1 = 0; // timer1
// unsigned long previousMillis2; // timer2
unsigned long previousMillis3 = 0; // timer3
void setup()
{
Serial.begin(115200); // Open serial port to computer
// HC12.begin(9600); // Open serial port to HC12
pinMode(HC12power, OUTPUT);
}
void loop() {
// read analog pin 5
analogValue5 = analogRead(5);
// remap values from the analogValue5 variable to 0 / 255
val5 = map(analogValue5, 0, 1023, 0, 255);
if (millis() - previousMillis3 >= interval3) {
previousMillis3 = millis();
if (digitalRead(HC12power) == LOW) {
digitalWrite(HC12power, HIGH);
previousMillis1 = millis();
delay(50);
}
Serial.print('<');
Serial.print(val5);
Serial.print(',');
Serial.print(val5);
Serial.print(",B>");
/*
HC12.print('<');
HC12.print(val5);
HC12.print(',');
HC12.print(val5);
HC12.print(",B>");
*/
}
//start timer to switch off HC12
if (millis() - previousMillis1 >= interval1) {
digitalWrite(HC12power, LOW);
}
}