(What do people have against using the code tags </> button on the menu, And CTRL-T in the IDE before posting? Moderator)
//master code
#include "RS485_protocol.h"
#include <SoftwareSerial.h>
int led=6;
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 ();
}
void setup()
{
rs485.begin (28800);
pinMode (ENABLE_PIN, OUTPUT); // driver output enable
pinMode (LED_PIN, OUTPUT); // built-in LED
pinMode(led,OUTPUT);
Serial.begin(9600);
//Serial.println("c");
} // end of setup
byte old_level = 0;
void loop()
{
//digitalWrite(led,LOW);
//Serial.println("b");
// read potentiometer
byte level = analogRead (A0) / 4;
//Serial.println(level);
// no change? forget it
if (level == old_level)
return;
// assemble message
byte msg [] = {
1, // device 1
2, // turn light on
level // to what level
};
//Serial.println(msg[1,2,level]);
// send to slave
digitalWrite (ENABLE_PIN, HIGH);
//Serial.println(digitalRead(ENABLE_PIN));// enable sending
sendMsg (fWrite, msg, sizeof msg);
digitalWrite (ENABLE_PIN, LOW); // disable sending
// Serial.println(digitalRead(ENABLE_PIN));
// receive response
byte buf [10];
byte received = recvMsg (fAvailable, fRead, buf, sizeof buf);
// Serial.println(buf[1]);
Serial.println(buf[1]);
if( buf[1]==3)
{
digitalWrite(led,HIGH);
}
else
{
digitalWrite(led,LOW);
}
digitalWrite (LED_PIN, received == 0); // turn on LED if error
// only send once per successful change
if (received)
old_level = level;
} // end of loop
//slave code
#include <SoftwareSerial.h>
#include "RS485_protocol.h"
const byte LED_PIN = 12;
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 ();
}
void setup()
{
rs485.begin (28800);
Serial.begin(9600);
pinMode (ENABLE_PIN, OUTPUT);
pinMode (LED_PIN, OUTPUT); // built-in LED// driver output enable
}
void loop()
{
byte buf [10];
byte received = recvMsg (fAvailable, fRead, buf, sizeof (buf));
if (received)
{
//Serial.println("a");
if (buf [0] != 1)
return; // not my device
if (buf [1] != 2)
return; // unknown command
// Serial.println(buf[1]);
//Serial.println("a");
// if (buf[1]==2)
//// {
//// digitalWrite(LED_PIN,HIGH);
//// Serial.println("ON");
//// }
byte msg [] = {
0, // device 0 (master)
3, // turn light on command received
};
delay (1); // give the master a moment to prepare to receive
digitalWrite (ENABLE_PIN, HIGH); // enable sending
sendMsg (fWrite, msg, sizeof msg);
digitalWrite (ENABLE_PIN, LOW); // disable sending
analogWrite (12, buf [2]);
Serial.println(buf[2]);// set light level
} // end if something received
} // end of loop
I want to blink LED whenever I got the signal. but master is sending message continuously. i want to stop when I got the message initially. Please help me