#include "PinChangeInterrupt.h"
#include "AiEsp32RotaryEncoder.h"
#include "Arduino.h"
#include <Wire.h> // include the Wire library for I2C communication
#include <RTClib.h> // include the RTClib library for accessing the RTC module
#include <Adafruit_SSD1306.h> // include the Adafruit_SSD1306 library for the OLED display
// #include <Adafruit_GFX.h> // Core graphics library
// #include <Adafruit_TFTLCD.h> // Hardware-specific library
// #include <Fonts/FreeSans9pt7b.h>
RTC_DS3231 rtc; // create an RTC object
Adafruit_SSD1306 display(128, 64, &Wire, -1); // create an OLED display object
#define CLK 2
#define DT 3
#define SW 4
float counter = 65;
int currentState;
int initState;
unsigned long debounceDelay = 0;
bool isPressed = 0;
void setup() {
pinMode(CLK, INPUT);
pinMode(DT, INPUT);
pinMode(SW, INPUT_PULLUP);
// Setup Serial Monitor
Serial.begin(9600);
// Read the initial state of CLK
initState = digitalRead(CLK);
// Call encoder_value() when any high/low changed seen
// on interrupt 0 (pin 2), or interrupt 1 (pin 3)
attachInterrupt(0, encoder_value, CHANGE);
attachInterrupt(1, encoder_value, CHANGE);
attachPCINT(digitalPinToPCINT(SW), button_press, CHANGE);
Wire.begin(); // start the I2C communication
rtc.begin(); // start the RTC module
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // start the OLED display
display.clearDisplay(); // clear the display buffer
display.setTextSize(2); // set the text size to 2
display.setTextColor(WHITE); // set the text color to white
// display.setFont(&FreeSans9pt7b);
}
void loop()
{
DateTime now = rtc.now(); // get the current date and time from the RTC module
display.clearDisplay(); // clear the display buffer
display.setCursor(0, 0); // set the cursor position to (0,0)
display.println("Date: "); // print the text "Date: "
display.print(now.day(), DEC); // print the day of the month
display.print("/"); // print a slash separator
display.print(now.month(), DEC); // print the month
display.print("/"); // print a slash separator
display.println(now.year(), DEC); // print the year
display.setCursor(18, 40);
display.print(counter);
display.print(" kg");
display.display();
Serial.print(isPressed);
isPressed == 1? display.invertDisplay(true):display.invertDisplay(false);
delay(10); // wait for one second before updating the display again
}
void button_press()
{
int buttonVal = digitalRead(SW);
//If we detect LOW signal, button is pressed
if (isPressed == 0 && buttonVal == LOW) {
if (millis() - debounceDelay > 200) {
Serial.println("Button pressed!");
isPressed = true;
}
debounceDelay = millis();
} else if (isPressed == 1 && buttonVal == LOW) {
if (millis() - debounceDelay > 200) {
Serial.println("Button pressed!");
isPressed = false;
}
}
}
void encoder_value() {
// Read the current state of CLK
currentState = digitalRead(CLK);
// If last and current state of CLK are different, then we can be sure that the pulse occurred
if (currentState != initState && currentState == 1) {
// Encoder is rotating counterclockwise so we decrement the counter
if (digitalRead(DT) != currentState) {
counter += 0.05;
} else {
// Encoder is rotating clockwise so we increment the counter
counter -= 0.05;
}
// print the value in the serial monitor window
Serial.print("Counter: ");
Serial.println(counter);
}
// Remember last CLK state for next cycle
initState = currentState;
}
Loading
ssd1306
ssd1306