I am starting to write the code for my binary clock, and I am stuck...
You know how on a normal clock, you can press the buttons and the time goes up by 1, and if you hold the button down, it just keeps going up? I want to do this, but I'm not sure how to... How can I tell between a single press and a hold?
I still don't know how... I'm not being lazy, I'm just a 12 year old kid who doesn't understand how to make it work... I'll keep trying, but I'm not sure...
You need to recognise two events: one when the switch is pressed, and one when the switch is released.
Start a timer when the first event happens, and if the current time each time through loop is "n" milliseconds after the first event, start counting up, until you get a release event.
[edit]No, your solution won't work, because you need to detect the change of state (from low to high), not the state itself ("==HIGH), otherwise you'll just keep resetting "time" to the current value of "millis()"[/edit]
See my edited comments, and don't forget "loop" runs many thousands of times a second.
Also, if something read by "digitalRead" isn't HIGH, it must be LOW, so there's no need to test it again.
Also buttonPin has a value of 13, so is never LOW.
[edit]Come to that, it's never HIGH either! ;D[/edit]
int buttonPin = 13;
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
unsigned long time;
void setup()
{
pinMode(buttonPin, INPUT);
Serial.begin(9600);
}
void loop()
{
buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState){
if (buttonState == HIGH) {
time = millis();
}
else{
time = 0;
}
lastButtonState = buttonState;
}
Serial.print("Time: ");
Serial.println(time);
}
As far as? Well, will I know that this isn’t completely for the press / hold… This just shows how long it’s being pressed… Then I would need two “if” statements, like if time <= 500, and if time >= 500…
Would this code work just to show how long the button has been pressed?
Well, I put the circut together real quick… When I tested it, my results weren’t what I had hoped…
What I got was then I pressed the button, the time would display, but it would be the time since the program started running… Also, the time wouldn’t update until I let the button go and and then pressed it again…
My current code:
int buttonPin = 12;
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
unsigned long time;
void setup()
{
pinMode(buttonPin, INPUT);
digitalWrite(buttonPin, HIGH);
Serial.begin(9600);
}
void loop()
{
buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState){
if (buttonState == LOW) {
time = millis();
}
else{
time = 0;
}
lastButtonState = buttonState;
}
Serial.print("Time: ");
Serial.println(time);
}
Don't forget to debounce the button presses (i.e. consider digitalRead of a button to be unreliable the first 20ms or so after detecting a state change).
The Serial.printing for every round of loop may take care of the debouncing for now.
And here is a modded version to serial print the output. Also, when you let go, it prints 0's. When you press it again, it will only print if pressTime is 1 or more.
Again, props to Kevin Darrah for the help and pushing me to finish this when I wanted to give up on it!