RS485 sending array?

Hello people.
I am trying to set up a RS485 connection between 2 arduinos. I successfully sent a single bit across the modbus network to turn 1 relay on and off. No worries. I setup the installation from this tutorial.

http://arduino-info.wikispaces.com/SoftwareSerialRS485Example

What I'm trying to do now is send an array across the system. The project I want to do it control 5 relays from 5 switches. When I connect it up like the last example and install the following code it turns the relay on but doesn't turn off when the switch turns off.

The led13 does indicate it's still receiving information but the array data[0]; doesn't change. The variable on the transmitter side does change with the switching.

have I got the completely wrong approach with this?

TRANSMITTER.

/* YourDuino SoftwareSerialExample1

  • Connect to another Arduino running "YD_SoftwareSerialExampleRS485_1Remote"
  • Connect this unit Pins 10, 11, Gnd
  • Pin 3 used for RS485 direction control
  • To other unit Pins 11,10, Gnd (Cross over)
  • Open Serial Monitor, type in top window.
  • Should see same characters echoed back from remote Arduino

Questions: terry@yourduino.com
*/

/-----( Import needed libraries )-----/
#include <SoftwareSerial.h>
/-----( Declare Constants and Pin Numbers )-----/
#define SSerialRX 10 //Serial Receive pin
#define SSerialTX 11 //Serial Transmit pin

#define SSerialTxControl 3 //RS485 Direction control

#define RS485Transmit HIGH
#define RS485Receive LOW

#define Pin13LED 13

/-----( Declare objects )-----/
SoftwareSerial RS485Serial(SSerialRX, SSerialTX); // RX, TX

/-----( Declare Variables )-----/
int previousButtonStatus;
int buttonStatus;
int button = A0;
int data[5] = {1, 1, 1, 1, 1};

void setup() /****** SETUP: RUNS ONCE ******/
{
// Start the built-in serial port, probably to Serial Monitor
Serial.begin(9600);

pinMode(Pin13LED, OUTPUT);
pinMode(SSerialTxControl, OUTPUT);
pinMode(button, INPUT_PULLUP);

digitalWrite(SSerialTxControl, RS485Receive); // Init Transceiver

// Start the software serial port, to another device
RS485Serial.begin(4800); // set the data rate

}//--(end setup )---

void loop() /****** LOOP: RUNS CONSTANTLY ******/
{

buttonStatus = digitalRead (button); // variable that updates with change in button state
data [0] = buttonStatus;
if (previousButtonStatus != buttonStatus){ // if button state changes.
previousButtonStatus = buttonStatus; // reset variable to current position. On or off. the variable will know next read weather the state has changed.

digitalWrite(Pin13LED, HIGH); // Show activity

digitalWrite(SSerialTxControl, RS485Transmit); // Enable RS485 Transmit
RS485Serial.write(data[5]); // Send byte to Remote Arduino

digitalWrite(Pin13LED, LOW); // Show activity
delay(10);

}
int dataTest = data[0];
Serial.println(dataTest);

}//--(end main loop )---

/-----( Declare User-written Functions )-----/

//NONE
//( THE END )**

RECEIVER.

/* YourDuino SoftwareSerialExample1Remote

  • Used with YD_SoftwareSerialExampleRS485_1 on another Arduino
  • Remote: Receive data, loop it back...
  • Connect this unit Pins 10, 11, Gnd
  • To other unit Pins 11,10, Gnd (Cross over)
  • Pin 3 used for RS485 direction control
  • Pin 13 LED blinks when data is received

Questions: terry@yourduino.com
*/

/-----( Import needed libraries )-----/
#include <SoftwareSerial.h>
/-----( Declare Constants and Pin Numbers )-----/
#define SSerialRX 10 //Serial Receive pin
#define SSerialTX 11 //Serial Transmit pin

#define SSerialTxControl 3 //RS485 Direction control
#define RS485Transmit HIGH
#define RS485Receive LOW

#define Pin13LED 13

/-----( Declare objects )-----/
SoftwareSerial RS485Serial(SSerialRX, SSerialTX); // RX, TX

/-----( Declare Variables )-----/
int buttonStatus = 1;
int relay = A5;
int data[5] ;

void setup() /****** SETUP: RUNS ONCE ******/
{
// Start the built-in serial port, probably to Serial Monitor
Serial.begin(9600);

pinMode(Pin13LED, OUTPUT);
pinMode(SSerialTxControl, OUTPUT);
pinMode (relay, OUTPUT);

digitalWrite (relay, HIGH);
digitalWrite(SSerialTxControl, RS485Receive); // Init Transceiver

// Start the software serial port, to another device
RS485Serial.begin(4800); // set the data rate
}//--(end setup )---

void loop() /****** LOOP: RUNS CONSTANTLY ******/
{

if (RS485Serial.available()) // if data comes through rs485
{
data[5] = RS485Serial.read(); // Read the byte
buttonStatus = data[0]; // store first bit into variable.

digitalWrite(Pin13LED, HIGH); // Show activity
delay(10);
digitalWrite(Pin13LED, LOW);

}// End If RS485SerialAvailable

if (buttonStatus == 0){
digitalWrite (relay, LOW);} // turn relay on
else {digitalWrite (relay, HIGH);} // turn relay off.

Serial.println(buttonStatus);

}//--(end main loop )---

/-----( Declare User-written Functions )-----/
//NONE

//( THE END )**

What I think I will do is just allocate a button with a decimal number to send to the other Arduino. each time that number is received I can switch relay. Moving array across modbus seems a bit harder than I expected. More reading is needed.

This might not be array related as your heading suggests, but this code will transmit the status of 8 switches to a receiving Arduino and light up 8 led's. The receiving Arduino does the same in reverse. Reads 8 binary signals, transmits back to master Arduino and display status of switches on 8 LED's

I am making use of Serial In/Out on both Arduino's as a normal UNO does not have enough I/O for 8 input and 8 output signals


This code is loaded into both Arduinos

/* ------------------------------------------------
IO Expander with RS485 communication
Written for Arduino Duemilanove
---------------------------------------------------
See end of file for sketch description
------------------------------------------------ */

//--------------------------------------------------------
// RS485
//--------------------------------------------------------
#include <SoftwareSerial.h>
#define bRX            (10)        // Serial Receive pin
#define bTX            (11)        // Serial Transmit pin
#define TxRxCtrl    (12)        // RS485 Direction control
#define Pin13        (13)        // RS485 Active indication
#define RS485Tx        HIGH        // For Tx Bit High
#define RS485Rx        LOW            // For RX bit Low
int ByteRx;                        // RS485 Receiving byte
int ByteTx;                        // RS485 Transmitting byte
byte Old_PISO_Byte;                // Register to hold PISO_Byte values to see if there were changes
SoftwareSerial RS485Serial(bRX, bTX);    // RX, TX

//--------------------------------------------------------
//Pin connections of 74HC164 SIPO
//--------------------------------------------------------
#define SIPO_Data    ( 8 )            // Pin 8 on Arduino goes to pins 1&2 on 74HC164
#define SIPO_Clock    (9)            // Pin 9 on Arduino goes to pin8 on 74HC164
byte SIPO_Byte;                    // SIPO Output Byte

//--------------------------------------------------------
//Pin connections of 74HC165 PISO
//--------------------------------------------------------
#define PIN_PL        (5)            // Pin 5 on Arduino goes to pin 1 of 74HC165
#define PIN_CP        (6)            // Pin 6 on Arduino goes to pin 2 of 74HC165
#define PIN_Q7        (7)            // Pin 7 on Arduino goes to pin 9 of 74HC165
byte j;                            // Counter variable for Serial input
byte InVal;                        // PISO Input Value 
byte PISO_Byte;                    // PISO Input Byte

// --------------------- SETUP ---------------------------
void setup()
{  // Start the built-in serial port, probably to Serial Monitor
    Serial.begin(28800);
//--------------------------------------------------------
// RS485 Hardware 
//--------------------------------------------------------
    pinMode(Pin13, OUTPUT);                    // Define LED pin   
    pinMode(TxRxCtrl, OUTPUT);                // Set PinMode for Direction control
    digitalWrite(TxRxCtrl, RS485Rx);        // Init Transceiver

//  Start the software serial port, to another device
    RS485Serial.begin(28800);                // set the data rate

//--------------------------------------------------------
// 74HC164 SIPO Hardware 
//--------------------------------------------------------
    pinMode(SIPO_Clock, OUTPUT);            // 74HC164 clock input (LOW-to-HIGH, edge-triggered)
    pinMode(SIPO_Data, OUTPUT);                // 74HC164 data input

//--------------------------------------------------------  
// 74HC165 PISO Hardware 
//--------------------------------------------------------
    pinMode(PIN_PL, OUTPUT);                // 74HC165 asynchronous parallel load input (active LOW)
    pinMode(PIN_CP, OUTPUT);                // 74HC165 clock input (LOW-to-HIGH edge-triggered)
    pinMode(PIN_Q7, INPUT);                    // 74HC165 serial output from the last stage
//--------------------------------------------------------
// Initialise Serial-In chip
//--------------------------------------------------------
	digitalWrite(PIN_PL, HIGH);
	digitalWrite(PIN_CP, LOW);
}  
// --------------------(end setup )---------------------


