Go Down

Topic: Adapting ButtonStateChange code (Read 340 times) previous topic - next topic

beperron

Greetings: 

I am new to Arduino and working through various examples with my Arduino board, with a Sparkfun Inventor's kit, although I am still a long way off from being an inventor!  Regarding my problem, I have successfully cmopleted the ButtonStateChange example on the Arduino site, with the various tutorials.  Here is a link to the full set of code:  http://arduino.cc/en/Tutorial/ButtonStateChange.  It is working exactly the way it should be.

Right now, when the button is pushed, the number of button pushes are being logged.  But, now I want to make a slight adaptation, and I am hoping somebody might be able to assist.  Here is my intermediary goal (I have a few other things to do subsequent to the following changes, but right now it is just working out this problem): 

1.  When the button is pushed, I would like the light to remain on for 15 minutes.  I am trying to work on some delay procedure to make this happen but I am not quite sure if this is the right approach.

2.  I want to continue recording the number of times the button is pushed, but I do not want any action to take place during the 15 minutes that the light is on.  In other words, if the button is pushed, it should record that it is pushed, and the light should remain on for 15 minutes.  However, if the button is pushed again while the light is on, I do not want that event recorded, and the light needs to remain on for the remainder of that original 15 minute interval.

3.  When the 15 minute interval is over, I would like the light to go off and have the system wait until the button is pushed again.

Here is the snippet of the loop code, and the full code can be found at the aforementioned link:

Code: [Select]

void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
    }
    else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off");
    }
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState;

 
  // turns on the LED every four button pushes by
  // checking the modulo of the button push counter.
  // the modulo function gives you the remainder of
  // the division of two numbers:
  if (buttonPushCounter % 4 == 0) {
    digitalWrite(ledPin, HIGH);
  } else {
   digitalWrite(ledPin, LOW);
  }
 
}
[\code]

Thanks in advance for your assistance.

Best,
Brian


Arrch


1.  When the button is pushed, I would like the light to remain on for 15 minutes.  I am trying to work on some delay procedure to make this happen but I am not quite sure if this is the right approach.

2.  I want to continue recording the number of times the button is pushed, but I do not want any action to take place during the 15 minutes that the light is on.  In other words, if the button is pushed, it should record that it is pushed, and the light should remain on for 15 minutes.  However, if the button is pushed again while the light is on, I do not want that event recorded, and the light needs to remain on for the remainder of that original 15 minute interval.

3.  When the 15 minute interval is over, I would like the light to go off and have the system wait until the button is pushed again.

Normally I would direct you to take a look at the Blink Without Delay example, but this is one of the few cases where using the delay() function isn't going to cause issues.  If you're serious about the microcontroller not doing ANYTHING AT ALL during those 15 minutes, then just put a delay() and pass whatever the millisecond value is for 15 minutes to it. If you need the microcontroller do to things during that 15 minutes, take a look at the aforementioned example.

guix

#2
Nov 15, 2012, 09:42 pm Last Edit: Nov 15, 2012, 09:45 pm by guix Reason: 1
Hello and welcome,

If I understand correctly, something like that ? :
Code: [Select]

loop()
{
 if ( button pushed )
 {
   if ( timer_enabled == false )
   {
     timer_enabled = true;
     increase counter
     turn on the led
     etc...
   }
 }

 if ( timer_enabled == true )
 {
   if ( 15 minutes elapsed )
   {
     timer_enabled = false;
     turn off the led
     etc
   }
 }
}

beperron

Guix and Arrch:

Thank you both for your very useful replies.  With your assistance, I was able to get my programming running exactly the way I envisioned.  Although it is a very basic program, it is my first real world application so I am pretty jazzed about seeing it come together!

Best,
Brian

Go Up