Using RF Receiver and Keyfob to Control 2 LEDs

Hi,

First time I am posting. I'm using an elegoo uno r3, Keyfob Single Button RF Remote Control, Simple RF T4 Receiver - 315MHz Toggle Type (both from adafruit).

What I want this to do is act like a cycle by using a counter. When the code is uploaded both LEDs are off. When I press the button the first time, the LED from pin 13 to be on and the LED from pin 12 to be off. The second time I press the button LED from pin 13 to be off and the LED from pin 12 to be on. The button presses will be counted and the third press will equal the first and start all over again. All of this with the use of a single button key fob.

When I upload my code both LEDs are off like I want. But the problem is when I press the button the first time, the LEDs alternate on and off continuously. The second button press turns both LEDs off.

I don't know if something in my code is wrong or if the RF Reciever is the wrong type (there are 3 types on adafruit) or perhaps the Keyfob I chose isn't right (there are 3 types on adafruit).

Any help would be greatly appreciated.

Below is my code:

const int buttonPin = 2;
const int ledPin1 = 13;
const int ledPin2 = 12;
int ledPin1_State = 0;
int ledPin2_State = 0;
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;

void setup() {
pinMode(buttonPin, INPUT);
digitalWrite(2,LOW);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);

}

void loop() {

buttonState = digitalRead(buttonPin);

if (buttonState != lastButtonState) {

if (buttonState == HIGH) {
  
  buttonPushCounter++;
}

delay(50);

}

if(buttonPushCounter == 3)buttonPushCounter = 1;
if (buttonPushCounter == 2) {
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, HIGH);
delay(1000);
}
if (buttonPushCounter == 1) {
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, LOW);
delay(1000);

}
while (digitalRead(buttonPin)==LOW){
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
}
}

Probably easier to perform your logic with a switch()-case block. with breaks.

Less words, and the flow is more obvious.

I will give that a try. Thanks. I will let you know if anything changes.

OK so I tried using switch case in my code and It did work better but not exactly the way I want it to.

Plugging in the arduino to the computer and uploading the code, both LEDs are off. When I press the RF Remote, the LED on pin 13 turns on while the LED on pin 12 is off. Everything is going as it should be but then for some reason I need to press the RF Remote twice to turn on the LED on pin 13 off and pin 12 on. I press twice again and the LED on pin 13 turns on while the LED on pin 12 is off. I have to keep clicking twice to turn on and off the LEDS. I only want to click once to turn on and off the LEDS.

Could something be wrong with my code? Or maybe the RF receiver and the RF controller are bad? Or that's how they work?

Below is my code:

const int buttonPin = 2;
const int ledPin1 = 13;
const int ledPin2 = 12;
boolean oldSwitchState = LOW;
boolean newSwitchState = LOW;
byte state = 0;

void setup() {

pinMode(ledPin1, OUTPUT);
digitalWrite(ledPin1, LOW);
pinMode(ledPin2, OUTPUT);
digitalWrite(ledPin2, LOW);

pinMode(buttonPin, INPUT);

}

void loop() {
newSwitchState = digitalRead(buttonPin);
if ( newSwitchState != oldSwitchState )
{
if (newSwitchState == HIGH )
{
state++;
if (state > 2) {
state = 1;
}
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);

  if (state == 1) {
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, LOW);
    
  }
  if (state == 2) {
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin1, LOW);
    
  }
}
oldSwitchState = newSwitchState;

}
}

Hi,

I didn't know if by replying to my last post you would be able to see it but I do have another question below.

Hi,

I'm using an elegoo uno r3, Keyfob Single Button RF Remote Control, Simple RF T4 Receiver - 315MHz Toggle Type (both from adafruit).

This is what I want the my project to do: When the code is uploaded both LEDs are off. When I press the button the first time, the LED from pin 13 to be on and the LED from pin 12 to be off. The second time I press the button LED from pin 13 to be off and the LED from pin 12 to be on. The next button press will be like the first and continue indefinitely. All of this with the use of a single button key fob.

