const int button = A0; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to
#include <LiquidCrystal.h>
//rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // input value
int buttonCounter = 12; // number of fulled stock
int hole_detected = 0; // current state of the button
int lasthole_detected = 0; // previous state of the button
int checkHole(void) {
static int state = 0; // private state variable
int sensorNow = analogRead(button);
if ((state == 0) && (sensorNow > 750))
{ // have a hole
state = 1;
if(buttonCounter > 0){
buttonCounter--; // decrease counting
}
else{
// add more
}
}
if ( (state == 1) && (sensorNow < 100))
{ // no hole
state = 0;
}
return state;
}
void setup() {
// initialize the button pin as a input:
pinMode(button, INPUT);
lcd.begin(16, 2); //size of display
lcd.setCursor(4, 0);
lcd.print("padcount");
delay(200);
}
void loop() {
static int lastCount = 0 ;
checkHole();
if (buttonCounter != lastCount) {
lcd.setCursor(4, 0); // set a location of words (rows,column)
lcd.println("Stock: "); //print text
lcd.setCursor(6, 1);
lcd.println(buttonCounter);
lastCount = buttonCounter;
if (lastCount < 1){ //condition when a stock is out.
lcd.setCursor(1,1);
lcd.println("pls refill :(");
}
else {
// do something
}
}
delay(10);
}