Send an array over an Arduino to Arduino Serial Connection

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!

This doesn't do what you were hoping:

sizeof(4)

The conventional idiom is

sizeof pins / sizeof pins[0]

Not sure if my understanding is correct, but I've replaced that into both arduinos codes as such, no change just yet though...

Serial.write((byte*) (pins) , (sizeof pins / sizeof pins[0]));

Forgive me if that's wrong

Best to post all your code. To debug it though, I'd print the content of pins on both arduinos - just before sending on the transmitter and just after the read on the receiver.

Rightyo, I've got the debug println bits all done i just removed them from the code to post here. On the sender arduino each button returns a nice 1 on the pc screen independently. On the receiver side it looks more like the image at the bottom of this reply.

The sender code raw as follows:

#define a 34
#define b 35
#define c 32
#define d 25
int pins[] = {9, 9, 9, 9};
int i = 9;

int aa = 9;
int bb = 9;
int cc = 9;
int dd = 9;

void setup() {
  // put your setup code here, to run once:
Serial.begin(115200);
Serial2.begin(115200);
//int pins[] = {2, 4, 8, 3, 6};
pinMode(a,INPUT);
pinMode(b,INPUT);
pinMode(c,INPUT);
pinMode(d,INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:

//First
if (digitalRead(a) == HIGH){
  pins [0] = 1;
}
else{
  pins [0] = 0;
}
//Second
if (digitalRead(b) == HIGH){
  pins [1] = 1;
}
else{
  pins [1] = 0;
}
//Third
if (digitalRead(c) == HIGH){
  pins [2] = 1;
}
else{
  pins [2] = 0;
}
//Fourth
if (digitalRead(d) == HIGH){
  pins [3] = 1;
}
else{
  pins [3] = 0;
}

i = pins [0];
/*Serial.println(pins [0]);
Serial.println(pins [1]);
Serial.println(pins [2]);
Serial.println(pins [3]);*/
aa = pins [0];
bb = pins [1];
cc = pins [2];
dd = pins [3];
//Serial2.write((byte*) (aa) , sizeof(2));
//Serial2.write((byte*) (bb) , sizeof(2));
//Serial2.write((byte*) (cc) , sizeof(2));
//Serial2.write((byte*) (dd) , sizeof(2));

Serial.write((byte*) (pins) , (sizeof pins / sizeof pins[0]));
Serial2.write((byte*) (pins) , (sizeof pins / sizeof pins[0]));
delay (100);




}

The receiver code raw as follows:

#define a 32
#define b 25
#define c 26
#define d 27
int pins [] = {9, 9, 9, 9};
void setup() {
  // put your setup code here, to run once:
Serial.begin(115200);
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() {
  // put your main code here, to run repeatedly:
Serial2.readBytes((byte*) pins, 4 * (sizeof pins / sizeof pins[0]));

//First
if (pins [0] == 1){
  digitalWrite(a,HIGH);
}
else{
  digitalWrite(a,LOW);
}
//Second
if (pins [1] == 1){
  digitalWrite(b,HIGH);
}
else{
  digitalWrite(b,LOW);
}
//Third
if (pins [2] == 1){
  digitalWrite(c,HIGH);
}
else{
  digitalWrite(c,LOW);
}
//Fourth
if (pins [3] == 1){
  digitalWrite(d,HIGH);
}
else{
  digitalWrite(d,LOW);
}
Serial.println(pins [0]);
Serial.println(pins [1]);
Serial.println(pins [2]);
Serial.println(pins [3]);

}

You've left a multiply by four in here:

  // put your main code here, to run repeatedly:
Serial2.readBytes((byte*) pins, 4 * (sizeof pins / sizeof pins[0]));

What do you get on the receiver if you hold down button 'b'.

Also, do you have pulldown resistors wired up on the buttons?

Removed the 4 *

Nothing, at this stage the receiver is now printing:

1 or 0
9
9
9

so b, c, d, aren't recognised.

And no pulldown resistors.

Try these sketches:

Sender Sketch:

#define a 34
#define b 35
#define c 32
#define d 25
byte pins[] = {0, 0, 0, 0};

void setup()
{
  Serial.begin(115200);
  Serial2.begin(115200);
  pinMode(a, INPUT);
  pinMode(b, INPUT);
  pinMode(c, INPUT);
  pinMode(d, INPUT);
}

void loop()
{
  if (digitalRead(a) == HIGH) 
  {
    pins [0] = 1;
  }
  else 
  {
    pins [0] = 0;
  }
  //Second
  if (digitalRead(b) == HIGH) 
  {
    pins [1] = 1;
  }
  else 
  {
    pins [1] = 0;
  }
  //Third
  if (digitalRead(c) == HIGH) 
  {
    pins [2] = 1;
  }
  else {
    pins [2] = 0;
  }
  //Fourth
  if (digitalRead(d) == HIGH) {
    pins [3] = 1;
  }
  else 
  {
    pins [3] = 0;
  }
  Serial2.write(pins, sizeof pins);
  delay (1000);
}

Receiver Sketch:

#define a 32
#define b 25
#define c 26
#define d 27
byte pins [] = {0, 0, 0, 0};

void setup() {

  // put your setup code here, to run once:
  Serial.begin(115200);
  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()
{
  // put your main code here, to run repeatedly:
  Serial2.readBytes(pins, 4);
  digitalWrite(a, pins[0]);
  digitalWrite(b, pins[1]);
  digitalWrite(c, pins[2]);
  digitalWrite(d, pins[3]);
}

Absolute legend, works a charm, I'll have a look and learn from that, thanks mate.