skizziks_53

Reddit pot belt counter v1.0

May 31st, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.14 KB | None | 0 0
  1. /*
  2.    Reddit pot belt counter
  3.    May 31, 2019
  4.    Function: increment a counter every time pin #8 is shorted to ground.
  5. */
  6.  
  7.  
  8. #include <LiquidCrystal_I2C.h> // Might be this library? -- https://www.arduinolibraries.info/libraries/liquid-crystal-i2-c
  9. #include <Wire.h>
  10. #include <SPI.h>
  11.  
  12. LiquidCrystal_I2C lcd(0x20, 20, 4); // Set the LCD I2C address
  13.  
  14. long potCount = 0; // I got rid of the ending zero on this variable. It makes me suspicious that the chip is not trying to increment by zero (potCount 0++) for some reason?...
  15.  
  16. int sensor_current_state = 0; // This variable will alternate between zero and 1.
  17. int sensor_previous_state = 0; // This variable will alternate between zero and 1.
  18.  
  19. bool sensor_enabled = true;
  20. unsigned long sensor_delay__current_time = 0;
  21. unsigned long sensor_delay__previous_time = 0;
  22. unsigned long sensor_delay_interval = 100; // The time in milliseconds to de-bounce the sensor input.
  23.  
  24.  
  25.  
  26. void setup()
  27. {
  28.   pinMode(8, INPUT);
  29.   digitalWrite(8, HIGH); // turn on pullup resistors
  30.  
  31.   lcd.init();
  32.   lcd.backlight();
  33.   lcd.setCursor(0, 1);
  34.   lcd.print("Variety Total");
  35.   update_displayed_value();
  36. }
  37.  
  38.  
  39.  
  40. void loop() {
  41.  
  42.   if (sensor_enabled == true) {
  43.     if (digitalRead(8) == LOW) {
  44.       sensor_current_state = 1;
  45.     }
  46.     else {
  47.       sensor_current_state = 0;
  48.     }
  49.     if (sensor_previous_state == 0) {
  50.       if (sensor_current_state == 1) {
  51.         potCount++;
  52.         update_displayed_value();
  53.         sensor_enabled = false;
  54.         sensor_delay__previous_time = millis();
  55.       }
  56.     }
  57.     sensor_previous_state = sensor_current_state;
  58.   }
  59.  
  60.   if (sensor_enabled == false) { // ----- if sensor_enabled == false
  61.     sensor_delay__current_time = millis();
  62.     if (sensor_delay__current_time >= sensor_delay__previous_time) {
  63.       if (sensor_delay__current_time >= (sensor_delay__previous_time + sensor_delay_interval)) {
  64.         sensor_enabled = true;
  65.       }
  66.     }
  67.     else {
  68.       sensor_delay__previous_time = millis();
  69.     }
  70.   }
  71.  
  72. } // end of main loop
  73.  
  74.  
  75. void update_displayed_value() {
  76.   lcd.setCursor(0, 2);
  77.   lcd.print("        ");
  78.   lcd.setCursor(0, 2);
  79.   lcd.print(potCount);
  80. }
Add Comment
Please, Sign In to add comment