Hi,
I'm connecting 2 Arduinos Pro Micros (though I have had this problem with all sorts of boards in similar setups) over Bluetooth using HC-05 modules.
I set the master to print integer values with a comma between them. Here is the code:
#define pot A0 // potentiometer for volume control
#define trigger1 2
#define trigger2 3
#define trigger3 4
#define trigger4 5
#define trigger5 6
#define lightPin 7
int trig1;
int trig2;
int trig3;
int trig4;
int trig5;
int volumeKnob;
int state;
long previousMillis;
long interval = 40;
void pair()
{
digitalWrite(lightPin, LOW); // turn LED on pin off
while(state == 0) {
state = digitalRead(statepin); // check to see when BT is paired
}
digitalWrite(lightPin, HIGH); // turn LED on pin 4 on
delay(200);
digitalWrite(lightPin, LOW); // turn LED on pin 4 off
delay(200);
digitalWrite(lightPin, HIGH); // turn LED on pin 4 on
delay(200);
digitalWrite(lightPin, LOW); // turn LED on pin 4 off
delay(500); // wait before transmitting data
digitalWrite(lightPin, HIGH); // turn LED on pin 4 on
}
// -----------------------------------------------------------------------------
// SETUP
// -----------------------------------------------------------------------------
void setup() {
Serial1.begin(9600); //Start Serial1, eventually connecting to HC-05
delay(2000); // Startup delay
pinMode(pot,INPUT);
pinMode(trigger1, INPUT_PULLUP);
pinMode(trigger2, INPUT_PULLUP);
pinMode(trigger3, INPUT_PULLUP);
pinMode(trigger4, INPUT_PULLUP);
pinMode(trigger5, INPUT_PULLUP);
pinMode(statepin,INPUT);
pinMode(lightPin, OUTPUT);
}
// -----------------------------------------------------------------------------
// MAIN LOOP
// -----------------------------------------------------------------------------
//Get things working in the loop then try to break out into functions.
void loop() {
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
state = digitalRead(statepin); // check to see when BT is paired
if (state == 0) {
pair();
}
}
trig1 = digitalRead(trigger1);
trig2 = digitalRead(trigger2);
trig3 = digitalRead(trigger3);
trig4 = digitalRead(trigger4);
trig5 = digitalRead(trigger5);
volumeKnob = analogRead(pot);
Serial1.print(trig1);
Serial1.print(",");
Serial1.print(trig2);
Serial1.print(",");
Serial1.print(trig3);
Serial1.print(",");
Serial1.print(trig4);
Serial1.print(",");
Serial1.print(trig5);
Serial1.print(",");
Serial1.println(volumeKnob);
}
And the other side is setup to parse the integers. Like this:
#include <SoftwareSerial.h>
int trig1;
int trig2;
int trig3;
int trig4;
int trig5;
int volumeKnob;
SoftwareSerial mySerial(15, 16); // RX, TX
// -----------------------------------------------------------------------------
// SETUP
// -----------------------------------------------------------------------------
void setup() {
mySerial.begin(9600);
Serial.begin(9600); //Start Serial
delay(2000); // Startup delay
}
// -----------------------------------------------------------------------------
// MAIN LOOP
// -----------------------------------------------------------------------------
//Get things working in the loop then try to break out into functions.
void loop() {
if (mySerial.available() > 0) {
// look for the next valid integer in the incoming serial stream:
trig1 = mySerial.parseInt();
trig2 = mySerial.parseInt();
trig3 = mySerial.parseInt();
trig4 = mySerial.parseInt();
trig5 = mySerial.parseInt();
volumeKnob = mySerial.parseInt();
// look for the newline. That's the end of your sentence:
if (mySerial.read() == '\n') {
}
}
Serial.print(trig1);
Serial.print(",");
Serial.print(trig2);
Serial.print(",");
Serial.print(trig3);
Serial.print(",");
Serial.print(trig4);
Serial.print(",");
Serial.print(trig5);
Serial.print(",");
Serial.println(volumeKnob);
}
The problem is I'm receiving data, but it's coming in in the wrong order. Almost like instead of 1,2,3,4,5,6 it's printing to serial as 3,4,5,6,1,2. If I reset the Arduino sometimes it's right, sometimes it's wrong. Very strange. can anyone see what I am doing wrong?