Pages: 1 [2]   Go Down
Author Topic: quick help -- button pressing and timing  (Read 1850 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 41
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i am not quite sure what the problem is but it may just be that something called 'contact bounce' is at work here
please look this up

i made a small tweak to the code
try this code:
Code:
boolean status;

void setup(){
Serial.begin(9600);
Status == digitalRead(5)
}
void loop(){
        if (digitalRead(5) == FALSE && status == TRUE){
                  Serial.println("5");
                  status = FALSE;
        }
        if (digitalRead(5) == TRUE){
                  status = TRUE;
        }
delay(20);
}
if contact bounce is the issue the code above should do the trick, also experiment with the delay time to find the minimum value for application because else it would slow down the whole loop
Logged

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

Yes I think that delay could fix it, but I was trying not to use delays so that I wouldn't miss key presses.  Do you think using some of the "Blink without Delay" code would fix it?

http://arduino.cc/en/Tutorial/BlinkWithoutDelay

Also, while I am currently using a button, I would like to switch to a rotary encoder.  I am not sure what that will do to my code.
« Last Edit: July 03, 2012, 05:49:58 am by scales11 » Logged

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

in my prev sketch Status == digitalRead(5) --> Status = digitalRead(5)

yes the blink without delay example will do fine just implement it in the right way
sketch:
Code:
#define contactbounce 20

boolean status;
unsigned int time;

void setup(){
Serial.begin(9600);
Status = digitalRead(5)
time =millis()
}
void loop(){
if (millis()+contactbounce>= time){
        if (digitalRead(5) == FALSE && status == TRUE){
                  Serial.println("5");
                  status = FALSE;
        }
        if (digitalRead(5) == TRUE){
                  status = TRUE;
        }
time = millis()
}
}
i never worked with rotaty encoders myself but ill bet there is some code for them somewhere on the arduino forum

some links:
http://arduino.cc/en/Tutorial/Debounce
http://arduino.cc/playground/Main/RotaryEncoders
Logged

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

Code:
unsigned int time;

void setup(){
Serial.begin(9600);
Status = digitalRead(5)
time =millis()
Aside from the milling semicolons, this code is a disaster waiting to happen.

millis() returns an unsigned long, not an unsigned int. Storing the value in an unsigned int will result in mangled data.
Logged

Pages: 1 [2]   Go Up
Jump to: