Advertisement
learnelectronics

Arduino Tally Counter

Dec 15th, 2022
2,508
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 2.72 KB | Software | 0 0
  1. //****************************************************
  2. //*              Arduino Tally Counter               *
  3. //*                learnelectronics                  *
  4. //*         www.youtube.com/learnelectronics         *
  5. //*                                                  *
  6. //* **************************************************
  7.  
  8. #include <Wire.h>                                     //library for the OLED on I2C
  9. #include <Adafruit_GFX.h>                             //goes with OLED driver
  10. #include <Adafruit_SSD1306.h>                         //OLED driver
  11.  
  12. #define OLED_RESET 4                                  //required for oled
  13. Adafruit_SSD1306 display(OLED_RESET);                 //setup oled as "display"
  14.  
  15. int count = 0;                                        //initial/reset state of main variable
  16. int buttonState = LOW;                                //used to debounce the button
  17. long lastDebounceTime = 0;                            //used to debounce the button
  18. long debounceDelay = 500;                             //used to debounce the button
  19.  
  20.  
  21.  
  22.  
  23. void setup() {
  24.  
  25.   pinMode(2,INPUT_PULLUP);                            //main switch held high with internal pullup
  26.   pinMode(4,INPUT_PULLUP);                            //counter reset held high blah blah
  27.  
  28.   //Serial.begin(9600);                               //serial comms for debugging
  29.   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);          //turn on ze bubble machine
  30.   display.display();                                  //show display
  31.   delay(500);                                         //pause
  32.   display.clearDisplay();                             //clear display
  33.  
  34.  
  35. }
  36.  
  37. void loop() {
  38.  
  39.  int pressed = (digitalRead(2));                      //pressed is set to state of pin 2
  40.  
  41.  //Serial.println(pressed);                        
  42.  
  43.  
  44.                                                      // debounce routine
  45.  if ( (millis() - lastDebounceTime) > debounceDelay) {
  46.  
  47.                                                      //if the button was pressed
  48.     if ( (pressed == LOW) ) {
  49.  
  50.       count++;                                       // increment count -- ie the whole purpose for the program
  51.      
  52.       lastDebounceTime = millis();                   //set the current time
  53.     }
  54.    
  55.  
  56.   }
  57.  
  58.  int setToZero = (digitalRead(4));                  //did someone hit reset?
  59.  if (setToZero == 0) count = 0;                     // set the count to zero
  60.  // Serial.println(setToZero);
  61.  
  62.                                                     //show the vaule of count on the oled
  63.   display.clearDisplay();
  64.   display.setTextSize(5);
  65.   display.setTextColor(WHITE);
  66.   display.setCursor(0,0);
  67.   display.clearDisplay();
  68.   display.println(count);
  69.   display.display();
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement