I'm working on a little project with some cheap 433 MHz radio transmitters and receivers, so my code incorporates the RADIOHEAD library. My device behaves almost exactly the way I need, but not quite yet.
Here's the gist of what I'm trying to do: The whole system uses 2 transmitters and once receiver unit. On the receiving end, there are 2 LED's that correspond to the transmitters: the first for transmitter "X" and the second for transmitter "Y". There is also a soundboard wired in that can be activated by both transmitters. If the system works as intended, transmitter "X" will send the message "X" to the receiver when activated, causing LED 1 to blink and the soundboard to play a sound. Transmitter "Y" will send message "Y" when activated, causing LED 2 to blink and the soundboard to play a sound. Both LED's are to stay on prior to transmitter activation.
Currently the receiver unit can distinguish between the two signals, but the results are a little screwy. Message "X" works perfectly, but when receiving message "Y", the receiving unit causes both LED'S to blink in an out-of-phase-ish pattern (LED 1 blinks slightly later than LED 2). My knowledge with using RADIOHEAD is very limited, so what do you guys think is the problem? Here's the code:
#include <RH_ASK.h>
#include <SPI.h> // Not actualy used but needed to compile
RH_ASK driver(2000, 4, 8, 5);
int sound = 3;
int data = 4;
int light1 = 5;
int light2 = 6;
void setup()
{
pinMode(sound, OUTPUT);
pinMode(data, INPUT);
pinMode(lightA, OUTPUT);
pinMode(lightB, OUTPUT);
Serial.begin(9600); // Debugging only
if (!driver.init())
Serial.println("init failed");
}
void loop()
{
digitalWrite(light1, HIGH);
digitalWrite(light2, HIGH);
digitalWrite(sound, HIGH);
uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
uint8_t buflen = sizeof(buf);
if (driver.recv(buf, &buflen)) // Non-blocking
{
if (buf[0] == 'X')
{
digitalWrite(sound, LOW);
digitalWrite(light1, LOW);
delay(200);
digitalWrite(light1, HIGH);
delay(200);
}
if (buf[0] == 'Y')
{
digitalWrite(sound, LOW);
digitalWrite(light2, LOW);
delay(200);
digitalWrite(light2, HIGH);
delay(200);
}
}
}
Your program does not compile. There is confusion between light1 light2 and lightA lightB
Are you saying that with the 'X' transmitter powered off and not sending, the 'Y' transmitter will blink both LED's with the receiver code?
If so, please provide working code for the 'Y' transmitter and the receiver which compile and demonstrate the problem. Also, can provide a sketch of how the LEDs are wired to the output pins.
I've attached an image of the fritzing layout for the LED's and the receiver. Also, the code provided for the transmitters is exactly the same minus the message being sent. That's the only change. The code for the reciever is the exact same code in my first post.
#include <RH_ASK.h>
#include <SPI.h> // Not actually used but needed to compile
RH_ASK driver(2000, 4, 8, 5);
int trip = 4;
int LED = 13;
void setup()
{
pinMode(trip, INPUT);
pinMode(LED, OUTPUT);
Serial.begin(9600); // Debugging only
if (!driver.init())
Serial.println("init failed");
}
void loop()
{
if (digitalRead(trip) == HIGH)
{
const char *msg = "Y";
digitalWrite(LED, HIGH);
driver.send((uint8_t *)msg, strlen(msg));
driver.waitPacketSent();
}
if (digitalRead(trip) == LOW)
{
digitalWrite(LED, LOW);
}
}
The code for the receiver is the exact same code in my first post.
That code does not compile. You did not answer my question about LightA and LightB so I have changed them to light1 and light2.
Both Led's need to be wired in series with 220 ohm current limiting resistors. This is very important to avoid damage to the leds and possibly the arduino.
I believe that the issue with your sketch is the interaction between the ptt(push to talk pin) and the light1 led both on pin 5. Change the ptt to an unused pin. When I change your RX sketch as below and use the TX sketch you provided, I see the pin6 led blink and the pin5 led steady on.
#include <RH_ASK.h>
#include <SPI.h> // Not actualy used but needed to compile
//RH_ASK driver(2000, 4, 8, 5);
RH_ASK driver(2000, 4, 8, 7); //move ptt from pin 5
int sound = 3;
int data = 4;
int light1 = 5;
int light2 = 6;
void setup()
{
pinMode(sound, OUTPUT);
pinMode(data, INPUT);
pinMode(light1, OUTPUT);
pinMode(light2, OUTPUT);
Serial.begin(9600); // Debugging only
if (!driver.init())
Serial.println("init failed");
}
void loop()
{
digitalWrite(light1, HIGH);
digitalWrite(light2, HIGH);
digitalWrite(sound, HIGH);
uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
uint8_t buflen = sizeof(buf);
if (driver.recv(buf, &buflen)) // Non-blocking
{
if (buf[0] == 'X')
{
digitalWrite(sound, LOW);
digitalWrite(light1, LOW);
delay(200);
digitalWrite(light1, HIGH);
delay(200);
}
if (buf[0] == 'Y')
{
digitalWrite(sound, LOW);
digitalWrite(light2, LOW);
delay(200);
digitalWrite(light2, HIGH);
delay(200);
}
}
}
My apologies, it took me a second to understand what you meant by "doesn't compile". I must have made an accidental error typing out the light1 light2 stuff before posting. It compiled before I made this topic, so I got a tad lost.
I just uploaded your edits, too, and everything seems to be working out now! The PTT issue I think is what got it all fixed. Thank you!