2
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int ssPin = 8;
int resetPin = 9;

bool cntrRunning = false;

int sec = 0;
int min = 0;
double currentMillis;

void setup()
{
    Serial.begin(9600);
    lcd.begin(16, 2);
    lcd.print("00:00");
    pinMode(ssPin, INPUT);
    pinMode(resetPin, INPUT);
}

void loop()
{
    if (digitalRead(ssPin) == HIGH)
    {
        if (cntrRunning)
        {
            stop();
        }
        else
        {
            start();
        }
    }
    else
    {
        if (cntrRunning)
        {
            keepRunning();
        }
        else
        {
            stop();
        }
    }

    if (digitalRead(resetPin) == HIGH)
    {
        if (cntrRunning)
        {
            stop();
            reset();
        }
        else
        {
            reset();
        }
    }
    delay(10);
}

void start()
{
    currentMillis = millis();
    cntrRunning = true;
    Serial.println("started");
}

void keepRunning()
{
    cntrRunning = true;

    if ((millis() - currentMillis) >= 1000)
    {
        sec++;
        currentMillis = millis();
        Serial.println(sec);

        if (sec < 10)
        {
            lcd.setCursor(4,0);
            lcd.print(sec);
        }
        else if (sec == 60)
        {
            min++;
            sec = 0;
            lcd.setCursor(3,0);
            lcd.print("00");
        }
        else
        {
            lcd.setCursor(3,0);
            lcd.print(sec);
        }

        if (min < 10)
        {
            lcd.setCursor(1,0);
            lcd.print(min);
        }
        else if (min == 60)
        {
            min = 0;
            lcd.setCursor(0,0);
            lcd.print("00");
        }
        else
        {
            lcd.setCursor(0,0);
            lcd.print(min);
        }
    }
}

void stop()
{
    cntrRunning = false;
    Serial.println("stopped");
}

void reset()
{
    if (cntrRunning)
    {
        stop();
    }
    reset();
    int sec=0;
    int min=0;
}

My professor wants us to add debouncing to the set/stop button but I'm a little confused on how to implement it.

  • do you understand the reason why debouncing is required? – jsotola Oct 28 at 2:20
  • No not entirely. – Amy Oct 28 at 3:09
  • ok ... imagine a tennis ball ... hold it high over your head and drop it ... the ball represents the moving contact of a switch ... the floor represents the fixed contact of a switch ... actual switch contacts do something similar to the ball, but in the millisecond realm .... the probem arises in the fact that the microcontroller sees each of the bounces as a keypress – jsotola Oct 28 at 3:42
  • @Amy, a typical tactile button finishes bouncing and achieves stability after about 50 ms. To test for stability, repeatedly sample the input pin looking for a series of equal values, i.e. 1,1,1,1,1 or 0,0,0,0,0. Your loop() runs once every 10 ms, so 5 equal values in a row is 50 ms of stability. Now base your if decisions on this stable value. – tim Oct 28 at 14:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.