#include <Wire.h>
#include <hd44780.h> // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header
#include <Encoder.h> // https://www.pjrc.com/teensy/td_libs_Encoder.html
#include <Toggle.h> // https://github.com/Dlloydev/Toggle
#include "RTClib.h" // https://github.com/adafruit/RTClib
const uint8_t nbColonnes = 20;
const uint8_t nbLignes = 4;
hd44780_I2Cexp lcd;
RTC_DS1307 rtc;
const char* jours[] = {"Dim", "Lun", "Mar", "Mer", "Jeu", "Ven", "Sam"};
const uint8_t nbJours = sizeof jours / sizeof * jours;
const byte encoderCLKPin = 2;
const byte encoderDTPin = 3;
Encoder encoder(encoderDTPin, encoderCLKPin);
const byte encoderSWPin = 4;
Toggle encoderSwitch(encoderSWPin);
const char lockBitmap[] PROGMEM = {0b01110, 0b10001, 0b10001, 0b11111, 0b11011, 0b11011, 0b11111, 0b00000};
const uint8_t lock = 0;
const char unlockBitmap[] PROGMEM = {0b01110, 0b10000, 0b10000, 0b11111, 0b11011, 0b11011, 0b11111, 0b00000};
const uint8_t unlock = 1;
const char OKBitmap[] PROGMEM = {0b00000, 0b00001, 0b00011, 0b10110, 0b11100, 0b01000, 0b00000, 0b00000};
const uint8_t OK = 2;
const char clocheBitmap[] PROGMEM = {0b00100, 0b01110, 0b01110, 0b01110, 0b11111, 0b00000, 0b00100, 0b00000};
const uint8_t cloche = 3;
long position = 0;
void afficheDate() {
static DateTime avant(1900, 0, 0, 0, 0, 0);
DateTime maintenant = rtc.now();
if (maintenant != avant) {
avant = maintenant;
lcd.setCursor(0, 0);
lcd.print(jours[maintenant.dayOfTheWeek()]); lcd.write(' ');
if (maintenant.day() < 10) lcd.write('0'); lcd.print(maintenant.day()); lcd.write('/');
if (maintenant.month() < 10) lcd.write('0'); lcd.print(maintenant.month()); lcd.write('/');
if (maintenant.year() - 2000 < 10) lcd.write('0'); lcd.print(maintenant.year() - 2000); lcd.write(' ');
lcd.setCursor(nbColonnes - 1, 0);
lcd.write(cloche);
lcd.setCursor(nbColonnes - 8, 1);
if (maintenant.hour() < 10) lcd.write(' '); lcd.print(maintenant.hour()); lcd.write(':');
if (maintenant.minute() < 10) lcd.write('0'); lcd.print(maintenant.minute()); lcd.write(':');
if (maintenant.second() < 10) lcd.write('0'); lcd.print(maintenant.second());
}
}
bool encoderChanged() {
long nouvellePosition = encoder.read() >> 2; // my encoder generates 4 ticks per click so I divide by 4
if (nouvellePosition != position) {
position = nouvellePosition;
return true;
}
return false;
}
void setup() {
encoderSwitch.begin(encoderSWPin);
Serial.begin(115200); Serial.println();
int result = lcd.begin(nbColonnes, nbLignes);
if (result) {
Serial.print("LCD initialization failed: ");
Serial.println(result);
hd44780::fatalError(result);
}
lcd.createChar(lock, lockBitmap);
lcd.createChar(unlock, unlockBitmap);
lcd.createChar(OK, OKBitmap);
lcd.createChar(cloche, clocheBitmap);
if (! rtc.begin()) {
Serial.println("Horloge absente");
Serial.flush();
abort();
}
if (! rtc.isrunning()) {
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
lcd.setCursor(0, 2); lcd.print("cptr : "); lcd.print(position);
lcd.setCursor(0, 3); lcd.print("etat : "); lcd.write(lock);
Serial.println(F("Ready"));
}
void loop() {
afficheDate();
if (encoderChanged()) {
lcd.setCursor(7, 2);
for (byte i = 7; i < nbColonnes; i++) lcd.write(' '); // on efface
lcd.setCursor(7, 2);
lcd.print(position);
}
encoderSwitch.poll();
if (encoderSwitch.onPress()) {
lcd.setCursor(7, 3);
lcd.write(unlock);
}
if (encoderSwitch.onRelease()) {
lcd.setCursor(7, 3);
lcd.write(lock);
}
}
connexions 5V et GND cachées