Offline
Newbie
Karma: 0
Posts: 24
|
 |
« on: January 31, 2013, 03:16:16 pm » |
Hey, My project: Make a simple timer that starts at the click of a button. My sketch works, but it starts immediately after reset. I want the sketch to stop and wait for a button press. If anyone could help, i would be very grateful:) Here is my code: #include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#define ledPin 13 // LED connected to digital pin 13 #define buttonPin 8 // button on pin 4
int value = LOW; // previous value of the LED int buttonState; // variable to store button state int lastButtonState; // variable to store last button state //int blinking; // condition for blinking - timer is timing long interval = 100; // blink interval - change to suit long previousMillis = 0; // variable to store last time LED was updated long startTime ; // start time for stop watch long elapsedTime ; // elapsed time for stop watch int fractional; // variable used to store fractional part of time
void setup() { Serial.begin(9600); lcd.begin(16, 2); // Print a message to the LCD. lcd.print("Test");
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT); digitalWrite(buttonPin, HIGH);
}
void loop() { buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) { activate(); } else { digitalWrite(13, HIGH); }
}
void activate() { buttonState = digitalRead(buttonPin);
if (buttonPin == HIGH){ // if true then found a new button press while clock is not running - start the clock
startTime = millis(); // store the start time delay(5); // short delay to debounce switch lastButtonState = buttonState; // store buttonState in lastButtonState, to compare next time
}
else if (lastButtonState == HIGH){ // if true then found a new button press while clock is running - stop the clock and report
elapsedTime = millis(); //- startTime; // store elapsed time lastButtonState = buttonState; // store buttonState in lastButtonState, to compare next time
lcd.setCursor(0,1);
lcd.print(" "); // routine to report elapsed time lcd.print( (int)(elapsedTime / 1000L)); // divide by 1000 to convert to seconds - then cast to an int to print
lcd.print("."); // print decimal point
fractional = (int)(elapsedTime % 1000L);
if (fractional == 0) lcd.print("000"); else if (fractional < 10) lcd.print("00"); else if (fractional < 100) lcd.print("0");
lcd.println(fractional); // print fractional part of time
}
else{ lastButtonState = buttonState; // store buttonState in lastButtonState, to compare next time
|
|
|
|
|
Logged
|
|
|
|
|
Queens, New York
Offline
Edison Member
Karma: 29
Posts: 1574
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #1 on: January 31, 2013, 03:19:11 pm » |
Does your button have a pull down resistor? That is not the proper way to debounce a button, look at the debounce example. Also you never turn the LED off.
|
|
|
|
« Last Edit: January 31, 2013, 03:21:42 pm by HazardsMind »
|
Logged
|
UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 24
|
 |
« Reply #2 on: January 31, 2013, 03:30:47 pm » |
Yes it has a pulldown resistor.
The led part of the sketch is obsolete, and will be removed.
It might not be the right way to debounce, but the code in the activate() function works.
Its the part in the void loop() that wont stop the activate() function that is bothering me...
|
|
|
|
|
Logged
|
|
|
|
|
Queens, New York
Offline
Edison Member
Karma: 29
Posts: 1574
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #3 on: January 31, 2013, 03:38:08 pm » |
Well if it keeps going to the activate function, then it is seeing that the button is high, otherwise it will not do that if statement. Check your wiring again just to be sure.
|
|
|
|
|
Logged
|
UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 24
|
 |
« Reply #4 on: January 31, 2013, 03:53:25 pm » |
You where correct. The butten is HIGH when not pressed and LOW when pressed. I changed the code to: void loop() { buttonState = digitalRead(buttonPin);
if (buttonState == LOW) { activate(); } else { digitalWrite(13, HIGH); }
}
But it still wont stop the code from being run...
|
|
|
|
|
Logged
|
|
|
|
|
Lost Wages
Offline
Full Member
Karma: 11
Posts: 103
|
 |
« Reply #5 on: January 31, 2013, 04:07:09 pm » |
Did you change the button logic in activate() as well?
Please re-pot your entire code.
|
|
|
|
|
Logged
|
|
|
|
|
Queens, New York
Offline
Edison Member
Karma: 29
Posts: 1574
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #6 on: January 31, 2013, 04:10:33 pm » |
I noticed that your not using a latch. It is possible that if you press the button the first time, your not giving it time to go back HIGH, so it is carring over to the activate function.
|
|
|
|
|
Logged
|
UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 24
|
 |
« Reply #7 on: January 31, 2013, 04:22:36 pm » |
How can i implement a latch?
|
|
|
|
|
Logged
|
|
|
|
|
Queens, New York
Offline
Edison Member
Karma: 29
Posts: 1574
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #8 on: January 31, 2013, 04:31:48 pm » |
byte lastbuttonState = 0; // new global variable
void loop() { buttonState = digitalRead(buttonPin); // normal debounce code here
if(buttonState != lastbuttonState) { activate(); lastbuttonState = buttonState; } }
Alternative: byte lastbuttonState = 0; // new global variable byte buttonState2 = 0; void loop() {
buttonState1 = digitalRead(buttonPin); delay(20); buttonState2 = digitalRead(buttonPin);
if(buttonState1 == buttonState2) { if(buttonState2 != lastbuttonState) { activate(); lastbuttonState = buttonState2; } } }
dont forget the setup()
|
|
|
|
|
Logged
|
UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
UK
Offline
Tesla Member
Karma: 89
Posts: 6391
-
|
 |
« Reply #9 on: January 31, 2013, 04:32:16 pm » |
I want the sketch to stop and wait for a button press.
The simplest way to do that is to put a loop in setup() that reads the switch state repeatedly until the button press is detected.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 24
|
 |
« Reply #10 on: January 31, 2013, 04:41:14 pm » |
HazardsMind
I ill try that tomorrow, and i will let you know how it goes..
And thanks for all your help:)
|
|
|
|
|
Logged
|
|
|
|
|
Lost Wages
Offline
Full Member
Karma: 11
Posts: 103
|
 |
