Hello,
I have try to change the code from the Nick Gammon Sample and library,
The code for the Master i have change is:
#include <RS485_protocol.h>
#include <SoftwareSerial.h>
const byte ENABLE_PIN = 4;
const byte LED_PIN = 13;
const byte But = 5;
SoftwareSerial rs485(2, 3); // receive pin, transmit pin
byte msg [2];
byte buf [10];
byte received = 0;
bool flag_High, flag_Low = false;
// callback routines
void fWrite (const byte what)
{
rs485.print (what);
}
int fAvailable ()
{
return rs485.available ();
}
int fRead ()
{
return rs485.read ();
}
void setup()
{
rs485.begin (28800);
pinMode (ENABLE_PIN, OUTPUT); // driver output enable
pinMode (LED_PIN, OUTPUT); // built-in LED
pinMode(But,INPUT);
Serial.begin(9600);
Serial.println("Master starting ...");
} // end of setup
void loop(){
//Check_Button();
if (digitalRead(But) == HIGH && flag_High == false){
Serial.println("Swithch HIGH ");
flag_Low = false;
flag_High = true;
byte msg [2] = {
1, // Address of device
1 // Turn On
}; // Switch on
digitalWrite(LED_PIN,HIGH);
digitalWrite (ENABLE_PIN, HIGH); // enable sending
sendMsg (fWrite, msg, sizeof msg);
delayMicroseconds (800);
digitalWrite (ENABLE_PIN, LOW); // disable sending
byte buf [10];
received = recvMsg (fAvailable, fRead, buf, sizeof buf);
}
if (digitalRead(But) == LOW && flag_Low == false){
Serial.println("Swithch LOW");
flag_Low = true;
flag_High = false;
byte msg [2] = {
1, // Address of device
0 // Turn Off
}; // Switch off
digitalWrite(LED_PIN,LOW);
digitalWrite (ENABLE_PIN, HIGH); // enable sending
sendMsg (fWrite, msg, sizeof msg);
delayMicroseconds (800);
digitalWrite (ENABLE_PIN, LOW); // disable sending
byte buf [10];
received = recvMsg (fAvailable, fRead, buf, sizeof buf);
}
//buf [10];
//received = recvMsg (fAvailable, fRead, buf, sizeof buf);
digitalWrite (LED_PIN, received == 0); // turn on LED if error
}
And for the slave it is:
#include <RS485_protocol.h>
#include <SoftwareSerial.h>
const byte ENABLE_PIN = 4;
const byte LED_PIN = 13;
SoftwareSerial rs485(2, 3); // receive pin, transmit pin
// callback routines
void fWrite (const byte what)
{
rs485.print (what);
}
int fAvailable ()
{
return rs485.available ();
}
int fRead ()
{
return rs485.read ();
}
void setup()
{
rs485.begin (28800);
pinMode (ENABLE_PIN, OUTPUT); // driver output enable
pinMode (LED_PIN, OUTPUT); // built-in LED
Serial.begin(9600);
Serial.println("Starting ...");
} // end of setup
void loop(){
byte buf[20];
byte received = recvMsg(fAvailable,fRead, buf, sizeof (buf)-1);
if (received){
if (buf [0] != 1){
return;
}
if (buf[1] == 0) digitalWrite(LED_PIN,LOW);
if (buf[1] == 2) digitalWrite(LED_PIN,HIGH);
}
// assemble message
byte msg [] = {
0, // device master
1, // turn light on
};
delay(10);
// send to slave
digitalWrite (ENABLE_PIN, HIGH); // enable sending
sendMsg (fWrite, msg, sizeof msg);
delayMicroseconds (800);
digitalWrite (ENABLE_PIN, LOW); // disable sending
}
The above example it use the library from the Nick site http://www.gammon.com.au/forum/?id=11428 (Thank you Nick for the Great Work)
My question is how can i modify the above to use the RS485_non_blocking.h library and if the above it is ok ....
Thank you in advance.
Best Regards,
Tasos