/*
============================================
code is placed under the MIT license
Copyright (c) 2024 J-M-L
For the Arduino Forum : https://forum.arduino.cc/u/j-m-l
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
===============================================
PIN
0 NC (Rx)
1 NC (Tx)
2 CLK encodeur
3 DT encodeur
4 SW bouton de l'encodeur
5 200Ω --- Led Anode Green led on breadboard
6 200Ω --- Led Anode Green led on breadboard
7
8
9
10
11
12
13
A0
A1
A2
A3
A4 LCD SDA commande du LCD
A5 LCD SCL commande du LCD
*/
#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://www.arduinolibraries.info/libraries/toggle$0
const uint8_t nbCols = 20;
const uint8_t nbRows = 4;
const byte encoderCLKPin = 2;
const byte encoderDTPin = 3;
const byte encoderSWPin = 4;
const byte ledPin1 = 5;
const byte ledPin2 = 6;
hd44780_I2Cexp lcd;
Encoder encoder(encoderDTPin, encoderCLKPin);
Toggle encoderSwitch;
long encoderValue;
const long encoderMinValue = -10;
const long encoderMaxValue = +10;
void blink() {
static unsigned long chrono = 0;
static bool flipflop = true;
unsigned long now = millis();
if (now - chrono >= 500) {
chrono = now;
digitalWrite(ledPin1, flipflop ? HIGH : LOW);
digitalWrite(ledPin2, flipflop ? LOW : HIGH);
flipflop = !flipflop;
}
}
void encoderSetValue(long newValue) {
// as encoders can have multiple ticks, we need to take into account possibles ticks that have been engaged
long ticks = encoder.read() & 0b11;
encoder.write(newValue << 2 + ticks);
}
bool encoderChanged() {
long newPosition = encoder.read() >> 2; // divide by 4 as the rotary sends 4 ticks per click
if (newPosition < encoderMinValue) {
newPosition = encoderMinValue;
encoderSetValue(encoderMinValue);
} else if (newPosition > encoderMaxValue) {
newPosition = encoderMaxValue;
encoderSetValue(encoderMaxValue);
}
if (newPosition != encoderValue) {
encoderValue = newPosition;
return true;
}
return false;
}
void testSwitch() {
encoderSwitch.poll();
if (encoderSwitch.onPress()) {
Serial.println("PRESSED");
}
}
void testEncoder() {
if (encoderChanged()) Serial.println(encoderValue);
}
void setup() {
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
encoderSwitch.begin(encoderSWPin);
Serial.begin(115200);
int result = lcd.begin(nbCols, nbRows);
if (result) {
Serial.print("LCD initialization failed: ");
Serial.println(result);
hd44780::fatalError(result);
}
lcd.clear();
lcd.setCursor(5, 0); //Defining positon to write from first row, first column .
lcd.print("CHARCUPRO");
lcd.setCursor(3, 2);
lcd.print("Version 1.1.1");
lcd.setCursor(0, 2); //Second row, first column
lcd.print("");
delay(1000);
lcd.clear(); //clear the whole LCD
lcd.setCursor(1, 1);
lcd.print("TEMP"); //text
//----------------------
lcd.setCursor(1, 3);
lcd.print("HUMI"); //text
//----------------------
lcd.setCursor(12, 1);
lcd.print("VENT"); //text
//----------------------
lcd.setCursor(12, 3);
lcd.print("UV"); //text
//----------------------
lcd.setCursor(8, 1);
lcd.print((char)223); //signe de degrees
//----------------------
lcd.setCursor(9, 1);
lcd.print("C"); //signe de degrees
//----------------------
lcd.setCursor(9, 3);
lcd.print("%"); //text
//----------------------
lcd.setCursor(17, 1);
lcd.print("off"); //text
//----------------------
lcd.setCursor(17, 3);
lcd.print("off"); //text
}
void loop() {
testEncoder();
testSwitch();
blink();
}