Hi,
I am beginner in arduino and I am trying to make this program work. But when I try it it doesn't print anything.
I will really appreciate if someone could help me.
This is my code:
int firstClickTime = 0;
int secondClickTime = 0;
byte button = 2;
byte buttonState = 0;
int sum = 0;
void setup() {
Serial.begin(9600);
pinMode(button,INPUT);
}
void loop() {
buttonState = digitalRead(button);
if ((buttonState == HIGH) ){
sum++;
if (sum==1){
firstClickTime = millis();
if(sum == 2 ){
secondClickTime = millis();
sum = 0;
if ( secondClickTime - firstClickTime >= 2000) {
Serial.println("penalty fee");
}
}
}
}
}
You could try it like this using libraies. (I) think this makes the code far simpler and easier to understand. Your milage may vary.
#include <mechButton.h>
mechButton aButton(2);
timeObj buttonTimer(2000);
bool firstClick;
void setup(void) {
Serial.begin(9600); // Fire up serial stuff.
aButton.setCallback(buttonClick); // Atttach a callback to the button.
firstClick = true; // This is our first click.
}
void buttonClick(void) {
if (!aButton.trueFalse()) { // If we got a click.. (Pulled to ground)
if (firstClick) { // If its the first click..
firstClick = false; // Note that we are no longer waiting for the first click.
} else { // Else, we got a subsequent click..
if (!buttonTimer.ding()) { // If the timer has NOT expired..
Serial.println("penalty fee"); // Tell 'em there's a fee.
}
}
buttonTimer.start(); // We restart the timer for the next click.
}
}
void loop(void) {
idle(); // Let the background stuff (like the button) do their thing.
}
If you would like to try it this way you will need to install LC_baseTools from the library manager. (tools menu in the IDE)