Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Reddit pot belt counter
- May 31, 2019
- Function: increment a counter every time pin #8 is shorted to ground.
- */
- #include <LiquidCrystal_I2C.h> // Might be this library? -- https://www.arduinolibraries.info/libraries/liquid-crystal-i2-c
- #include <Wire.h>
- #include <SPI.h>
- LiquidCrystal_I2C lcd(0x20, 20, 4); // Set the LCD I2C address
- 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?...
- int sensor_current_state = 0; // This variable will alternate between zero and 1.
- int sensor_previous_state = 0; // This variable will alternate between zero and 1.
- bool sensor_enabled = true;
- unsigned long sensor_delay__current_time = 0;
- unsigned long sensor_delay__previous_time = 0;
- unsigned long sensor_delay_interval = 100; // The time in milliseconds to de-bounce the sensor input.
- void setup()
- {
- pinMode(8, INPUT);
- digitalWrite(8, HIGH); // turn on pullup resistors
- lcd.init();
- lcd.backlight();
- lcd.setCursor(0, 1);
- lcd.print("Variety Total");
- update_displayed_value();
- }
- void loop() {
- if (sensor_enabled == true) {
- if (digitalRead(8) == LOW) {
- sensor_current_state = 1;
- }
- else {
- sensor_current_state = 0;
- }
- if (sensor_previous_state == 0) {
- if (sensor_current_state == 1) {
- potCount++;
- update_displayed_value();
- sensor_enabled = false;
- sensor_delay__previous_time = millis();
- }
- }
- sensor_previous_state = sensor_current_state;
- }
- if (sensor_enabled == false) { // ----- if sensor_enabled == false
- sensor_delay__current_time = millis();
- if (sensor_delay__current_time >= sensor_delay__previous_time) {
- if (sensor_delay__current_time >= (sensor_delay__previous_time + sensor_delay_interval)) {
- sensor_enabled = true;
- }
- }
- else {
- sensor_delay__previous_time = millis();
- }
- }
- } // end of main loop
- void update_displayed_value() {
- lcd.setCursor(0, 2);
- lcd.print(" ");
- lcd.setCursor(0, 2);
- lcd.print(potCount);
- }
Add Comment
Please, Sign In to add comment