Offline
Newbie
Karma: 0
Posts: 24
|
 |
« Reply #30 on: February 01, 2013, 05:36:51 pm » |
I still cant get it to work... Here is the code: #include <Wire.h> #include <LiquidCrystal.h>
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
boolean test = false; boolean start = false; byte lastbuttonState2 = 0; // new global variable byte buttonState1 = 0; byte buttonState2 = 0;
void setup() { Serial.begin(9600);
lcd.begin(16, 4); // 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 == HIGH){ start = true; startTime = millis(); lastButtonState = buttonState2; } if(start == true){ elapsedTime = millis() - startTime; // store elapsed time lastButtonState = buttonState; // store buttonState in lastButtonState, to compare next time
lcd.setCursor(0,2);
lcd.print(" "); // routine to report elapsed time lcd.print( (int)(elapsedTime / 1000UL)); // divide by 1000 to convert to seconds - then cast to an int to print
lcd.print(".");
fractional = (int)(elapsedTime % 1000UL);
if (fractional == 0) lcd.print("000"); else if (fractional < 10) lcd.print("00"); else if (fractional < 100) lcd.print("0");
lcd.print(fractional); // print fractional part of time } if(test == true){ if(buttonState1 == HIGH) start == true; else;{} } } }
|
|
|
|
|
Logged
|
|
|
|
|
Queens, New York
Online
Edison Member
Karma: 27
Posts: 1538
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #31 on: February 01, 2013, 06:04:27 pm » |
You've got a delay(20) in there. Your minimum response is over 20 ms....
It'd be smoother to loop while ( now - startPress > 10 ) true after 10 ms continuous contact, Arduino can check that button 8000 times in 10 ms so why have it do nothing for 20 ms?
I ment it to be 10 but I pressed 2 instead, and just never bothered to change it. It's a crude way to do it, I know, but it works as a temporary debounce.
|
|
|
|
|
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
|
|
|
|
Queens, New York
Online
Edison Member
Karma: 27
Posts: 1538
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #32 on: February 01, 2013, 06:11:49 pm » |
I still cant get it to work... Here is the code: #include <Wire.h> #include <LiquidCrystal.h>
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
boolean test = false; boolean start = false; byte lastbuttonState2 = 0; // new global variable byte buttonState1 = 0; byte buttonState2 = 0;
void setup() { Serial.begin(9600);
lcd.begin(16, 4); // 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 == HIGH){ start = true; startTime = millis(); lastButtonState = buttonState2; } if(start == true){ elapsedTime = millis() - startTime; // store elapsed time lastButtonState = buttonState; // store buttonState in lastButtonState, to compare next time
lcd.setCursor(0,2);
lcd.print(" "); // routine to report elapsed time lcd.print( (int)(elapsedTime / 1000UL)); // divide by 1000 to convert to seconds - then cast to an int to print
lcd.print(".");
fractional = (int)(elapsedTime % 1000UL);
if (fractional == 0) lcd.print("000"); else if (fractional < 10) lcd.print("00"); else if (fractional < 100) lcd.print("0");
lcd.print(fractional); // print fractional part of time } if(test == true){ if(buttonState1 == HIGH) start == true; else;{} } } }
I guess my hint wasn't easy enough to understand. It's only 3 new lines you need to add, along with any brackets.
|
|
|
|
« Last Edit: February 01, 2013, 07:09:54 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 #33 on: February 01, 2013, 06:15:38 pm » |
I am new at this so the learning curve is pretty steep.
couldnt you add the 3 lines in the code for me?
|
|
|
|
|
Logged
|
|
|
|
|
Pittsburgh, PA, USA
Offline
Faraday Member
Karma: 29
Posts: 2880
I only know some basic electricity....
|
 |
« Reply #34 on: February 01, 2013, 08:44:26 pm » |
Time debounced button presses, start to start not release to start. LED 13 lights when the button is pressed. TX flashes when debounce is done. Tested with UNO 3, serial monitor and jumper from pin 9 to gnd as button. // Time debounced button presses, start to start not release to start.
//#include <Wire.h> //#include <LiquidCrystal.h>
// LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#define LEDPIN 13 // LED connected to digital pin 13 #define BPIN 9 // button on pin 4
byte buttonState; // variable to store button state unsigned long debounce = 0UL; // variable to store last time LED was updated unsigned long Timer ; // start/elapsed time for stop watch
void setup() { Serial.begin(9600);
//lcd.begin(16, 4); // Print a message to the LCD. //lcd.print("Test");
Serial.println("Test");
pinMode( LEDPIN, OUTPUT ); // sets the digital pin as output digitalWrite( LEDPIN, LOW ); // turn on pullup resistors. Wire button so that press shorts pin to ground.
pinMode( BPIN, INPUT ); // not really necessary, pins default to INPUT anyway digitalWrite( BPIN, HIGH ); // turn on pullup resistors. Wire button so that press shorts pin to ground.
Timer = millis(); // Timer now is start millis }
void loop() { digitalWrite( LEDPIN, LOW );
while ( digitalRead( BPIN )); // button is not pressed
digitalWrite( LEDPIN, HIGH );
debounce = millis(); while (( millis() - debounce < 10UL ) && ( buttonState = !digitalRead( BPIN ))); // button pressed && time not up
if ( buttonState ) // success if contact holds till time is up, { Timer = millis() - Timer; // Timer now is elapsed millis
/* lcd.setCursor(0,2); lcd.print( " " ); // routine to report elapsed time lcd.print( Timer / 1000UL ); // convert to seconds and print lcd.print( "." ); Timer = Timer % 1000UL; // Timer now is remaining millis < 1 sec if ( Timer < 100UL ) { lcd.print( "0" ); if ( Timer < 10UL ) { lcd.print( "0" ); } } lcd.print( Timer ); */ Serial.println();
Serial.print( " " ); // routine to report elapsed time Serial.print( Timer / 1000UL ); // divide by 1000 to convert to seconds - then cast to an int to print
Serial.print( "." );
Timer = Timer % 1000UL; // Timer now is remaining millis < 1 sec
if ( Timer < 100UL ) { Serial.print( "0" ); if ( Timer < 10UL ) { Serial.print( "0" ); } }
Serial.println( Timer );
Timer = millis(); // start millis before the button is released
if ( !digitalRead( BPIN )) { while ( !digitalRead( BPIN )); // wait until BPIN is released } } }
|
|
|
|
« Last Edit: February 01, 2013, 08:48:06 pm by GoForSmoke »
|
Logged
|
Examples can be found at Learning in the Main Site and at the Playground
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 24
|
 |
« Reply #35 on: February 03, 2013, 04:49:14 am » |
Thanks to everyone for helping me in this mather, and a special thanks to "HazardsMind" for solving my problem. Here is the functioning code HazardsMind gave me: #include <Wire.h> #include <LiquidCrystal.h>
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
boolean test = false; boolean start = false; boolean lockout = false; //**NEW BOOLEAN** byte lastbuttonState2 = 0; // new global variable byte buttonState1 = 0; byte buttonState2 = 0;
void setup() { Serial.begin(9600);
lcd.begin(16, 4); // 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() { // no more debounce buttonState2 = digitalRead(buttonPin); if(buttonState2 == HIGH){ if(lockout == false){ // #1 start = true; startTime = millis(); lastButtonState = buttonState2; lockout = true; // (The lockout) this will make sure it does not go back to this IF statement } } if(buttonState2 == HIGH && start == true){ // #2 elapsedTime = millis() - startTime; // store elapsed time lastButtonState = buttonState; // store buttonState in lastButtonState, to compare next time
lcd.setCursor(0,2);
lcd.print(" "); // routine to report elapsed time lcd.print( (int)(elapsedTime / 1000UL)); // divide by 1000 to convert to seconds - then cast to an int to print
lcd.print(".");
fractional = (int)(elapsedTime % 1000UL);
if (fractional == 0) lcd.print("000"); else if (fractional < 10) lcd.print("00"); else if (fractional < 100) lcd.print("0");
lcd.print(fractional); // print fractional part of time } else lcd.clear(); // #3 }
|
|
|
|
|
Logged
|
|
|
|
|
Pittsburgh, PA, USA
Offline
Faraday Member
Karma: 29
Posts: 2880
I only know some basic electricity....
|
 |
« Reply #36 on: February 03, 2013, 11:20:20 pm » |
Hey, at least he got rid of the delays. Now it's responsive!
He's still working millis() with signed longs but you won't be running this any 24 days straight so no problem.
It's still good practice (so you don't trip up later doing "what worked" before) to use unsigned long or unsigned long long to work with millis() and micros(). Both of those functions return unsigned long which is clue #1 about best use of those values.
Micros() rolls over in 1.193... hours using unsigned long. It turns negative in half that with signed. Millis() can measure intervals to over 48 days because it takes 49.7... to overflow using unsigned long. With signed, just under 25 days to find that bug. With unsigned long long the longest interval is over 4 billion times as large, counting millisecs. The sun is not that old! With signed... you should live to see the bug! Or of course knowing about rollover the clever programmer writes some logic to handle it. But why do that when there's no need for extra code at all with unsigned long. If signed integers are like measuring sticks with 2 ends for - and +, unsigned integers are circles like clock faces. You measure around the circle and don't worry about crossing zero, it comes out in the math always as a positive. The sign is in the direction from-to you measured, not in the result, unsigned don't have sign.
|
|
|
|
|
Logged
|
Examples can be found at Learning in the Main Site and at the Playground
|
|
|
|
|