Advertisement
learnelectronics

Arduino Totalizer

Dec 11th, 2017
1,055
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.65 KB | None | 0 0
  1. /*
  2.  * Arduino Totalizer
  3.  *
  4.  * learnelectronics
  5.  * 12 DEC 2017
  6.  *
  7.  * www.youtube.com/c/learnelectronics
  8.  *
  9.  * note: range: +/- 5-300Hz
  10.  */
  11.  
  12. #include <Wire.h>                                     //I2C Driver for OLED
  13. #include <Adafruit_SSD1306.h>                         //OLED Driver
  14.  
  15. #define OLED_RESET 4                                  //for oled
  16.  
  17. Adafruit_SSD1306 display(OLED_RESET);                 //create instance of SS1306 called display
  18.  
  19. long count = 0;                                       //declare var for counting
  20. int flag = 0;                                         //declare variable for stop flag
  21.  
  22. void setup()   {                
  23.  
  24.   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);          //start OLED @ hex addy 0x3c
  25.   display.display();                                  //initialize display
  26.   display.clearDisplay();
  27.   pinMode(8,INPUT);                                   //set dig pin 8 as input
  28.   pinMode(12,INPUT_PULLUP);                           //set dig pin 12 as input w/internal pullup
  29.  
  30. }
  31.  
  32. void loop() {
  33.  
  34. int stopnow = digitalRead(12);                        //check if stop switch is pressed
  35. if(stopnow == 0)flag = 1;                             //if stop switch has been pressed set flag
  36. int check = digitalRead(8);                           //check for a pulse
  37. if(check==1 && flag == 0)count++;                     // if the pulse is high AND the flag is not set increment count
  38.  
  39.   display.clearDisplay();
  40.   display.setTextSize(3);
  41.   display.setTextColor(WHITE);
  42.   display.setCursor(0,0);
  43.   display.println(count);                             //show the count
  44.   display.display();
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement