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 )**