Stopwatch with I/O micro switches instead of push buttons.

Hi All,

Being new to Arduino and programming, I hope you could help me please.

I programmed a stopwatch I found on YouTube (Youtube Stopwatch) with a Nano and an OLED SSD1306 128x64 display.

This stopwatch works with two push buttons, one to start/reset and one to stop. I’d like to replace the two push buttons with either one push button to reset/start when pressed and stop when released, or with one/two on/off switches. I have found by replacing the push buttons with on/off switches, the stopwatch goes into an endless loop of stop/start and I know too little about programming to remove the loop and only run the stopwatch code once, or change it to work with using I/O micro switches.

My goal is to use this code to accurately measure the time an automated door closes and with on/off micro switches providing the signal for reset/start and stop.

Any help and ideas are highly appreciated.

Thanks!

#include <Wire.h>                               //library for the I2C display
#include <Adafruit_SSD1306.h>                   //driver for the display

#define OLED_RESET 4                            //display reset is on digital 4 but not used

Adafruit_SSD1306 display(OLED_RESET);           //create instance of the SSD306 called display

unsigned long start, finished, elapsed;         //create unsigned long integer variables for start finished elapsed


void setup()                                    //setup - only runs on power up or reset
{

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);    //begin the display @ hex address 0x3c
  display.display();                            //show splash screen
  display.clearDisplay();                       //clear display
  
 pinMode(2, INPUT);                             // the start button
 pinMode(3, INPUT);                             // the stop button

  display.setTextSize(1);                       //set display params
  display.setTextColor(WHITE);                  //set display params
  display.setCursor(0,0);                       //set display params
  display.println("Blue to Start");             //send initial message to display buffer
  display.println("Red to Stop");               //send initial message to display buffer
  display.display();                            //showbuffer
 
}
void displayResult()                            //create a function called displayResult
{
  display.setTextSize(1);                       //set display params
  display.setTextColor(WHITE);                  //set display params
  display.setCursor(0,0);                       //set display params
 display.clearDisplay();                        //clear display & buffer
 float h, m, s, ms;                             //create float variables (numbers with dec point)
 unsigned long over;                            //create unsigned long integer variable over
 elapsed = finished - start;                    //elapsed time is finished minus start
 h = int(elapsed / 3600000);                    //h is the integer created by dividing elapsed by 360000
 over = elapsed % 3600000;                      //over is created by elapsed modulo 360000
 m = int(over / 60000);                         //m is the integer created by dividing over (seconds over 60) by 60000
 over = over % 60000;                           //new over is over modulo 60000
 s = int(over / 1000);                          //seconds is the integer created by dividing over by 1000
 ms = over % 1000;                              //ms is created by new over modulo 1000
 display.print("Elapsed time: ");               //send a title for elapsed to display buffer
 display.println(elapsed);                      //send the raw elapsed time (miliseconds) to display buffer
 display.println("Stop time: ");                //send title for h,m,s,ms to display buffer
 display.print(h, 0);                           //send value to display buffer, no decimals
 display.print("h ");
 display.print(m, 0);                           //send value to display buffer, no decimals
 display.print("m ");
 display.print(s, 0);                           //send value to display buffer, no decimals
 display.print("s ");
 display.print(ms, 0);                          //send value to display buffer, no decimals
 display.println("ms");
 display.println();                             //send blank line to display buffer
 display.display();                             //show buffer data on display
}
void loop()
{
 display.clearDisplay();                        //set display params
 display.setTextSize(1);                        //set display params
  display.setTextColor(WHITE);                  //set display params
  display.setCursor(0,0);                       //set display params
 display.clearDisplay();                        //clear display & buffer
 if (digitalRead(2) == HIGH)                     //if you pressed the blue button
 {
 start = millis();                              //start = current millis
 delay(100);                                    // for debounce
 display.println("Started...");                 //send started to display buffer
 display.display();                             //show buffer data on display
 }
 if (digitalRead(3) == HIGH)                    //if you pressed the red buton
 {
 finished = millis();                           //finished = curent millis
 delay(100);                                    // for debounce
 displayResult();                               //call function displayResult
 }
}

+1 for using code tags on your first post.

You inputs are defined as INPUT so you need to have pull-up or pull-down resistors associated with them to make them read HIGH/LOW and not be in a floating state. The typical way is do define them as INPUT_PULLUP which enables the internal pullup on the chip so you don't need external resistors. This does change the logic sense when reading so a HIGH value means no press and a LOW value means it is pressed.

You may also want to check out the State Change example in the IDE (File->Examples-02 Digital) because you really want to detect when the buttons change, not necessarily what their current state is.