I have two arduino pro micro (atmega32u4), and i want to send a char over Serial from the first arduino and receive it from the other. If this char doesnt come (plug is disconnected) after an interval i want to know it.
With one SoftwareSerial it works perfect..with two i cannot make it work..(ultimatelely i would be happy with 3-4 serials). I tried "listen", but it confuses the program execution:
so my code is:
#include "MIDIUSB.h"
#include <SoftwareSerial.h>
#define myRxPin 14
SoftwareSerial myCable1(myRxPin, 99); //rx,tx
#define myRxPin2 15
SoftwareSerial myCable2(myRxPin2, 98); //rx,tx
bool newDataRecorded[3] = {false};
unsigned long prevMillis[3] = {0};
const long interval = 50;
void controlChange(byte channel, byte control, byte value) {
//send some midi
}
void pressTheButtons(byte control1, byte control2) {
controlChange(0, control1, 127);
delay(10);
controlChange(0, control2, 127);
delay(10);
}
void checkCable(SoftwareSerial& thisCable, int whichJack, byte control1, byte control2) {
thisCable.listen();
if (thisCable.available() > 0) {
char myID = thisCable.read();
if (myID == '!' ) {
prevMillis[whichJack] = millis();
if (newDataRecorded[whichJack] == false) {
Serial.println("!, on");
pressTheButtons(120, control2);
newDataRecorded[whichJack] = true;
}
}
}
if ((millis() - prevMillis[whichJack]) > interval) {
if (newDataRecorded[whichJack] == true) {
Serial.println("off");
pressTheButtons(control1, control2);
newDataRecorded[whichJack] = false;
}
}
}
void setup() {
Serial.begin(115200);
myCable1.begin(4800);
myCable2.begin(4800);
}
void loop(){
checkCable(myCable1, 1, 100, 101);
//this works fine, until i try to add the second SoftwareSerial with the following line
//checkCable(myCable2, 2, 110, 150);
}
Any suggestion?