« Reply #11 on: January 31, 2013, 05:05:45 pm » |
Your new initialization code: byte lastbuttonState = 0; // new global variable
initializes to the button being pressed in the new scheme, yes? Please post your entire code...
|
|
|
|
|
Logged
|
|
|
|
|
Queens, New York
Offline
Edison Member
Karma: 29
Posts: 1574
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #12 on: January 31, 2013, 06:07:08 pm » |
Your new initialization code: byte lastbuttonState = 0; // new global variable
initializes to the button being pressed in the new scheme, yes? Please post your entire code... My code? It's just a snippet from his code that I made. Just replace his loop function with one of mine.
|
|
|
|
|
Logged
|
UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
Lost Wages
Offline
Full Member
Karma: 11
Posts: 103
|
 |
« Reply #13 on: January 31, 2013, 07:22:24 pm » |
No, I meant Webca.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 24
|
 |
« Reply #14 on: February 01, 2013, 05:51:19 am » |
I didnt get it to work  Here is my code: #include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#define ledPin 13 // LED connected to digital pin 13 #define buttonPin 9 // button on pin 4
int value = LOW; // previous value of the LED int buttonState; // variable to store button state int lastButtonState; // variable to store last button state long interval = 100; // blink interval - change to suit long previousMillis = 0; // variable to store last time LED was updated long startTime ; // start time for stop watch long elapsedTime ; // elapsed time for stop watch int fractional; // variable used to store fractional part of time
byte lastbuttonState2 = 0; // new global variable byte buttonState1 = 0; byte buttonState2 = 0;
void setup() { Serial.begin(9600); lcd.begin(16, 2); // Print a message to the LCD. lcd.print("Test");
pinMode(ledPin, OUTPUT); // sets the digital pin as output
pinMode(buttonPin, INPUT); // not really necessary, pins default to INPUT anyway digitalWrite(buttonPin, HIGH); // turn on pullup resistors. Wire button so that press shorts pin to ground.
}
void loop() { buttonState1 = digitalRead(buttonPin); delay(20); buttonState2 = digitalRead(buttonPin);
if(buttonState1 == buttonState2) { if(buttonState2 != lastButtonState) { activate(); lastButtonState = buttonState2; } }
}
void activate() { buttonState = digitalRead(buttonPin); // read the button state and store
if (buttonState == HIGH){ // if true then found a new button press while clock is not running - start the clock
startTime = millis(); // store the start time delay(5); // short delay to debounce switch lastButtonState = buttonState; // store buttonState in lastButtonState, to compare next time
}
else if (buttonState == LOW){ // if true then found a new button press while clock is running - stop the clock and report
elapsedTime = millis(); - startTime; // store elapsed time lastButtonState = buttonState; // store buttonState in lastButtonState, to compare next time
lcd.setCursor(0,1);
lcd.print(" "); // routine to report elapsed time lcd.print( (int)(elapsedTime / 1000L)); // divide by 1000 to convert to seconds - then cast to an int to print
lcd.print(".");
fractional = (int)(elapsedTime % 1000L);
if (fractional == 0) lcd.print("000"); else if (fractional < 10) lcd.print("00"); else if (fractional < 100) lcd.print("0");
lcd.println(fractional); // print fractional part of time
}
else{ lastButtonState = buttonState; // store buttonState in lastButtonState, to compare next time }
}
|
|
|
|
|
Logged
|
|
|
|
|
|