TransWikia.com

Please help with my tinkercad code. Debouncing stopwatch with lcd

Arduino Asked by Amy on December 21, 2020

#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.

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP