i am in the last stage of my DieselHeaterEnhancer. It will controll a DieselHeater with various options. I created a Menu for these options. To change the Values (mostly the time) i use a rotary encoder.
So far everything works fine!
The only thing is that the rotary encoder is glitchy. You have to turn it very slowly for it to register the turn. Also it counts every turn as 2 turns. So it adds or subtracts twice the value it should.
Do you have any tips to how i can improve the coder?
Thx!
/**
Name: DiselHeaterController
Purpose: Controlling a Diesel Heater
@author Peter Kirsch
@version 1.0 20/10/2023
*/
//RotaryEncoder
#define inputCLK 2
#define inputDT 5
int aState;
int aLastState;
//TimerFunktion
int counter = 30;
//CountDownFunktion
float countDown = 60;
//RotaryEncoderSwitch
#define inputSW 4
int sw; //Switch
int swValue;
int lastSwValue = 0;
//Button
#define button 7
int buttonVal;
int lastButtonVal = 0;
int menuVal = 1;
int buttonPeriod = 100;
//SerialPrint intervall
int serialPrintPeriod = 1000;
//Screen
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
//Heater
int relayPin = 6; //relayPin Pin
int HeaterButton = 4000; // Length of the HeaterControllerButton necessary to be HIGH
//millis
unsigned long timeOld;
//HeaterThermostate
bool heaterThermo = 0;
//DS18B20
// Include the libraries we need
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 3
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
float tempC;
//Ob Heizun An oder Aus ist
bool heaterVal = 0;
void setup() {
Serial.begin(9600);
pinMode(inputCLK, INPUT);
pinMode(inputDT, INPUT);
pinMode(inputSW, INPUT_PULLUP); //SwitchPin
pinMode(relayPin, OUTPUT); //relayPin
digitalWrite(relayPin, HIGH); //relayPin
pinMode(button, INPUT); //ButtonPin
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //DisplayStart
display.clearDisplay();
display.display(); //Update/WriteToDisplay
aLastState = digitalRead(inputCLK);
sensors.begin(); //DS18B20Start
}
void loop() {
//Reading DS18B20
ReadingTemp();
//Serial.Print
if (millis() - timeOld >= serialPrintPeriod) {
Serial.print("buttonVal ");
Serial.println(buttonVal);
Serial.print("menuVal ");
Serial.println(menuVal);
Serial.print("counter ");
Serial.println(counter);
Serial.print("CountDown ");
Serial.println(countDown);
timeOld = millis();
}
//Buttonfunktion
checkButton();
//Heater ON & OFF
while (menuVal == 1) {
//Buttonfunktion
checkButton();
//Reading DS18B20
sensors.requestTemperatures(); // Send the command to get temperatures
float tempC = sensors.getTempCByIndex(0); // We use the function ByIndex, and as an example get the temperature from the first sensor only.
//DisplayText
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println("Heizung An & Aus");
display.println("----------");
display.println("Schaltet die Heizung an und nach ");
display.print(counter);
display.println(" Minuten ");
display.println("---------");
display.print("wieder aus.");
display.setCursor(88, 57);
display.print(tempC, 1);
display.println(" C");
display.display();
//Rotary
aState = digitalRead(inputCLK);
if (aState != aLastState) {
if (digitalRead(inputDT) != aState) {
counter = counter - 5;
} else {
counter = counter + 5;
}
}
aLastState = aState;
//Timer Funktion
sw = digitalRead(inputSW);
if (sw == 0) {
Serial.println(counter);
//HeaterButton
digitalWrite(relayPin, LOW);
delay(HeaterButton);
digitalWrite(relayPin, HIGH);
//delay till heater on/off
delay(counter * 60ul * 1000ul);
//HeaterButton
digitalWrite(relayPin, LOW);
delay(HeaterButton);
digitalWrite(relayPin, HIGH);
}
}
//Heater OFF
while (menuVal == 2) {
//Buttonfunktion
checkButton();
//Reading DS18B20
sensors.requestTemperatures(); // Send the command to get temperatures
float tempC = sensors.getTempCByIndex(0); // We use the function ByIndex, and as an example get the temperature from the first sensor only.
//DisplayText
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println("Heizung Aus");
display.println("----------");
display.println("Schaltet die Heizung nach ");
display.print(counter);
display.println(" Minuten ");
display.println("---------");
display.print("aus.");
display.setCursor(88, 57);
display.print(tempC, 1);
display.println(" C");
display.display();
//Rotary
aState = digitalRead(inputCLK);
if (aState != aLastState) {
if (digitalRead(inputDT) != aState) {
counter = counter - 5;
} else {
counter = counter + 5;
}
}
aLastState = aState;
//Timer Funktion
sw = digitalRead(inputSW);
if (sw == 0) {
Serial.println(counter);
//delay till heater on/off
delay(counter * 60ul * 1000ul);
//HeaterButton
digitalWrite(relayPin, LOW);
delay(HeaterButton);
digitalWrite(relayPin, HIGH);
}
}
//Heater Delay ON&OFF
while (menuVal == 3) {
//Buttonfunktion
checkButton();
//Reading DS18B20
sensors.requestTemperatures(); // Send the command to get temperatures
float tempC = sensors.getTempCByIndex(0); // We use the function ByIndex, and as an example get the temperature from the first sensor only.
//DisplayText
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println("Heizung An");
display.println("----------");
display.println("Schaltet die Heizung nach ");
if (countDown <= 60) {
display.print("-> ");
display.print(countDown);
display.print(" <-");
display.println(" Minuten ");
} else {
display.print("-> ");
display.print(countDown / 60, 1);
display.print(" <-");
display.println(" Stunden ");
}
display.println("an und nach");
display.print("-> ");
display.print(counter);
display.print(" <-");
display.print(" Minuten ");
display.print("wieder aus.");
//Temperatur in der rechten unteren Ecke
display.setCursor(88, 57);
display.print(tempC, 1);
display.println(" C");
display.display();
//Rotary
aState = digitalRead(inputCLK);
if (aState != aLastState) {
if (digitalRead(inputDT) != aState) {
countDown = countDown - 15;
} else {
countDown = countDown + 15;
}
}
aLastState = aState;
//SW && Timer Funktion
sw = digitalRead(inputSW);
if (sw == 0) {
Serial.println(countDown);
//delay till heater on
delay(countDown * 60ul * 1000ul);
digitalWrite(relayPin, LOW);
delay(HeaterButton);
digitalWrite(relayPin, HIGH);
//delay till heater on/off
delay(counter * 60ul * 1000ul);
//HeaterButton
digitalWrite(relayPin, LOW);
delay(HeaterButton);
digitalWrite(relayPin, HIGH);
//Buttonfunktion
checkButton();
}
}
//Heater Delay ON&OFF
while (menuVal == 4) {
//Buttonfunktion
checkButton();
//Reading DS18B20
sensors.requestTemperatures(); // Send the command to get temperatures
float tempC = sensors.getTempCByIndex(0); // We use the function ByIndex, and as an example get the temperature from the first sensor only.
//DisplayText
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println("Thermostate");
display.println("----------");
display.println("Heizung Aus/An = 0/1");
display.setCursor(62, 40);
display.print(heaterVal);
display.setCursor(55, 45);
display.print("---");
display.setCursor(88, 57);
display.print(tempC, 1);
display.println(" C");
display.display();
//Rotary heaterVal
aState = digitalRead(inputCLK);
if (aState != aLastState) {
if (digitalRead(inputDT) != aState) {
heaterVal = 0;
} else {
heaterVal = 1;
}
}
aLastState = aState;
//SW Funktion
sw = digitalRead(inputSW);
if (sw == 0) {
while (menuVal == 4) {
if (tempC >= 15 && heaterVal == 1) {
//HeaterButton
digitalWrite(relayPin, LOW);
delay(HeaterButton);
digitalWrite(relayPin, HIGH);
heaterVal = 0;
}
if (tempC <= 5 && heaterVal == 0) {
//HeaterButton
digitalWrite(relayPin, LOW);
delay(HeaterButton);
digitalWrite(relayPin, HIGH);
heaterVal = 1;
}
//Buttonfunktion
checkButton();
//Reading DS18B20
sensors.requestTemperatures(); // Send the command to get temperatures
float tempC = sensors.getTempCByIndex(0); // We use the function ByIndex, and as an example get the temperature from the first sensor only.
}
}
}
}