Using a Millis example with RCswitch

I used a basic example which worked with a tactile button I had but I’m trying to get it working with a 433mhz remote i have and i’m not getting any success.

Original example code:

//Global Variables
const byte BUTTON=2; // our button pin
const byte LED=13; // LED (built-in on Uno)
 
unsigned long buttonPushedMillis; // when button was released
unsigned long ledTurnedOnAt; // when led was turned on
unsigned long turnOnDelay = 2500; // wait to turn on LED
unsigned long turnOffDelay = 5000; // turn off LED after this time
bool ledReady = false; // flag for when button is let go
bool ledState = false; // for LED is on or not.
 
void setup() {
 pinMode(BUTTON, INPUT_PULLUP);
 pinMode(LED, OUTPUT);
 digitalWrite(LED, LOW);
}
 
void loop() {
 // get the time at the start of this loop()
 unsigned long currentMillis = millis(); 
 
 // check the button
 if (digitalRead(BUTTON) == LOW) {
  // update the time when button was pushed
  buttonPushedMillis = currentMillis;
  ledReady = true;
 }
  
 // make sure this code isn't checked until after button has been let go
 if (ledReady) {
   //this is typical millis code here:
   if ((unsigned long)(currentMillis - buttonPushedMillis) >= turnOnDelay) {
     // okay, enough time has passed since the button was let go.
     digitalWrite(LED, HIGH);
     // setup our next "state"
     ledState = true;
     // save when the LED turned on
     ledTurnedOnAt = currentMillis;
     // wait for next button press
     ledReady = false;
   }
 }
  
 // see if we are watching for the time to turn off LED
 if (ledState) {
   // okay, led on, check for now long
   if ((unsigned long)(currentMillis - ledTurnedOnAt) >= turnOffDelay) {
     ledState = false;
     digitalWrite(LED, LOW);
   }
 }
}

Then My modified code with the RCswitch which just removes the button and replaces it with myswitch.getReceivedValue. :

//Global Variables
#include <RCSwitch.h> //new line I've added
const byte LED=13; // LED (built-in on Uno)
 
unsigned long buttonPushedMillis; // when button was released
unsigned long ledTurnedOnAt; // when led was turned on
unsigned long turnOnDelay = 2500; // wait to turn on LED
unsigned long turnOffDelay = 5000; // turn off LED after this time
bool ledReady = false; // flag for when button is let go
bool ledState = false; // for LED is on or not.
 
void setup() {
mySwitch.enableReceive(0); //new line I've added
 pinMode(LED, OUTPUT);
 digitalWrite(LED, LOW);
}
 
void loop() {
 // get the time at the start of this loop()
 unsigned long currentMillis = millis(); 
 
 // check the button
if (mySwitch.getReceivedValue() == XXXXXXX) {
  // XXXXXXX = 7 digit RCswitch button code, new line I've added
  buttonPushedMillis = currentMillis;
  ledReady = true;
 }
  
 // make sure this code isn't checked until after button has been let go
 if (ledReady) {
   //this is typical millis code here:
   if ((unsigned long)(currentMillis - buttonPushedMillis) >= turnOnDelay) {
     // okay, enough time has passed since the button was let go.
     digitalWrite(LED, HIGH);
     // setup our next "state"
     ledState = true;
     // save when the LED turned on
     ledTurnedOnAt = currentMillis;
     // wait for next button press
     ledReady = false;
   }
 }
  
 // see if we are watching for the time to turn off LED
 if (ledState) {
   // okay, led on, check for now long
   if ((unsigned long)(currentMillis - ledTurnedOnAt) >= turnOffDelay) {
     ledState = false;
     digitalWrite(LED, LOW);
   }
 }
}

If i take out a few lines I can get it to turn on with the remote and a delay but then it won’t turn off so i know the rc switch is detecting my remote press, just trying to figure out why it won’t turn off with a delay.

What happens if you first press the button that gives XXXXXXX and next another button. If the library that you use simply remembers the last button, you will always keep on updating buttonPushedMillis till another button is pressed.

You can debug with a Serial.println to see if mySwitch.getReceivedValue() changes.

 // check the button
if (mySwitch.getReceivedValue() == XXXXXXX) {
  // XXXXXXX = 7 digit RCswitch button code, new line I've added
  buttonPushedMillis = currentMillis;
  ledReady = true;
 }
Serial.println(mySwitch.getReceivedValue());

If that keeps on printing your XXXXXX, you know. Don't forget to add a Serial.begin() in setup().

Do you have a link for the RCswitch library?

interesting. it does keep on repeating the RCswitch code over and over until another button is pushed.

is the library. Do you know of a way to stop it from repeating?

I did not dig through the library but both the ReceivedDemo_Simple and the ReceivedDemo_Advanced examples contain the line

    mySwitch.resetAvailable();

Look at the examples how it is used and implement in your code; I suspect that that will solve the problem. If not, I can dig through the library.

A possible work around if 'resetAvailable()' does not work is to remember the last button and only react if 'getReceivedValue()' returns a different value. But that is a work-around.

That did the trick. Thank you!