#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
//Variables
int counter = 0;
int addup = 2;
int adddown = 3;
int MotorButton = 4;
int MotorStart = 11;
String motorState;
int motordir;
void setup()
{
pinMode(MotorButton,INPUT_PULLUP);
pinMode(MotorStart,OUTPUT);
pinMode(adddown, INPUT_PULLUP);
pinMode(addup, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(adddown),ISR_counterdown,FALLING);
attachInterrupt(digitalPinToInterrupt(addup),ISR_counterup,FALLING);
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Hello!");
lcd.setCursor(0,1);
lcd.print("This is a mixer.");
delay(3000);
lcd.setCursor(0,0);
lcd.print("Minimum speed 30");
lcd.setCursor(0,1);
lcd.print("Maximum speed 70");
delay(3000);
lcd.clear();
}
void loop()
{
lcd.setCursor(0,0);
lcd.print("Counter = ");
lcd.setCursor(10,0);
lcd.print(counter);
lcd.setCursor(0,1);
if (MotorButton == HIGH){
motorState = "on";
MotorStart = HIGH;
} else {
motorState = "off";
MotorStart = LOW;
}
lcd.print("Motor State = ");
lcd.setCursor(13,1);
lcd.print(motorState);
if (addup == HIGH && adddown == HIGH){
changeDirection();
delay(1000);
}
}
void changeDirection (){
if (motordir == HIGH){
motordir = LOW;
} else if(motordir == LOW) {
motordir = HIGH;
}
}
void ISR_counterup(){
static unsigned long previousTime = 0;
unsigned long currentTime = millis();
int deltaTime =300;
if (currentTime - previousTime > deltaTime){
counter = counter + 1;
}
previousTime = currentTime;
}
void ISR_counterdown(){
static unsigned long previousTime = 0;
unsigned long currentTime = millis();
int deltaTime =300;
if (currentTime - previousTime > deltaTime){
counter = counter - 1;
}
previousTime = currentTime;
}