Pages: 1 [2]   Go Down
Author Topic: Button Counter with Reset Timer  (Read 2487 times)
0 Members and 1 Guest are viewing this topic.
Hillsboro, OR
Offline Offline
Jr. Member
**
Karma: 1
Posts: 73
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

stupid name fixed. tried
Quote
Code:
  if(millis() - eventTIme > interval)

Still isn't working. Anyway, here is all my code

Code:
// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int redled = 7;
const int greenled = 12;
const int yellowled = 11;    // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button
int time = millis();
long eventTime = 0;

void setup()
{
pinMode(buttonPin, INPUT);   // initialize the button pin as a input
pinMode(redled, OUTPUT);
pinMode(greenled, OUTPUT);
pinMode(yellowled, OUTPUT);
Serial.begin(9600);          // initialize serial communication:
}

void loop()

{

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

 if(lastButtonState != buttonState)
 {
   if(buttonState == LOW)
   {
     delay(50);
   }
   else
   {
     delay(50);
     buttonPushCounter++;
     delay(50);
   }
 }
 if(millis() - eventTime > 15000)
 {
   buttonPushCounter=0;
   eventTime=millis();
   digitalWrite(redled, LOW);
   digitalWrite(greenled, LOW);
   digitalWrite(yellowled, LOW);
 }
 lastButtonState = buttonState;
 if(buttonPushCounter == 1)
 {
   digitalWrite(redled, HIGH);
   delay(50);
   eventTime=millis();
   delay(50);
 }
 else
 {
   digitalWrite(redled, LOW);
 }
 if(buttonPushCounter >1 && buttonPushCounter <5)
 {
   digitalWrite(greenled, HIGH);
 }
 else
 {
   digitalWrite(greenled, LOW);
 }
 if(buttonPushCounter == 5)
 {
   digitalWrite(yellowled, HIGH);
   delay(5000);
   buttonPushCounter=0;
 }
 else
 {
   digitalWrite(yellowled, LOW);
 }
 if(buttonPushCounter > 5)
 {
   buttonPushCounter=0;
 }
}
Logged

"Beer is proof that god loves us and wants us happy" - a popular misquote of Benjamin Franklin, but true nonetheless.

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@HazardsMind

I wouldn't bet on equality when comparing times.

PaulS already mentioned the usual code template:

Code:
if(millis() - eventTime > interval)

I'd avoid reinventing this particular wheel.
Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
if(buttonPushCounter == 1)
 {
   digitalWrite(redled, HIGH);
   delay(50);
   eventTime=millis();
   delay(50);
 }
 else
 {
   digitalWrite(redled, LOW);
 }

When buttonPushCounter is != 1, the red led is turned off, regardless of whether the timer has expired or not.
Logged

Hillsboro, OR
Offline Offline
Jr. Member
**
Karma: 1
Posts: 73
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@HazardsMind

Didn't see your reply there before my last post. Unfortunately, that didn't help either.

I am currently not using any debounce code, never thought about it. I will look into it later. My night at work is almost over and I'm too tired to keep staring at code.

@tuxduino

Quote
When buttonPushCounter is != 1, the red led is turned off, regardless of whether the timer has expired or not.

i assume what you're saying is that when the button counter is anything but 1, the red led is off. Yes, that is correct. the problem i am having is that when the button is pressed only once, the red led stays on all the time, and the timer (eventTime) does not reset it as it should (or at least as I would like)
Logged

"Beer is proof that god loves us and wants us happy" - a popular misquote of Benjamin Franklin, but true nonetheless.

Queens, New York
Offline Offline
Faraday Member
**
Karma: 99
Posts: 3618
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I dont know what problems your getting but, based on what you wrote and what I modified, it seems to work.

1st press: RED ON
2nd -4th press: GRN ON, RED OFF
5th press: GRN OFF, YEL ON then 5 second delay, counter back to zero, YEL OFF

Code:
// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int redled = 7;
const int greenled = 12;
const int yellowled = 11;    // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button
int time = millis();
long eventTime = 0;

void setup()
{
pinMode(buttonPin, INPUT);   // initialize the button pin as a input
pinMode(redled, OUTPUT);
pinMode(greenled, OUTPUT);
pinMode(yellowled, OUTPUT);
Serial.begin(9600);          // initialize serial communication:
}

void loop()

{

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

 if(buttonState != lastButtonState )
 {
   if(buttonState != LOW)
   {
     buttonPushCounter++;
   }
 }
 Serial.println(buttonPushCounter);
 lastButtonState = buttonState;
 if((millis() - eventTime / 1000) == 30)
 {
   buttonPushCounter=0;
   //eventTime=millis();
   digitalWrite(redled, LOW);
   digitalWrite(greenled, LOW);
   digitalWrite(yellowled, LOW);
 }
 if(buttonPushCounter == 1)
 {
   digitalWrite(redled, HIGH);
   delay(50);
   eventTime=millis();
   delay(50);
 }
 else
 {
   digitalWrite(redled, LOW);
 }
 if(buttonPushCounter >1 && buttonPushCounter <5)
 {
   digitalWrite(greenled, HIGH);
 }
 else
 {
   digitalWrite(greenled, LOW);
 }
 if(buttonPushCounter == 5)
 {
   digitalWrite(yellowled, HIGH);
   delay(5000);
   buttonPushCounter=0;
 }
 else
 {
   digitalWrite(yellowled, LOW);
 }
 if(buttonPushCounter > 5)
 {
   buttonPushCounter=0;
 }
}
 
