Hey, I'm trying to build a Dehydrator with Arduino as my first self-sufficient Arduino project (go easy on me :'D) and I wanted to use a KY-040 to set a temperature value which is also printed onto an LCD. Everything works except for the recognition of turns from the rotary encoder and accordingly the "Soll." (set temperature) Value doesn't rises.
I found a few routines of a KY-040 in use that were to complicated for my understanding and accordingly i couldn't frankenstein the usefull parts into my patch.
I based mine on this KY-040 code example
https://www.linkerkit.de/index.php?title=KY-040_Kodierter_Drehschalter_(Rotary_Encoder)
It prints into the serial monitor, but its easy to comprehend and I thought I made a good job adjusting it to my situation but obviously that wasn't the case.
Anyway I would like to stick to this procedure if possible because its easy to comprehend and I prefer understanding the stuff that I'm doing and not just copy-paste magic into my sketch that I have no clue of.
Anyway here is my Sketch, thanks for help in advance (sorry for the german worde here and there):
// Doofenschmirtz Dörrinator
// Mads Duggen
// Interactive Prototyping 2021
// Muthesius Kunsthochschule
// ARDUINO PIN USAGE
// LCD with I2C module
// A5 - SCL
// A4 - SDA
// 5v - VCC
// GND - GND
// DHT 11 sensor
// 8 - OUTPUT
// 5v - VCC
// GND - GND
// Heater and fan relay CH1 & CH2
// 10 - CH1
// 11 - CH2
// 5v - DC+
// GND - DC-
// KY-040 rotary encoder
// 3 - CLK
// 4 - DT
// 5 - SW
// 5v - +
// GND - GND
//Utilised libarys
#include <DHT.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
// Data-Pin and sensortype
#define DHTPIN 8
#define DHTTYPE DHT11
// KY-040 pins
int CLK = 3;
int DT = 4;
int SW = 5;
int solltemperatur;
int temperatur;
float luftfeuchtigkeit;
int clkletzter;
int clkaktuell;
// LCD und DHT objects
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
// initialising relay pins
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
// initialising KY-040 pins
pinMode(CLK,INPUT);
pinMode(DT,INPUT);
pinMode(SW,INPUT);
digitalWrite(CLK, true);
digitalWrite(DT, true);
digitalWrite(SW, true);
//initial read CLK pin
clkletzter = digitalRead(CLK);
// Welcoming Screen
dht.begin(); // initialising DHT11 sensor
lcd.backlight(); // Start backlight
lcd.begin(); // initialising LCD
delay(2000);
lcd.setCursor( 0, 0);
lcd.print(" Doofenschmirtz");
lcd.setCursor( 0, 1);
lcd.print(" Doerrinator");
delay(5000);
}
void loop() {
// Read temperature und humidity from DHT11 and Print to LCD
solltemperatur = 0; // The manually set temperature
temperatur = dht.readTemperature(); // The environment temperature
luftfeuchtigkeit = dht.readHumidity(); // The environment humidity (not in use atm)
lcd.clear();
// read the "now" value
clkaktuell = digitalRead(CLK);
// notice turns
if(clkaktuell != clkletzter) {
// count up if CLK is triggered first
if(digitalRead(DT) != clkaktuell){
solltemperatur ++;
}
// count down if DT is triggered first
else {
solltemperatur --;
}
}
// low end limit of set temperature
if(solltemperatur < 25) {
solltemperatur = 25;
}
// high end limit of set temperature
if(solltemperatur > 70) {
solltemperatur = 70;
}
//print the environment temperature to the first row of the LCD
lcd.setCursor( 0, 0);
lcd.print("Temp. :");
lcd.print(temperatur);
lcd.print("C");
//print the environment humidity to the second row of the LCD
lcd.setCursor( 0, 1);
lcd.print("Soll. :");
lcd.print(solltemperatur); // Changeable Value representing the set temperature of the Dehydrator
lcd.print("C");
// turn the relay on if the environment temperature is lower
// than the set temperature
if(solltemperatur < temperatur){
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
}
// turn of if not
else {
digitalWrite(10, LOW);
digitalWrite(11, LOW);
}
// update Sequenz DHT11
delay(2000);
lcd.clear();
}