Hello All,
This isn't a request for help, just how it took 2 days to figure out a simple and stupid mistake.
Using 2 XRF modules i wanted to control AT328's pin states, sending 2 bytes, pin number and state on|off
using this line if (Serial.available()>0)
the led was switching on regardless of what bytes it received because it didn't wait for the 2nd byte
So the answer was if (Serial.available()>1)
which does wait for 2 bytes before doing anything, and thats what took 2 days to figure out, Dumb huh?
The result is now by sending pin number and state i can control any pin on AT328, eventually relays,
I'm open to surgestions for improvements, like should i send 3 bytes, the first being a command byte?
TX Code - Turn on pin 13
Serial.println("P13"); // debug
Serial1.print(13,BYTE); // Send Pin Number
Serial1.print(HIGH,BYTE);// Send Pin state ON
TX Code - Turn off pin 13
Serial.println("p13"); // debug
Serial1.print(13,BYTE); // Send Pin Number
Serial1.print(LOW,BYTE); // Send Pin state OFF
RX Code
int cPin; // Control Pin
int state; // Pin State, on | off
void setup()
{
Serial.begin(9600); // Start Serial
pinMode(13,OUTPUT); // Set Pin 13 led OUTPUT
}
void loop()
{
if (Serial.available()>1) // wait for 2 bytes
{
cPin = Serial.read(); // First Byte is Pin Number
state = Serial.read(); // 2nd Byte is Pin state on | off
digitalWrite(cPin, state); // Do it
}
}