learnelectronics

Arduino Stopwatch

Jul 11th, 2017
3,524
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.76 KB | None | 0 0
  1. /*
  2.  * Arduino Stopwatch
  3.  * based on original sketch by John Boxall
  4.  *
  5.  * learnelectronics
  6.  * 10 JUN 2017
  7.  *
  8.  * www.youtube.com/c/learnelectronics
  9.  */
  10.  
  11.  
  12.  
  13. #include <Wire.h>                               //We need the wire library for the I2C display
  14. #include <Adafruit_SSD1306.h>                   //We need the driver for the display
  15.  
  16. #define OLED_RESET 4                            //display reset is on digital 4 but not used
  17.  
  18.  
  19.  
  20.  
  21. Adafruit_SSD1306 display(OLED_RESET);           //create instance of the SSD306 called display
  22.  
  23.  
  24.  
  25.  
  26. unsigned long start, finished, elapsed;         //create unsigned long integer variables for start finished elapsed
  27.  
  28.  
  29.  
  30. void setup()                                    //setup - only runs on power up or reset
  31. {
  32.  
  33.   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);    //begin the display @ hex address 0x3c
  34.   display.display();                            //show splash screen
  35.   display.clearDisplay();                       //clear display
  36.  
  37.  pinMode(2, INPUT);                             // the start button
  38.  pinMode(3, INPUT);                             // the stop button
  39.  
  40.   display.setTextSize(1);                       //set display params
  41.   display.setTextColor(WHITE);                  //set display params
  42.   display.setCursor(0,0);                       //set display params
  43.   display.println("Green to Start");            //send initial message to display buffer
  44.   display.println("Red to Stop");               //send initial message to display buffer
  45.   display.display();                            //showbuffer
  46.  
  47. }
  48. void displayResult()                            //create a function called displayResult
  49. {
  50.   display.setTextSize(1);                       //set display params
  51.   display.setTextColor(WHITE);                  //set display params
  52.   display.setCursor(0,0);                       //set display params
  53.  display.clearDisplay();                        //clear display & buffer
  54.  float h, m, s, ms;                             //create float variables (numbers with dec point)
  55.  unsigned long over;                            //create unsigned long integer variable over
  56.  elapsed = finished - start;                    //elapsed time is finished minus start
  57.  h = int(elapsed / 3600000);                    //h is the integer created by dividing elapsed by 360000
  58.  over = elapsed % 3600000;                      //over is created by elapsed modulo 360000
  59.  m = int(over / 60000);                         //m is the integer created by dividing over (seconds over 60) by 60000
  60.  over = over % 60000;                           //new over is over modulo 60000
  61.  s = int(over / 1000);                          //seconds is the integer created by dividing over by 1000
  62.  ms = over % 1000;                              //ms is created by new over modulo 1000
  63.  display.print("Elapsed time: ");               //send a title for elapsed to display buffer
  64.  display.println(elapsed);                      //send the raw elapsed time (miliseconds) to display buffer
  65.  display.println("Stop time: ");                //send title for h,m,s,ms to display buffer
  66.  display.print(h, 0);                           //send value to display buffer, no decimals
  67.  display.print("h ");
  68.  display.print(m, 0);                           //send value to display buffer, no decimals
  69.  display.print("m ");
  70.  display.print(s, 0);                           //send value to display buffer, no decimals
  71.  display.print("s ");
  72.  display.print(ms, 0);                          //send value to display buffer, no decimals
  73.  display.println("ms");
  74.  display.println();                             //send blank line to display buffer
  75.  display.display();                             //show buffer data on display
  76. }
  77. void loop()
  78. {
  79.  display.clearDisplay();                        //set display params
  80.  display.setTextSize(1);                        //set display params
  81.   display.setTextColor(WHITE);                  //set display params
  82.   display.setCursor(0,0);                       //set display params
  83.  display.clearDisplay();                        //clear display & buffer
  84.  if (digitalRead(2) == HIGH)                    //if you pressed the green button
  85.  {
  86.  start = millis();                              //start = current millis
  87.  delay(200);                                    // for debounce
  88.  display.println("Started...");                 //send started to display buffer
  89.  display.display();                             //show buffer data on display
  90.  }
  91.  if (digitalRead(3) == HIGH)                    //if you pressed the red buton
  92.  {
  93.  finished = millis();                           //finished = curent millis
  94.  delay(200);                                    // for debounce
  95.  displayResult();                               //call function displayResult
  96.  }
  97. }
Add Comment
Please, Sign In to add comment