Hi everyone.
I'm still quite new to Arduino programming, but I'm having a go at some code for transmitting (and receiving) an LED on / off.
I sort of have it working, but its not quite right.
What I am trying to achieve is:
On the transmitter side, when the switch is on - transmit the switch state
On the receiver side, light up an LED to reflect the (TX) switch state.
(switch on = LED on, switch off = LED off)
So far, I can transmit the switch state, I have the LED (pin13) on the TX lighting up to indicate the switch state.
And the receiver is lighting up the LED.
However the switch is acting like a toggle switch.
On - off = LED on
On - off = LED off
I think I have run out of understanding on how to constantly display the TX state.
The current code is:
TX:
#include <RH_ASK.h>
#include <SPI.h>
const int switch_in = 3;
const int ledPin = 13;
int state = 0;
char *msg;
RH_ASK driver;
void setup()
{
driver.init();
pinMode(switch_in, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop()
{
if (digitalRead(switch_in) == HIGH && state == 1) {
digitalWrite(ledPin, HIGH);
msg = "Switch";
driver.send((uint8_t *)msg, strlen(msg));
driver.waitPacketSent();
delay(200);
state = 0;
}
else if (digitalRead(switch_in) == LOW) {
digitalWrite(ledPin, LOW);
state = 1;
}
}
RX:
#include <RH_ASK.h>
#include <SPI.h>
RH_ASK driver;
const int ledPin = 13;
char receive[32];
int output_state = 0;
void setup()
{
driver.init();
pinMode(ledPin, OUTPUT);
}
void loop()
{
uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
uint8_t buflen = sizeof(buf);
if (driver.recv(buf, &buflen))
{
memset(receive, 0, sizeof(receive));
for (int i = 0; i < buflen; i++) {
receive[i] = buf[i];
}
if (strcmp(receive, "Switch" ) == 0) {
output_state = !output_state;
digitalWrite(ledPin, output_state);
}
if (strcmp(receive, "LOW") == 0) {
output_state = !output_state;
digitalWrite(ledPin, LOW);
}
}
}
Any advice would be much appreciated.
thanks
Jules