// --------------------- LOOP ---------------------------
void loop()
{
// --------------------------------------------------------
// 74HC165 PISO - Processing Inputs
// --------------------------------------------------------

//  load logic bits from the DIP switches
	digitalWrite(PIN_PL, LOW);                  // LOAD BITS

//  reset "load" line, this freezes the internal buffer on both chips
	digitalWrite(PIN_PL, HIGH);

	for(j = 0 ; j < 8 ; j++)
	{
		InVal = digitalRead(PIN_Q7);

//	Clock data one cycle to read next "switch"
		digitalWrite(PIN_CP, HIGH);                    // Clock Pulse On
		digitalWrite(PIN_CP, LOW);                    // Clock Pulse Off

		Serial.print(InVal);                        // #### Diagnostic use only. To be deleted
		if (InVal == HIGH)
			{
				bitSet(PISO_Byte, j) ;                // Set bits in Byte PISO_Byte to make numeric value
			}
		else
			{
				bitClear(PISO_Byte, j) ;            // Clear bits in Byte PISO_Byte to make numeric value
			}
	}	// End for(j = 0 ; j < 8 ; j++)

// --------------------------------------------------------
// RS485 - Send/Receive RS485 data
// Transmit to Slave
// --------------------------------------------------------

	if (PISO_Byte != Old_PISO_Byte)                    // Compare if switches have changed from last reading
		{
		digitalWrite(Pin13, HIGH);                // Show activity

			digitalWrite(TxRxCtrl, RS485Tx);        // Enable RS485 Transmit   
			RS485Serial.write(PISO_Byte);            // Send byte to Remote Arduino

			digitalWrite(Pin13, LOW);                // Show activity    
			delay(10);
			digitalWrite(TxRxCtrl, RS485Rx);        // Disable RS485 Transmit       
		}
// --------------------------------------------------------
// See if Slave replied 
// --------------------------------------------------------
	if (RS485Serial.available())                    // Look for data from other Arduino
		{
			digitalWrite(Pin13, HIGH);                // Show activity
			SIPO_Byte = RS485Serial.read();            // Read received byte
			Serial.println(SIPO_Byte);                // Show on Serial Monitor
			delay(10);
			digitalWrite(Pin13, LOW);                // Show activity   
		}
  
// --------------------------------------------------------
// 74HC164 SIPO - Processing Outputs
// --------------------------------------------------------

	shiftOut(SIPO_Data, SIPO_Clock, LSBFIRST, SIPO_Byte); 

	Old_PISO_Byte = PISO_Byte;                        // Save value of PISO_Byte to compare for changes
} 
//--(end main loop )---

/*
--------------------------------------------------------
Arduino Pin connection
--------------------------------------------------------
Pin 00: - 
Pin 01: - 
Pin 02: - 
Pin 03: - 
Pin 04: - 
Pin 05: - 74HC165 PISO -
Pin 06: - 74HC165 PISO -
Pin 07: - 74HC165 PISO -

Pin 08: - 74HC164 SIPO - Data 
Pin 09: - 74HC164 SIPO - Clock
Pin 10: - RS485 (RO)
Pin 11: - RS485 (DI)
Pin 12: - RS485 Direction Control
Pin 13: - RS485 Data active LED 

--------------------------------------------------------
RS485 Pin Connections
--------------------------------------------------------
                        +-----\/-----+
        Arduino 10 -----|1 RO   Vcc 8|----- Vcc
                     +--|2 /RE    A 7|----- A    - RS485 Line A
        Arduino 12 --+--|3 DE    /B 6|----- /B    - RS485 Line B
        Arduino 11 -----|4 DI   Gnd 5|----- Gnd
                        +------------+   

--------------------------------------------------------
74HC164 (SIPO) Pin Connections
--------------------------------------------------------

							+------\/------+
		Arduino 08 --+--|01 DSA  Vcc 14|----- Vcc
						+--|02 DSB   Q7 13|----- LED7 
			  LED0 -----|03 Q0    Q6 12|----- LED6 
			  LED1 -----|04 Q1    Q5 11|----- LED5
			  LED2 -----|05 Q2    Q4 10|----- LED4
			  LED3 -----|06 Q3   /MR 09|----- Gnd
			   Gnd -----|07 Gnd   CP 08|----- Arduino 09
								+--------------+   

--------------------------------------------------------
74HC165 (PISO) Pin Connections
--------------------------------------------------------

							+------\/------+
		Arduino 05 -----|01 /PL  Vcc 16|----- Vcc
		Arduino 06 -----|02 CP   /CE 15|----- Gnd
			   Sw4 -----|03 D4    D3 14|----- Sw3
			   Sw5 -----|04 D5    D2 13|----- Sw2
			   Sw6 -----|05 D6    D1 12|----- Sw1
			   Sw7 -----|06 D7    D0 11|----- Sw0
			   NC       |07 /Q7   DS 10|----- Gnd
			   Gnd -----|08 Gnd   Q7 09|----- Arduino 07
						+--------------+

*/