/*
  Green pushbutton counts up and red pushbutton counts down when pressed.
  by kidkara
*/
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

unsigned long curmil;
unsigned long prevmil;
const unsigned long duration = 200; // debounce time
int count = 0;
int maxcount = 25; // maximum limit can be adjusted to suit your required range
int mincount = 0; // minimum limit can be adjusted to suit your required range
const int greenpin2 = 2; // Green pushbutton 1 increment
const int redpin3 = 3; // Red pushnutton 2 decrement

void setup() {
  lcd.init();
  lcd.backlight();
  pinMode(greenpin2, INPUT_PULLUP);
  pinMode(redpin3, INPUT_PULLUP);

  if (mincount != 0)
  {
    count = mincount;
  }
  lcd.setCursor(0, 0);
  lcd.print(count);
}


void loop() {
  curmil = millis();
  
  if (digitalRead(greenpin2) == LOW && count < maxcount && curmil - prevmil>= duration)
  {  // Counts up
    count = ++count;
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print(count);
    prevmil= curmil;
  }

  if (digitalRead(redpin3) == LOW && count > mincount && curmil - prevmil>= duration)
  {  // Counts down
    count = --count;
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print(count);
    prevmil= curmil;
  }


}