Pages: [1]   Go Down
Author Topic: LED on for x time and then wait for x time  (Read 181 times)
0 Members and 1 Guest are viewing this topic.
Hilversum
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What I got now, is that my LED will go on when the variable buttonPushCounter is higher than or equal to 100. That's oke.
But... I want to MAke the LED go on for X time for example 3 seconds.. and then wait 10 seconds before the LED could turn on when buttonPushCounter is higher than or equal to 100.

I hope someone can help me.

Code:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 5;       // the pin that the LED is attached to

long decreaseCounter = 0;
long wachttijd = 1000;

int buttonPushCounter = 0;   // buttonPressedcounter
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}

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:
      
      if (buttonPushCounter >=0 && buttonPushCounter <100) buttonPushCounter = buttonPushCounter +10;
      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 when counter is higher or equal to 100
  if (buttonPushCounter >=100) {
    digitalWrite(ledPin, LOW);
  } else {
   digitalWrite(ledPin, HIGH);
  }
  
//every 1000 miliseconds, counter will decrease with the amount of 5 untill the amount is 0.
  if (millis() > decreaseCounter)
  {
    decreaseCounter = millis() + wachttijd;
    if (buttonPushCounter >0 && buttonPushCounter <=105) buttonPushCounter = buttonPushCounter - 5;
  }
}
« Last Edit: November 20, 2012, 04:27:43 pm by RtW_nl » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46026
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
Then, why don't the names both reflect that? currState and prevState imply that they go together. Your names fo not.

Code:
      if (buttonPushCounter >=0 && buttonPushCounter <100) buttonPushCounter = buttonPushCounter +10;
As opposed to counting by 1 and limiting the range to 10?

Code:
But... I want to MAke the LED go on for X time for example 3 seconds.. and then wait 10 seconds before the LED could turn on when buttonPushCounter is higher than or equal to 100.
You want to turn the LED on for 3 seconds, and then wait 10 seconds. Won't that result in the LED being on for 13 seconds?

Your primary difficulty appears to be in defining your requirements. I think that you'll find that writing the code when you know what it needs to do will be relatively simple. Writing the code without really understanding what it is to do will be nearly impossible.
Logged

Hilversum
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
// compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
I ain't got any problem with this code.. this code just checks the state of the button

Code:
if (buttonPushCounter >=0 && buttonPushCounter <100) buttonPushCounter = buttonPushCounter +10;
it can be 1 to 10, but I wanted to make the code easier for the amount of fire 0 to 100% and decreasing the time with 5 percent every second.
0 to 10 or 1 to 100 in this case is both the same indeed

Code:
But... I want to MAke the LED go on for X time for example 3 seconds.. and then wait 10 seconds before the LED could turn on when buttonPushCounter is higher than or equal to 100.
Maybe if you read my explaination for the use of my installation you know what i mean with the 3 and 10 seconds..
I mean giving a pulse for 3 seconds (LED of for 3 seconds) and after stop the option to giving a pulse for 10 seconds (after 3 seconds the LED can't be switchen on for 10 seconds). So the room won't be an invasion of smoke.

And I made the code myself so I fully understand it.
« Last Edit: November 20, 2012, 08:26:29 am by RtW_nl » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46026
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So, you want to turn the LED on for 3 seconds, and the not allow it to be turned on for another 10 seconds, regardless of how high the counter gets?

That's easy enough. You need to keep track of when you turn it on, and when you turn it off. Before turning it on, check that it has been off long enough, if the off time is not 0.
Logged

Hilversum
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Oké thanks, difficult enough for me to code that! Hahaha.
but I got in my head what to do now and try to fix it.
Thanks for the help so far
Logged

Pages: [1]   Go Up
Jump to: