Author Topic: Interrupt , delay() and Confusion  (Read 1236 times)

Mahimna

  • Newbie
  • *
  • Posts: 16
Interrupt , delay() and Confusion
« on: March 21, 2018, 11:06:11 pm »
Hello Guys,

I am in the process of learning programming and its know hows. I have started a project where according to the room size I want to change the working of LED- simple, if loop is working perfectly there. Whenever I am pressing a button I want to have a different application for limited time and back to the routine where it is paused- again simple, interrupt seems the answer. As I found out , No It is complex. I want to flash an LED whenever that button is pressed for say 20 times, but it is not doing anything at all when tested on hardware. Google told me that you can not use delay() in interrupt routine and now with my knowledge base I am clueless how to pull this application off. So please enlighten me; I'LL REALLY APPRECIATE YOUR HELP.
I am attaching the code below, please guide me with any suggestions even for programming practices or better alternatives and of-course the main issue of serving the delay purpose after sensing the interrupt and resuming the previous work.

Code: [Select]
//ALL THE DELAYS USED ARE REDUCED VERSIONS FOR TESTING, IN REAL APPLICATION I WANT THEM AROUND 45-60 MINUTES

int i_roomSize = 0; //slide button with two states- LOW or HIGH
int i_boostButton = 2; // momentory release (push) button- ON(till manually pressed hold) or OFF(when released)

void setup() {
  // put your setup code here, to run once:
pinMode(1, OUTPUT); // LED output
pinMode(i_roomSize, INPUT);
pinMode(i_boostButton, INPUT);

//attachInterrupt(i_boostButton, boostedOutput, RISING);

}

void loop() {
  // put your main code here, to run repeatedly:

    if(digitalRead(i_roomSize) == HIGH){
    digitalWrite(1, HIGH); //LED is permenantly ON(Glowing)
    }
 
    if(digitalRead(i_roomSize) == LOW){
    digitalWrite(1, HIGH);
    delay(900);
                          //LED is Blinking
    digitalWrite(1, LOW);
    delay(900);
    }
}

/*
void boostedOutput() {  //interupt for when boost button is pressed
int i = 0;
  for(i=0; i<20 ; i++){
    digitalWrite(1, HIGH);
    delay(150);
                        // LED Blinking Rapidly
    digitalWrite(1, LOW);
    delay(150);
    }
}
*/

MrCookie

  • Newbie
  • *
  • Posts: 21
Re: Interrupt , delay() and Confusion
« Reply #1 on: April 30, 2018, 04:52:05 am »
You can use a interrupt to add roomsize when button is released after being pressed.
Then in your main setup loop, have a delay for 1 second.
Set the led ON, if the roomsize is greater than 0.
Then, as the last thing in the loop. Reduce the roomsize by one.

Make sense?
« Last Edit: April 30, 2018, 11:52:15 am by MrCookie »