« Last Edit: January 15, 2013, 08:57:03 am by HazardsMind » Logged

Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think what the OP is looking at is this other sequence:

press button once => red led on
wait 15s => red led should go off

But he sees this:

press button once => red led on
red led never goes off

And the reason is this:

Code:
if(buttonPushCounter == 1)
 {
   digitalWrite(redled, HIGH);
   delay(50);
   eventTime=millis();       <<<===== event time is continuously reinitialized
   delay(50);
 }

Until buttonPushCounter equals 1, millis() - eventTime will never be more than about 50ms
Logged

Hillsboro, OR
Offline Offline
Jr. Member
**
Karma: 1
Posts: 73
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@tuxduino, you are correct, that is the problem. everything else works fine, i just cant get the red led to shut off if it sits too long (currently set at 15 seconds).

I think what the OP is looking at is this other sequence:

press button once => red led on
wait 15s => red led should go off

But he sees this:

press button once => red led on
red led never goes off

And the reason is this:

Code:
if(buttonPushCounter == 1)
 {
   digitalWrite(redled, HIGH);
   delay(50);
   eventTime=millis();       <<<===== event time is continuously reinitialized
   delay(50);
 }

Until buttonPushCounter equals 1, millis() - eventTime will never be more than about 50ms

I kind of had a suspicion that is where the problem lies, but I really have no idea how to get past it.
Logged

"Beer is proof that god loves us and wants us happy" - a popular misquote of Benjamin Franklin, but true nonetheless.

Queens, New York
Offline Offline
Faraday Member
**
Karma: 99
Posts: 3618
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I fixed it. I also added a new function at the bottom.
Code:
// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int redled = 7;
const int greenled = 12;
const int yellowled = 11;    // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button
int time = millis();
long eventTime = 0;

void setup()
{
pinMode(buttonPin, INPUT);   // initialize the button pin as a input
pinMode(redled, OUTPUT);
pinMode(greenled, OUTPUT);
pinMode(yellowled, OUTPUT);
Serial.begin(9600);          // initialize serial communication:
}

void loop()

{

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

 if(buttonState != lastButtonState )
 {
   if(buttonState != LOW)
   {
     buttonPushCounter++;
     eventTime=millis();
     Time_Check(eventTime);
   }
 }
 //Serial.println(buttonPushCounter);
 lastButtonState = buttonState;
 
 if(buttonPushCounter == 1)
 {
   digitalWrite(redled, HIGH);
   Time_Check(eventTime);
 }
 else
 {
   digitalWrite(redled, LOW);
 }
 if(buttonPushCounter >1 && buttonPushCounter <5) // Question, is this really needed?
 {
   digitalWrite(greenled, HIGH);
   Time_Check(eventTime);
 }
 else
 {
   digitalWrite(greenled, LOW);
 }
 if(buttonPushCounter == 5)
 {
   digitalWrite(yellowled, HIGH);
   Time_Check(eventTime);
   //delay(5000); // I do not this is needed anymore
   buttonPushCounter=0; // same here
 }
 else
 {
   digitalWrite(yellowled, LOW);
 }
}

void Time_Check(long Time)
{ //Serial.print("Im here ");
 long Ntime = ((millis() - Time )/ 1000);
 //Serial.print(Ntime);
 //Serial.print("   ");
  if(Ntime == 10) // sets time to turn all off, DEFAULT 10 seconds
 {
   buttonPushCounter=0;
   digitalWrite(redled, LOW);
   digitalWrite(greenled, LOW);
   digitalWrite(yellowled, LOW);
 }
 return;
}
« Last Edit: January 29, 2013, 08:04:35 am by HazardsMind » Logged

Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

IMHO the proper way to fix it is to change the output (i.e. the led state) at _state transition_ time, rather than checking which state the program is in.
Kind of like triggering an interrupt based on pin change or pin level.

In this context "state transition" means a change in push button counter value.
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 99
Posts: 3618
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@tuxduino I agree, but I just wanted to fix his timing issue, I sure he can do the rest.
Logged

Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

My comment was not specifically directed at your code. It was just an idea that I had and that I wanted to put on the table.

IOW, the usual 2 cents smiley
Logged

Hillsboro, OR
Offline Offline
Jr. Member
**
Karma: 1
Posts: 73
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey guys, sorry it took me so long to get back to you. Had vacation and the wife wouldn't let me bring my toys. Anyway, HazardsMind, your code worked! Thank you!

Thanks to all you guys for your help.

In case you were interested, this is the beginning for a larger project that will incorporate a "useless machine" that I am going to use to encase a gift in. My brother=in-law and i have a gag wrapping competition each christmas and so we find creative ways to wrap each others gifts. I like to win.

So basically, the "button" here in the button counter will actually be the switch used by the useless machine. He will have ## seconds to hit the switch ## times, which will not only be activating the useless machine but lighting up the LED's as well. Once the final "Green LED" has been reached, his gift will be unlocked. If he doesn't do it fast enough, it resets and he has to start all over.

Anyway, thanks again!
« Last Edit: January 29, 2013, 07:09:26 am by cwhummel » Logged

"Beer is proof that god loves us and wants us happy" - a popular misquote of Benjamin Franklin, but true nonetheless.

Pages: 1 [2]   Go Up
Jump to: