I am having trouble trying to send/read multiple data using serial. I have 2 xbee series 1s that are connected to arduinos with a wireless shield. I have been trying to figure out how to read multiple data from serial but have been having problems.
The sender has 3 switches, which I want to turn on an led on the receiver. I just want to send 3 0/1s and have the receiver read this data. I have already had success with using 1 button and 1 led.
Sender:
int button1pin = 11;
int button2pin = 10;
int button3pin = 9;
int button1state = 0;
int button2state = 0;
int button3state = 0;
void setup() {
Serial.begin(9600);
pinMode(button1pin, INPUT);
pinMode(button2pin, INPUT);
pinMode(button3pin, INPUT);
}
void loop() {
button1state = digitalRead(button1pin);
button2state = digitalRead(button2pin);
button3state = digitalRead(button3pin);
Serial.write(button1state);
Serial.write(button2state);
Serial.write(button3state);
delay(10);
}
Receiver:
int led1pin = 6;
int led2pin = 5;
int led3pin = 4;
int led4pin = 3;
int dataArray[] = {0, 0, 0};
byte inByte = 0;
void setup() {
Serial.begin(9600);
pinMode(led1pin, OUTPUT);
pinMode(led2pin, OUTPUT);
pinMode(led3pin, OUTPUT);
pinMode(led4pin, OUTPUT);
}
void loop() {
while(Serial.available() < 3) {
for(int i = 0; i < 3; i++) {
inByte = Serial.read();
dataArray[i] = inByte;
}
}
if(dataArray[0] == 1) {
digitalWrite(led1pin, HIGH);
} else {
digitalWrite(led1pin, LOW);
}
if(dataArray[1] == 1) {
digitalWrite(led2pin, HIGH);
} else {
digitalWrite(led2pin, LOW);
}
if(dataArray[2] == 1) {
digitalWrite(led3pin, HIGH);
} else {
digitalWrite(led3pin, LOW);
}
inByte = 0;
delay(10);
}