#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_LINES 4
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int montant = 0; // montant de l'utilisateur
int inputPin = 2; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
const int buttonPin = 8;
int oldValue = LOW; // default/idle value for pin 8 is low.
int pos = 0; // variable to store the servo position
void setup() {
pinMode(inputPin, INPUT); // declare sensor as input
// Init lcd
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("votre montant :");
pinMode(buttonPin, INPUT);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
Serial.begin(9600);
}
void loop() {
//motionn sensor + lcd update
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
if (pirState == LOW) {
// we have just turned on
Serial.println("Motion detected!");
montant += 500;
lcd.setCursor(0, 1);
lcd.print(montant);
// We only want to print on the output change, not state
pirState = HIGH;
}
} else {
if (pirState == HIGH) {
// we have just turned of
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
}
}
//button de choix
// Read the value of pin 8.
int newValue = digitalRead(buttonPin);
if (newValue != oldValue)
{
if (newValue == HIGH)
{
Serial.println("The button is pressed.");
if (montant >= 500) {
montant -= 500;
}
lcd.print("");
lcd.setCursor(0, 1);
lcd.print(montant);
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos' // waits 15ms for the servo to reach the position
}
}
else
{
Serial.println("The button is released.");
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos' // waits 15ms for the servo to reach the position
}
}
// Remember the value for the next time.
oldValue = newValue;
}
}