DrDiettrich:
loop() is just what the name indicates: it executes in an endless loop.
If you want something to happen only once, set a flag when it should be done, test the flag and do it once and reset the flag afterwards. Also see the StateChangeDetection example.
I add the flag status change in array format it work fine on transmitter side ( like A0/A1 and B0/B1 OFF/ON) but did not receive anything on receiver side. I think due to " Serial.print(txValue*); " here lost the byte and data should be send in byte which I try to do but something is wrong if you can correct me.*
My transmitter code is Below:
```
*#include "Arduino.h"
// Library to allow a Serial port on arbitrary pins
#include <SoftwareSerial.h>
// These are the pins we'll be talking to the RS485 device on
#define RS485rx 3 // RS485 Receive pin
#define RS485Tx 4 // RS485 Transmit pin
#define RS485inout 2 // RS485 Transmit or Receive status
#define RS485Transmit HIGH
#define RS485Receive LOW
#define ledPin 13
#define baudRate 115200
//////////////Btn***//////////////////
boolean LED_state[4] = {0}; // stores the states of the LEDs
boolean PState[4] = {0}; // stores the states of the LEDs
// Define the RS485 object
SoftwareSerial RS485(RS485rx, RS485Tx);
// The data bytes we're sending or receiving
byte rxValue;
byte txValue;
// -----------------------------------------------------------------
// SETUP SETUP SETUP SETUP SETUP
// -----------------------------------------------------------------
void setup() /****** SETUP: RUNS ONCE ******/
{
// Debugging window
Serial.begin(9600);
// switches
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
// LEDs
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
// Set modes for pins
pinMode(ledPin, OUTPUT);
pinMode(RS485inout, OUTPUT);
// Ensure the on-board LED is off
digitalWrite(ledPin, HIGH);
// Set RS485 device to read initially
digitalWrite(RS485inout, RS485Receive);
// Set the baud rate. The longer the wire the slower you should
// set the transmission rate. Anything here:
// 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 31250,
// 38400, 57600, and 115200
// MUST BE THE SAME AS THE SENDER UNIT!
RS485.begin(baudRate);
}
// -----------------------------------------------------------------
// LOOP LOOP LOOP LOOP LOOP LOOP LOOP
// -----------------------------------------------------------------
void loop()
{
ButtonDebounce();
}
void ButtonDebounce(void)
{
static byte buttonState[4] = {LOW, LOW, LOW, LOW, }; // the current reading from the input pin
static byte lastButtonState[4] = {LOW, LOW, LOW, LOW, }; // the previous reading from the input pin
char txValue[4] = {'A', 'B', 'C', 'D'};
// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
static long lastDebounceTime[8] = {0}; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
byte reading[4];
reading[0] = digitalRead(5);
reading[1] = digitalRead(6);
reading[2] = digitalRead(7);
reading[3] = digitalRead(8);
for (int i = 0; i < 4; i++) {
if (reading[i] != lastButtonState[i]) {
// reset the debouncing timer
lastDebounceTime[i] = millis();
}
if ((millis() - lastDebounceTime[i]) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
// if the button state has changed:
if (reading[i] != buttonState[i]) {
buttonState[i] = reading[i];
// only toggle the LED if the new button state is HIGH
if (buttonState[i] == HIGH) {
LED_state[i] = !LED_state[i];
PState[i] = !PState[i];
Serial.print(txValue[i]);
Serial.println(PState[i]);
}
}
}
}
// set the LEDs
digitalWrite(9, LED_state[0]);
digitalWrite(10, LED_state[1]);
digitalWrite(11, LED_state[2]);
digitalWrite(12, LED_state[3]);
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastButtonState[0] = reading[0];
lastButtonState[1] = reading[1];
lastButtonState[2] = reading[2];
lastButtonState[3] = reading[3];
}
void ActA()
{
char teststring[] = "R"; // Serial Communication is easy!";
txValue = teststring[0];
Serial.print("Sending:"); Serial.println(char(txValue));
// Set the RS485 to transmit mode and send the value
digitalWrite(RS485inout, RS485Transmit);
RS485.write(txValue);
// small delay to allow transmission to complete
delay(1);
// Switch RS485 to receive mode
digitalWrite(RS485inout, RS485Receive);
while (!RS485.available());
// After each character is sent look for a received answer
if (RS485.available())
{
// LED flicker
digitalWrite(ledPin, HIGH);
// Read the incoming byte
rxValue = RS485.read();
// Display it on the Serial Monitor as a char (not an integer value)
Serial.print("Got back:"); Serial.println(char(rxValue));
// Turn off LED
digitalWrite(ledPin, LOW); // Show activity
// Debugging delay so we can follow activity
delay(100);
}
}
void ActB()
{
char teststring[] = "S"; // Serial Communication is easy!";
txValue = teststring[0];
Serial.print("Sending:"); Serial.println(char(txValue));
// Set the RS485 to transmit mode and send the value
digitalWrite(RS485inout, RS485Transmit);
RS485.write(txValue);
// small delay to allow transmission to complete
delay(1);
// Switch RS485 to receive mode
digitalWrite(RS485inout, RS485Receive);
while (!RS485.available());
// After each character is sent look for a received answer
if (RS485.available())
{
// LED flicker
digitalWrite(ledPin, HIGH);
// Read the incoming byte
rxValue = RS485.read();
// Display it on the Serial Monitor as a char (not an integer value)
Serial.print("Got back:"); Serial.println(char(rxValue));
// Turn off LED
digitalWrite(ledPin, LOW); // Show activity
// Debugging delay so we can follow activity
delay(100);
}
}*
```