If two click in less than 2 seconds then penalty fee.

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");
    
                }
           }
     }
}  

}

int firstClickTime = 0;
int secondClickTime = 0;

These should be type ‘unsigned long’.


You should only increment your variable ‘when’ to switch changes from LOW to HIGH.


Do you have your serial monitor at 9600 baud ?


Does the serial print example in the IDE print to the serial monitor ?


Show us a good schematic of your circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.
Posting images:


FYI

Welcome

Your

if(sum == 2 )

is inside

if (sum==1)

, that will never be true because it can't be 1 and 2 at the same time.

But more important is the fact that your code doesn't detect button state changes and doesn't debounce the button

I recommend that you use this little library GitHub - JChristensen/JC_Button: Arduino library to debounce button switches, detect presses, releases, and long presses

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)

Good luck!

-jim lee