Trying not to use delay

So I’m trying to take everyone’s advice and not use delay in my sketchs but I’m having a hard time implementing it. I’m trying to add a half second of “delay” to the sketch below. I looked at the Blink Without Delay example sketch which got me this far but not far enough.

What do I need to do to insert a half second pause in my sketch?

Thanks
Rich

#include <IRLib.h>

const byte RECV_PIN = A4; //byte big enough

const byte LedPins[] = {13, 4, 5}; //byte is big enough
bool ledsState = LOW; //bool is big enough

unsigned long timer = millis();
const long interval = 500; 

IRrecv My_Receiver(RECV_PIN);

void setup()
{
  //Make all LedPins output
  for (byte i = 0; i < sizeof(LedPins); i++) { //LedPins is a byte so no need to devide the size
    pinMode(LedPins[i], OUTPUT);
  }

  My_Receiver.enableIRIn(); // Start the receiver
  Serial.begin(9600);
  Serial.print("Ready!");
}

void loop() {

  if(irSignals() == 0x95EB7802)  {  // Zero Button
    Serial.println('Button #0');
    toggleLeds();
  }
  timer = interval;
}

void toggleLeds() {
  ledsState = !ledsState; //toggle state
  for (byte i = 0; i < sizeof(LedPins); i++)
  {
    digitalWrite(LedPins[i], ledsState);
    Serial.println("Proof positive, the function is being called!");
  }
}

//unsinged long to hold the 32-bit ;)
unsigned long irSignals() {
  IRdecode My_Decoder;
  if (My_Receiver.GetResults(&My_Decoder)) {//Puts results in My_Decoder
    //decoders are only needed here
    IRdecodeHash My_Hash_Decoder;
  
    My_Hash_Decoder.copyBuf(&My_Decoder);//copy the results to the hash decoder
    My_Decoder.decode();
    Serial.print("real decode type:");
    Serial.print(Pnames(My_Decoder.decode_type));
    Serial.print(" value: 0x");
    Serial.println(My_Decoder.value, HEX);
    My_Hash_Decoder.decode();
    Serial.print("Hash decode: 0x");
    Serial.println(My_Hash_Decoder.hash, HEX); // Do something interesting with this value
    My_Receiver.resume(); // Turn on the IR receiver for more singnals
    return My_Hash_Decoder.hash; //Yes, new hash is:
    //WE exit NOW so next return is only executed when the if is not executed
  }
  return 0; //No new IR
}

I don’t think your quite understand the blink without delay example :wink: Nowhere in your code your comparing current time with previous time.

You can modify toggleLeds() to toggle every N seconds (shown below for 1 second)

/*
  toggleLeds(); non-blocking switching LEDs on and off for 1 second
*/
void toggleLeds()
{
  // variable to remember start time of delay
  static unsigned long starttime = 0;

  // if delay not started
  if (starttime == 0)
  {
    // toggle all LEDs
    ledsState = !ledsState; //toggle state
    for (byte i = 0; i < sizeof(LedPins); i++)
    {
      digitalWrite(LedPins[i], ledsState);
      Serial.println("Proof positive, the function is being called!");
    }
    // set the start time
    starttime = millis();
  }

  // if delay lapsed
  if(millis() - starttime > 1000)
  {
    // reset start time so next call will toggle the leds (again)
    starttime = 0;
  }
}

Note: I love to waste memory, hence the use of a static variable to remember the start time. You can use your ‘timer’ variable instead (initialize it to zero) but I find this a safer approach (there will never be a conflict between different delays that are executed simultaneously). Obviously I have to reconsider when I start running out of memory.

I now have a better understanding of blink without delay. Thanks so much!

You may find Several Things at a Time useful. It is an extended example of BWoD.

...R Edit to change to the correct link - apologies for any confusion.