/*
* sketch used to test everything ...
*
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define BTN1 2 // push button input
bool btn1State = 0 ;
// rotary encoder 1
#define CLK1 3
#define DT1 4
#define SW1 5
int counter1=0;
int currentStateCLK1;
int lastStateCLK1;
String currentDir1="";
unsigned long lastButtonPress1 = 0;
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
// the setup function runs once when you press reset or power the board
void setup() {
Serial.begin(9600);
// digital inits
pinMode(LED_BUILTIN, OUTPUT);
pinMode(BTN1, INPUT_PULLUP);
pinMode(CLK1,INPUT);
pinMode(DT1,INPUT);
pinMode(SW1, INPUT_PULLUP);
// fct launched at startup
LCD_intro();
attachInterrupt(digitalPinToInterrupt(CLK1), check_encoders, FALLING);
}
// the loop function runs over and over again forever
void loop() {
// blink_led();
check_button();
LCD_menu();
}
void check_encoders(){
int dtValue = digitalRead(DT1);
if (dtValue == HIGH) {
counter1++; // Clockwise
}
if (dtValue == LOW) {
counter1--; // Counterclockwise
}
Serial.println(counter1);
// Read the button state
int btnState1 = digitalRead(SW1);
//If we detect LOW signal, button is pressed
if (btnState1 == LOW) {
//if 50ms have passed since last LOW pulse, it means that the
//button has been pressed, released and pressed again
if (millis() - lastButtonPress1 > 50) {
Serial.println(F("Button 1 pressed!"));
counter1 = 0;
LCD_menu();
}
// Remember last button press event
lastButtonPress1 = millis();
}
// Put in a slight delay to help debounce the reading
delay(1);
}
void check_button(){
btn1State = digitalRead(BTN1);
if(btn1State == 0){
digitalWrite(LED_BUILTIN, HIGH);
LCD_message("button pressed !","waiting 1s ...",1000);
}
else {
digitalWrite(LED_BUILTIN, LOW);
}
}
void blink_led(){
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(200); // wait
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(200);
}
void LCD_intro(){
// configure & launch LCD display
lcd.init();
lcd.clear();
lcd.backlight();
lcd.setCursor(0,0);
// TODO : make an animation with autoscroll() and ----
lcd.print("ARDUINO UNO tests");
lcd.setCursor(0,1);
lcd.print("by whips@musinux");
delay(500);
}
void LCD_message(String line1, String line2, int del){
lcd.clear();
lcd.setCursor(0,0);
lcd.print(line1); // 16 chars long
lcd.setCursor(0,1);
lcd.print(line2);
delay(del);
}
void LCD_menu(){
lcd.clear();
lcd.setCursor(0,0);
lcd.print(""); //
lcd.setCursor(0,1);
lcd.print(""); //
delay(100);
}