I have 2 arduino's connected via RF. There are 2 buttons on one and 3 LED's on the other. One LED stays on verifying connection. When button1 is pushed LED2 stays on, when button2 is pushed LED3 flashes.
I have this broken down as such, when button1 is pushed, it sends a 6 digit code to the LED arduino. When button 2 is pushed it sends another code to the LED arduino. I can see the codes in the serial monitor.
I am trying to put together a garage door monitor (dang kid leaves it up). I want to know I have a connection to the RF unit, when it is up (button1) or when it is down (button2). The other applications for this code would be amazing and I'd love to share. I have searched all over the googles and youtubes but they mostly all use virtual wire or don't use multiple buttons to send codes for specific pins. I found one example out there that seemed close to what I'm trying to achieve but it was in .hex and I couldn't get it to open in visual studio.
Transmitter:
#include <RH_ASK.h>
#include <SPI.h> // Not actually used but needed to compile
// tx/rx configuration
const int txSpeed = 2000;
const int rxPin = 5;
const int txPin = 6;
const int pttPin = 7;
const int buttonPin1 = 2; // the number of the pushbutton pin
const int buttonPin2 = 3; // the number of the pushbutton pin
int buttonState = 0; // variable for reading the pushbutton status
RH_ASK driver(txSpeed, rxPin, txPin, pttPin);
void setup()
{
if (!driver.init())
Serial.println("rf driver init failed");
pinMode(LED_BUILTIN, OUTPUT);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
}
void loop()
{
buttonState = digitalRead(buttonPin1);
if (buttonState == HIGH)
{ // turn LED on:
const char *msg1 = "349516";
driver.send((uint8_t *)msg1, strlen(msg1));
driver.waitPacketSent();
delay(20);
}
buttonState = digitalRead(buttonPin2);
if (buttonState == HIGH)
{ // turn LED on:
const char *msg2 = "654321";
driver.send((uint8_t *)msg2, strlen(msg2));
driver.waitPacketSent();
delay(20);
}}
Receiver:
#include <RH_ASK.h>
#include <SPI.h> // Not actualy used but needed to compile
// tx/rx configuration
const int txSpeed = 2000;
const int rxPin = 5;
const int txPin = 6;
const int pttPin = 7;
int ledPin3 = 3;
int ledPin4 = 4;
int ledPin5 = 8;
int val1 = 349516;
int val2 = 135790;
RH_ASK driver(txSpeed, rxPin, txPin, pttPin);
void setup()
{
Serial.begin(9600); // Debugging only
if (!driver.init())
Serial.println("init failed");
pinMode(ledPin4, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin5, OUTPUT);
}
void loop()
{
uint8_t buf[RH_ASK_MAX_MESSAGE_LEN] = {0};
uint8_t buflen = sizeof(buf);
if (driver.recv(buf, &buflen)) {// if message received, save it
for(int i=0;i<sizeof(buf);i++) {
Serial.println((char*)buf); // print received message, used for debugging.
}
if(buf[0] != 654321){ // and if the first letter in message array is X
digitalWrite(ledPin4,HIGH);
delay(500);
digitalWrite(ledPin4,LOW);
delay(500);
}
if(buf[0] != 349516){ // and if the first letter in message array is X
digitalWrite(ledPin3,HIGH);
delay(500);
digitalWrite(ledPin3,LOW);
delay(500);
}
}
else if(buf[0]!= 000000){
digitalWrite(ledPin5,HIGH);
}
}
There seems to be an issue with the loop. I believe don't have the correct code here: if(buf[0] != 654321
What really happens, is the loop happens no matter which code is received: 654321 or 349516.