#include <Wire.h>
#include<Arduino.h>
#include <LiquidCrystal_I2C.h>
// #include <Adafruit_LiquidCrystal.h>
#define I2C_DEV_ADDR 0X27 //0x55
LiquidCrystal_I2C lcd_1(I2C_DEV_ADDR, 16,2);
// Adafruit_LiquidCrystal lcd_1(0x27);
#define DebugBegin(x) Serial.begin(x)
#define debug(x) Serial.print(x)
#define debugln(x) Serial.println(x)
#define ENCODER_CLK 25
#define ENCODER_DT 26
#define ENCODER_SW 12
#define LED 2
int myCount = 0;
int counter = 0;
uint32_t i = 0;
void readEncoder() {
int dtValue = digitalRead(ENCODER_DT);
if (dtValue == HIGH) {
counter++; // Clockwise
}
if (dtValue == LOW) {
counter--; // Counterclockwise
}
// debug("counter is ");
// debugln(counter);
}
void setup() {
Wire.begin();
Serial.begin(115200);
// debugln("Hello, ESP32!");
//lcd.init();
lcd_1.clear();
lcd_1.backlight();
lcd_1.setCursor(0, 0);
lcd_1.print("hello world :-)");
// printf(line0, "NEW hello world :-)");
// char line0[20] = "NEW hello world :-)";
// debugln("at line0");
// updateDisplay(0,0, mymessage);
// debugln("NEW hello world :-)");
pinMode(LED, OUTPUT);
pinMode(ENCODER_CLK, INPUT);
pinMode(ENCODER_DT, INPUT);
pinMode(ENCODER_SW, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(ENCODER_CLK), readEncoder, FALLING);
}
// Get the counter value, disabling interrupts.
// This make sure readEncoder() doesn't change the value
// while we're reading it.
int getCounter() {
int result;
noInterrupts();
result = counter;
interrupts();
return result;
}
void resetCounter() {
noInterrupts();
counter = 0;
interrupts();
}
void loop() {
int x = getCounter();
// put your main code here, to run repeatedly:
digitalWrite(ENCODER_SW, HIGH);
if( digitalRead(ENCODER_SW) == LOW){
// lcd_1.clear();
// lcd_1.setCursor(10, 1);
lcd_1.print("HI");
resetCounter();
delay(1000);
}
digitalWrite(LED, LOW);
delay(500);
digitalWrite(LED, HIGH);
// debug("my test: ");
// debugln(myCount);
x = getCounter();
// debug("X is : ");
// debugln(x);
myCount++;
if( myCount >= 10)
{
// debugln("We made 10");
myCount = 0;
}
//int x = myFunction();
}
// #include <Wire.h>
// #include <LiquidCrystal_I2C.h>
// // #include <Adafruit_LiquidCrystal.h>
// #define I2C_DEV_ADDR 0X27 //0x55
// LiquidCrystal_I2C lcd_1(I2C_DEV_ADDR, 20,2);
// // Adafruit_LiquidCrystal lcd_1(0x27);
// int LED = 2;
// int Button = 12;
// int myCount = 0;
// uint32_t i = 0;
// void setup() {
// // put your setup code here, to run once:
// Wire.begin();
// Serial.begin(115200);
// Serial.println("Hello, ESP32!");
// pinMode(LED, OUTPUT);
// pinMode(Button, INPUT_PULLUP);
// //lcd.init();
// lcd_1.backlight();
// lcd_1.begin(16, 2);
// lcd_1.print("hello world :-)");
// }
// void loop() {
// // put your main code here, to run repeatedly:
// digitalWrite(Button, HIGH);
// Serial.println(digitalRead(Button));
// if( digitalRead(Button) == LOW){
// lcd_1.clear();
// lcd_1.setCursor(6,0);
// lcd_1.print("Oops ;-)");
// }
// delay(500); // this speeds up the simulation
// lcd_1.clear();
// lcd_1.setCursor(2,0);
// lcd_1.print("hello :-)");
// lcd_1.setCursor(10,1);
// lcd_1.print(myCount-1);
// delay(3000);
// //"pins": "i2c";
// digitalWrite(LED, LOW);
// delay(500);
// digitalWrite(LED, HIGH);
// Serial.print("my test : ");
// Serial.println(myCount);
// lcd_1.setCursor(10,1);
// lcd_1.print(myCount);
// myCount++;
// if( myCount >= 10)
// {
// Serial.println("We made 10");
// myCount = 0;
// }
// //int x = myFunction();
// }
// int myFunction(){
// byte error, address;
// int nDevices;
// Serial.println("Scanning...");
// nDevices = 0;
// for(address = 1; address < 127; address++ ) {
// Wire.beginTransmission(address);
// Serial.printf("endTransmission: %u\n", error);
// error = Wire.endTransmission();
// if (error == 0) {
// Serial.print("I2C device found at address 0x");
// if (address<16) {
// Serial.print("0");
// }
// Serial.println(address,HEX);
// nDevices++;
// }
// else if (error==4) {
// Serial.print("Unknow error at address 0x");
// if (address<16) {
// Serial.print("0");
// }
// Serial.println(address,HEX);
// }
// }
// if (nDevices == 0) {
// Serial.println("No I2C devices found\n");
// }
// else {
// Serial.println("done\n");
// }
// Wire.printf("Hello World! %lu", i++);
// // uint8_t error = Wire.endTransmission(true);
// delay(5000);
// return(1);
// }