Hello everyone, i am trying to incorporate the blocking example and use it with the non-blocking library from this tutorial https://www.gammon.com.au/forum/?id=11428 .
My problem is that i dont have the max485 chips yet and i would like to be ready when they arrive.
What i wrote so far is this.
Non-blocking transmitter :
#include <RS485_non_blocking.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.write (what);
}
int fAvailable ()
{
return rs485.available ();
}
int fRead ()
{
return rs485.read ();
}
RS485 myChannel (NULL, NULL, fWrite, 0);
void setup()
{
rs485.begin (28800);
pinMode (ENABLE_PIN, OUTPUT); // driver output enable
pinMode (LED_PIN, OUTPUT); // built-in LED
} // end of setup
//byte old_level = 0;
void loop()
{
// read potentiometer
byte level = analogRead (0) / 4;
// assemble message
byte msg [] = {
1, // device 1
2, // turn light on
level // to what level
};
// send to slave
myChannel.sendMsg (msg, sizeof (msg));
delay (1000);
} // end of loop
Non-blocking reciever :
#include <RS485_non_blocking.h>
#include <SoftwareSerial.h>
SoftwareSerial rs485 (2, 3); // receive pin, transmit pin
const byte ENABLE_PIN = 4;
void fWrite (const byte what)
{
rs485.write (what);
}
int fAvailable ()
{
return rs485.available ();
}
int fRead ()
{
return rs485.read ();
}
RS485 myChannel (fRead, fAvailable, NULL, 20);
void setup()
{
rs485.begin (28800);
pinMode (ENABLE_PIN, OUTPUT); // driver output enable
}
void loop()
{
byte buf [10];
if (myChannel.update ())
{
Serial.write (myChannel.getData (), myChannel.getLength ());
Serial.println ();
if (buf [0] != 1)
return; // not my device
if (buf [1] != 2)
return; // unknown command
byte msg [] = {
0, // device 0 (master)
3, // turn light on command received
};
//byte received = recvMsg (fAvailable, fRead, buf, sizeof (buf));
delay (1); // give the master a moment to prepare to receive
analogWrite (11, buf [2]); // set light level
} // end if something received
} // end of loop
Is this code correct? Do you guys think it will work? or did i leave something out?
Thanks in advance!