Hello all,
I have two arduinos, one has three buttons pinned in, the other has three lights pinned in. I have written an array on arduino 1 to log the on off values as a 1 or 0 in an array called pins. Then i send the array over a serial connection to arduino 2, which saves the signal to an array and turns on all four lights respectively to whether their button is on in the other arduino.
In a basic form my code is as follows in the sender unit:
#define a 34
#define b 35
#define c 32
#define d 25
int pins[] = {9, 9, 9, 9};
void setup() {
Serial2.begin(115200);
pinMode(a,INPUT);
pinMode(b,INPUT);
pinMode(c,INPUT);
pinMode(d,INPUT);
}
void loop() {
//First button
if (digitalRead(a) == HIGH){
pins [0] = 1;
}
else{
pins [0] = 0;
}
//This is replicated for b, c, and d but omitted for this already longish post, you get the idea.
Serial2.write((byte*) (pins) , sizeof(4));
delay (100);
}
Independently printing the a, b, c, d values in my pc screen shows they are being triggered correctly.
However on the receiving arduino it seems to only register the 1st switch (switch a), it receives 4 values over the serial connection, but buttons b, c, and d do not trigger any changes in the receiving arduino, wheras button a registers four 1's in the receiving arduino, only when held down for 4 times the 100ms delay obviously. Effectively I believe the sending arduino is only sending the value of button a.
The code for the receiving arduino is below.
#define a 32
#define b 25
#define c 26
#define d 27
int pins [] = {9, 9, 9, 9};
void setup() {
Serial2.begin(115200);
pinMode(a,OUTPUT);
pinMode(b,OUTPUT);
pinMode(c,OUTPUT);
pinMode(d,OUTPUT);
digitalWrite(a,LOW);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
digitalWrite(d,LOW);
}
void loop() {
Serial2.readBytes((byte*) pins, 4 * sizeof(int));
//First light
if (pins [0] == 1){
digitalWrite(a,HIGH);
}
else{
digitalWrite(a,LOW);
}
//Again ommitted b, c, and d just for this post
}
To summarise, how do i go about sending the entire array instead of just button a?
Also, how do I make the second arduino register when the array starts and ends so the correct light comes on instead of just all of them over time?
Thank you very much in advance!
