Unless button is pressed within five seconds of detection, alarm will go off...

Hello! I am stuck on how to approach a certain problem. Summed up, I have an ultrasonic sensor that is going to trigger about 5 seconds of light flashing when someone walks close to it. If the button isn't pressed withing five seconds of activation, the alarm will go off. However if the button is pressed, I want the code to go back to the ultrasonic sensor just waiting.

The thing I don't know how to do is get the code LOOKING for a button press WHILE the light is flashing for approx 5 secs.

Any help on how to approach this will be greatly appreciated.

The code attached is just the code WITHOUT any button press, just the ultrasonic and light flashing working together.

RFID_ALARM.ino (1.27 KB)

By the way I haven't implemented an alarm yet obviously, I first need to work out how to use the button with the flash timer.

Two approaches you could take would be to forgo the blink function as you have it. Delays are always a pain in these kinds of designs. Just keep a timer in your while loop and flip a bool at state change for the led, then read the button pin.

void loop() {
  int buttonstate = digitalRead(button);
  int distance = sonar.ping_cm();

  if(distance < 20 && buttonstate == LOW){
    if(inc == 100) {
      inc = 0;
      if(led_state) {
        led_state = true;
        digitalWrite(led,HIGH);
      } else {
        led_state = false;
        digitalWrite(led,LOW);
      }
    }
  }else if (led_state = false){
    digitalWrite(led,HIGH);
  }else if (buttonstate == HIGH){
    delay(10000);
  }
}

If you want to keep the delay you can jump into the interrupt rabbit hole, lots of good reading on it!

-Edit- Completed it more and fixed a boo-boo. Also check the state of the button whether it is HIGH default or LOW.

Thank you so much! I haven't tested it in the actual code yet, but I think I'll need to do some quick research on all of the little sub loops, booleans, and all of that jazz to truly understand what's going on (I'm still just an ambitious newbie). I really appreciate the help, so thank you! I'll see how it goes soon.

It might not work, don't have the hardware hooked up here. But I can run you through it.

bool is basically just int but limited to 0 - false or 1 - true. Very nice for if statements!

I'm not quite sure how HIGH and LOW are defined within the arduino IDE so going with the standard way of doing it I see everywhere.

bool led_state = true; //By default  you seem to want the led on.

void loop() {
  int buttonstate = digitalRead(button); // Read the button with every every loop.
  int distance = sonar.ping_cm(); // Read the sonar.

  if(distance < 20 && buttonstate == LOW){ //If the sensor is high and the button isn't pressed (High/Low vary on how you wire it) start counting.
    inc = inc + 1; // Oops forgot this in original!
    if(inc == 100) { //Every hundredth count flip the led and start counter over
      inc = 0;
      if(led_state == false) { //Made these to more easier to understand, but the comparison (led_state == false) in itself is either true or false. So the original one is functionally equivalent.
        led_state = true;
        digitalWrite(led,HIGH);
      } else if(led_state == true) {
        led_state = false;
        digitalWrite(led,LOW);
      }
    }
  }else if (led_state = false){ // If nobody in sensor range reset the led to default state.
    digitalWrite(led,HIGH);
  }else if (buttonstate == HIGH){ // If the button is pressed you get 10 seconds to get out of range.
    delay(10000);
  }
}

Forgot Arduino had this capability millis() it will give you more control over the timing. I normally don't much programming!

Joshua10902:
Any help on how to approach this will be greatly appreciated.

See the sticky at the top of this forum.

dougp:
See the sticky at the top of this forum.

Making newbies dive into the deep is not always the best approach. I know to you and I this is simple, but it's a mindset you need to get into. Telling them their problems are trivial and can be easily solved with generic steps also does not encourage those needing help. Stop being an elitist.

Dirkhens:
Making newbies dive into the deep is not always the best approach. I know to you and I this is simple, but it's a mindset you need to get into. Telling them their problems are trivial and can be easily solved with generic steps also does not encourage those needing help. Stop being an elitist.

I did not tell anyone to do anything. If the thread originator chooses to ignore being pointed to a resource so be it. I use the 'unsubscribe' feature freely if it's obvious I'm not making a contribution to a thread.

Dirkhens:
I know to you and I this is simple, but it's a mindset you need to get into.

It is not simple to me. My avatar was chosen carefully. There are many, many threads here where I read two or three replies and give up because it's over my head. Certain of the basics I do have a grasp of and it's not because it was spoon fed to me, I tried, failed, read, changed something, recompiled, ad nauseum.

Dirkhens:
...it's a mindset you need to get into.

Thanks for the tip.

Dirkhens:
Telling them their problems are trivial and can be easily solved with generic steps also does not encourage those needing help.

I did no such thing. If an intelligent discussion is to be had don't you think the participants should at least have *some * common ground?

Dirkhens:
Stop being an elitist.

That's a surprising first! Considering some of the snarky, demeaning, demanding replies I've seen by others I think I'm pretty mild.

correction: My knowledge of PROGMEM was pretty much spoon fed to me.

Easiest is millis() timing. What you want is a state machine.
If proximity detected flash LED.
If button pressed, stop flashing, time out the proximity testing for 10 seconds.
If after 5 seconds of flashing no button press, alarm.

void loop() {

  if (haveProximity() && flashing == false && millis() - buttonPressTime > 10000) {
    flashing = true;
    lastFlash = millis();
    proximityDetected = millis();
  }

  // LED flashing, 500 ms on and off.
  if (flashing) {
    if (millis() - lastFlash > 500) {
      lastFlash += 500;
      ledState = !ledState;
      digitalWrite(LEDPIN, ledState);
    }
  }

  // 5000 ms wait for the alarm.
  if (flashing && millis() - proximityDetected > 5000) {
    soundAlarm();
  }

  if (digitalRead(BUTTONPIN) == LOW) { // Usually buttons are active low
    flashing = false;
    digitalWrite(LEDPIN, LOW);
    buttonPressedTime = millis();
  }
}

bool haveProximity() {
  // Use sensor to detect proximity, return true or false.
}

void soundAlarm() {
  // Make a lot of noise or so.
}