I'm using a RedBear Duo to read the Serial writes from a RedBear Nano v2. Basically, I use a digital pin of the Duo to raise an interrupt on the Nano. Once, the nano catches the interrupt it writes on its Serial.
You can find a minimum working example below:
RedBear Duo
#include <Wire.h>
/*BLE and WiFi setup*/
#if defined(ARDUINO)
SYSTEM_MODE(MANUAL);
#endif
char incomingByte; // for incoming serial data
void setup() {
Serial1.begin(115200); // opens serial port, sets data rate to 9600 bps
delay(500);
Serial1.println("Begin!");
Serial2.begin(115200);
pinMode(D5, OUTPUT);
}
void readSerial() {
while (Serial2.available() > 0) {
incomingByte = Serial2.read();
Serial1.print(incomingByte);
}
Serial1.println(" ");
}
void loop() {
digitalWrite(D5, HIGH);
readSerial();
delay(2); // Here's the delay I'm talking about
digitalWrite(D5, LOW);
delay(1000);
}
RedBear Nano
uint8_t fire;
void task_handle(void) {
fire = 1;
}
void setup() {
Serial.begin(115200);
fire = 0;
pinMode(D4, INPUT);
attachInterrupt(D4, task_handle, RISING);
}
void loop() {
if (fire == 1) {
fire = 0;
Serial.write("hello ");
// Serial.println("fire");
digitalWrite(D13, HIGH);
}
delay(150);
}
I found this question (https://electronics.stackexchange.com/questions/28739/arduino-delay-of-1ms-necessary-between-serial-read-and-serial-write), and I understood that the problem might be the fact that it takes a while to empty the Serial buffer.
However, I tried to bring the baudrate at 500000 (more than 3x) and still if I change the delay to delay(1), the code is not printing what it has received on the Serial.
I'm trying to understand what is really going on and why I need this delay(2).
Thanks a lot in advance!