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