Go Down

Topic: Radiohead with multiple buttons and leds (Read 243 times) previous topic - next topic

MrMetro

Ok... So, my quest is simple.  2 arduinos, the receiver has 3 LED's.  The Transmitter has 2 buttons.  On the receiver, one LED is lit indicating a connection to the transmitter.  When button1 is pressed on the transmitter it makes an LED1 flash on the receiver.  When button2 is pressed on the transmitter it makes LED2 flash on the receiver. 

My issue seems to be with the receiver loop function, when one button is pressed, it sends a specific code, when the other button is code it sends another code.  The receiver isn't decoding correctly.   

Transmitter code:
Code: [Select]

#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 code:

Code: [Select]

#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);

    }
   }

Go Up