When I plug in the arduino to the computer and upload the code, both LEDs are off. When I press the RF Remote, the LED on pin 13 turns on while the LED on pin 12 is off. Everything is going as it should be but then for some reason I need to press the RF Remote twice to turn on the LED on pin 13 off and pin 12 on. I press twice again and the LED on pin 13 turns on while the LED on pin 12 is off. I have to keep clicking twice to turn on and off the LEDS. I only want to click once to turn on and off the LEDS.

Could something be wrong with my code? Or maybe the RF receiver and the RF controller are bad? Or that's how they work? Any help would be greatly appreciated. Thanks.

Below is my code:

const int buttonPin = 2;
const int ledPin1 = 13;
const int ledPin2 = 12;
boolean oldSwitchState = LOW;
boolean newSwitchState = LOW;
byte state = 0;

void setup() {

pinMode(ledPin1, OUTPUT);
digitalWrite(ledPin1, LOW);
pinMode(ledPin2, OUTPUT);
digitalWrite(ledPin2, LOW);

pinMode(buttonPin, INPUT);

}

void loop() {
newSwitchState = digitalRead(buttonPin);
if ( newSwitchState != oldSwitchState )
{
if (newSwitchState == HIGH )
{
state++;
if (state > 2) {
state = 1;
}
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);

if (state == 1) {
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, LOW);

}
if (state == 2) {
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin1, LOW);

}
}
oldSwitchState = newSwitchState;
}
}

Please edit your post, select all code and click the </> button to apply so-called code tags and next save your post. It makes it easier to read, easier to copy and prevents the forum software from incorrect interpretation of the code.

I don't see much wrong with your code (but that does not mean everything). I siggest that you sprinkle some Serial.println statements at relevant places in your code to see what is happening; maybe there is some bouncing going on; for testing purposes, you can add a short delay (e.g. 50ms) at the beginning or end of loop().

I think that does those are redundant; state can only be 1 or 2 and you handle that in your if blocks.

button switches are typically wired between the pin and ground relying on the internal pullup resistor to pull the pin HIGH and the switch pulling it LOW when pressed.

the pin should be configured as INPUT_PULLUP and tested for LOW. adding a 10 msec delay would handle debounce

Hi,

I'm using an Arduino Mega, Touchscreen, Keyfob Single Button RF Remote Control, Simple RF T4 Receiver - 315MHz Toggle Type (both from adafruit).

This is what I want the my project to do: When the code is uploaded both LEDs are off. When I press the button the first time, the LED from pin 52 to be on and the LED from pin 53 to be off. The second time I press the button LED from pin 53 to be off and the LED from pin 52 to be on. The next button press will be like the first and continue indefinitely. This will be done with the touchscreen and the RF Receiver/RF controller interchangeably.

