i trying multiplex between arduino mega using RS485 i succeed turn 1 output with 1 input
but now the problem i need turn an array of input through rs485 to output
MASTER
/*-----( Import needed libraries )-----*/
#include <SoftwareSerial.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define SSerialRX 10 //Serial Receive pin
#define SSerialTX 11 //Serial Transmit pin
#define RS485Transmit HIGH
#define RS485Receive LOW
#define Pin13LED 13
SoftwareSerial RS485Serial(SSerialRX, SSerialTX); // RX, TX
int byteReceived;
int byteSend;
byte buttons[] = {22, 24, 26, 28, 30, 32, 34, 36, 38};
#define NUMBUTTONS sizeof(buttons)
byte lastStates[sizeof(buttons)];
int buttonState = 0; // current state of the button
int lastState = 0; // previous state of the button
void setup() /****** SETUP: RUNS ONCE ******/
{
// Start the built-in serial port, probably to Serial Monitor
Serial.begin(9600);
Serial.println("Error Checking ");
pinMode(Pin13LED, OUTPUT);
for (int i=0;i<NUMBUTTONS;i++)
pinMode(buttons[i], INPUT);
// Start the software serial port, to another device
RS485Serial.begin(9600); // set the data rate
}//--(end setup )---
void loop()
{
// flag to indicate that a button changed state
bool fChanged = false;
// values of buttons
byte buttonValues = 0;
// read all buttons and check for change
for (int i = 0; i < sizeof(buttons); i++)
{
byte buttonState = digitalRead(buttons[i]);
if (buttonState != lastStates[i])
{
// update last state
lastStates[i] = buttonState;
// indicate that a button changed
fChanged = true;
// set flags in button values
buttonValues |= (1 << i);
}
}
// if a button changed, send the data
if (fChanged == true)
{
RS485Serial.write(buttonValues);
Serial.print(buttonValues);
}
}
serial monitor show 1,2,4,8,16,32,64...etc
so i can only have 8 input? how to send more than that?
SLAVE
#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
const byte leds[] = { 22, 24, 26, 28, 30, 32, 34, 36, 38 };
/*-----( Declare objects )-----*/
SoftwareSerial RS485Serial(SSerialRX, SSerialTX); // RX, TX
/*-----( Declare Variables )-----*/
int byteReceived;
int byteSend;
void setup() /****** SETUP: RUNS ONCE ******/
{
for (int cnt = 0; cnt < sizeof(leds); cnt++)
{
pinMode(leds[cnt], OUTPUT);
digitalWrite(leds[cnt], LOW);
}
// Start the built-in serial port, probably to Serial Monitor
Serial.begin(9600);
Serial.println("SerialRemote"); // Can be ignored
pinMode(SSerialTxControl, OUTPUT);
digitalWrite(SSerialTxControl, RS485Receive); // Init Transceiver
// Start the software serial port, to another device
RS485Serial.begin(4800); // set the data rate
}//--(end setup )---
void loop(){
// put your main code here, to run repeatedly:
if(RS485Serial.available())
{
byte b = RS485Serial.read();
Serial.print(b, DEC);
// loop through the bits in the received byte for every defined LED
for (int cnt = 0; cnt < sizeof(leds); cnt++)
{
digitalWrite(leds[cnt], b & (1 << cnt));
}
}
Serial.println();
}
The LED sometime stay light on sometime light off not responsive
Just beginner here sorry to cause any inconvenience
Moderator edit:
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags added.