What happens instead is (it doesn't matter which one I start with) it works like how I said above but once I switch over to either the touchscreen or the RF Reciever/RF controller I have to press two times in order for the LEDs to switch.

The codes for both work fine separately but when I put them together is where that issue happens.

Any help would be greatly appreciated.

#include <LCDWIKI_GUI.h> 
#include <LCDWIKI_KBV.h> 
#include <Adafruit_GFX.h>  
#include <Adafruit_TFTLCD.h> 
#include <TouchScreen.h> 
#include <MCUFRIEND_kbv.h>
int LFT = 115, RT = 908, TOP = 964, BOT = 80; 
LCDWIKI_KBV mylcd(ILI9486, A3, A2, A1, A0, A4); 
#define BLACK       0x0000
#define NAVY        0x000F
#define DARKGREEN   0x03E0
#define DARKCYAN    0x03EF
#define MAROON      0x7800
#define PURPLE      0x780F
#define OLIVE       0x7BE0
#define LIGHTGREY   0xC618
#define DARKGREY    0x7BEF
#define BLUE        0x001F
#define GREEN       0x07E0
#define CYAN        0x07FF
#define RED         0xF800
#define MAGENTA     0xF81F
#define YELLOW      0xFFE0
#define WHITE       0xFFFF
#define ORANGE      0xFD20
#define GREENYELLOW 0xAFE5
#define PINK        0xF81F
#define YP A3 
#define XM A2 
#define YM 9   
#define XP 8   
MCUFRIEND_kbv tft;
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
#include <Servo.h>
Servo myservo1;
const int  buttonPin = 13;    
const int buttonPin1 = 42;
const int servoPin1 = 35;
const int ledPin1 = 52;       
const int ledPin2 = 53;
boolean oldSwitchState = LOW;
boolean newSwitchState = LOW;
byte state = 0;
int buttonPushCounter = 0;   
int buttonState = 0;        
int lastButtonState = 0;    

void setup() {
  pinMode(buttonPin, OUTPUT);
  pinMode(buttonPin1, INPUT_PULLUP);
  digitalWrite(13, LOW);
  pinMode(ledPin1, OUTPUT);
  digitalWrite(ledPin1, LOW);
  pinMode(ledPin2, OUTPUT);
  digitalWrite(ledPin2, LOW);
  Serial.begin(9600);
  Serial1.begin(9600);

  tft.begin(0x9486);
  Serial.print("TFT size is "); Serial.print(tft.width()); Serial.print("x"); Serial.println(tft.height());
  tft.setRotation(2);
  tft.fillScreen(WHITE);
  tft.fillCircle(160, 240, 60, 0x7BEF);
}
void loop() {
  touchScreenLoopCode();
  rFReceiverCode();
}
void touchScreenLoopCode(void) {
  int button_on = 0;
  int x, y;
  TSPoint p = ts.getPoint();
  if (p.z > ts.pressureThreshhold) {
    //Portrait Calibration
    x = map(p.x, LFT = 115, RT = 908, 0, 320);
    y = map(p.y, TOP = 964, BOT = 80, 0, 480);

    if (x > 100 && x < 220 && y > 180 && y < 300 ) {

      digitalWrite(13, HIGH);
      buttonState = digitalRead(buttonPin);

      if (buttonState != lastButtonState) {
        
        if (buttonState == HIGH) {
         
          buttonPushCounter++;
        }
        delay(50);
      }
     
      if (buttonPushCounter == 3)buttonPushCounter = 1;
      if (buttonPushCounter == 2) {
        digitalWrite(ledPin1, LOW);
        digitalWrite(ledPin2, HIGH);
      }
      if (buttonPushCounter == 1) {
        digitalWrite(ledPin1, HIGH);
        digitalWrite(ledPin2, LOW);
      }
      while (digitalRead(buttonPin) == LOW) {
        digitalWrite(ledPin1, LOW);
        digitalWrite(ledPin2, LOW);
      }
    }
  }
}
void rFReceiverCode() {
  newSwitchState = digitalRead(buttonPin1);
  if ( newSwitchState != oldSwitchState )
  {
    if (newSwitchState == HIGH )
      delay(50);
      state++;
    {
      if (state > 2) {
        state = 1;
      }
      digitalWrite(ledPin1, LOW);
      digitalWrite(ledPin2, LOW);

       if (state == 1) {
        digitalWrite(ledPin1, HIGH);
        digitalWrite(ledPin2, LOW);
      }
      if (state == 2) {
        digitalWrite(ledPin2, HIGH);
        digitalWrite(ledPin1, LOW);
      }
    }
    oldSwitchState = newSwitchState;
  }
}

I didn't dive into the details, but I'm wondered why your buttonPin configured as OUTPUT ?

It was a silly mistake. With that in mind, I did change it to INPUT. It still does the same thing. have to click twice when changing from touchscreen and rf receiver (and vice versa) for the LEDs to switch.