// GM IAT Controller JKW 2021
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Stepper.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2
#define BACKLIGHT_PIN 3
const int stepsPerRevolution = 255;
Stepper myStepper(stepsPerRevolution, 4, 5, 6, 7);
int stepCount = 0;
int Step = 0;
int MinSteps = 3;
int prog = 0;
int istep = 0;
int maxtmp = 180;
int potpin = 0;
unsigned long myTime;
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display 0X26 for 20 by 4 lcd.
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
void iprog() {
istep = map(stepCount,0,255,15,0);
lcd.setCursor(0,0);
lcd.print("Initializing!");
lcd.setCursor(0,1);
switch (istep) {
case 1:
lcd.print("=> ");
break;
case 2:
lcd.print("==> ");
break;
case 3:
lcd.print("===> ");
break;
case 4:
lcd.print("====> ");
break;
case 5:
lcd.print("=====> ");
break;
case 6:
lcd.print("======> ");
break;
case 7:
lcd.print("=======> ");
break;
case 8:
lcd.print("========> ");
break;
case 9:
lcd.print("=========> ");
break;
case 10:
lcd.print("==========> ");
break;
case 11:
lcd.print("===========> ");
break;
case 12:
lcd.print("============> ");
break;
case 13:
lcd.print("=============> ");
break;
case 14:
lcd.print("==============> ");
break;
case 15:
lcd.print("===============>");
break;
}
}
void homeit() {
maxtmp = analogRead(potpin);
lcd.clear();
lcd.setBacklight(HIGH);
stepCount = 0;
while (stepCount < 255) {
iprog();
myStepper.step(1);
Serial.print("steps:");
Serial.println(stepCount);
stepCount++;
}
}
void setup() {
Serial.begin(9600);
Wire.begin();
lcd.begin (16,2); // <<----- LCD 16x2
lcd.clear();
sensors.begin();
delay(2000);
homeit();
lcd.setBacklight(HIGH);
myTime = millis();
}
void progressBar() {
prog = map(Step,0,255,6,1);
lcd.setCursor(9,1);
switch (prog) {
case 1:
lcd.print("=> ");
break;
case 2:
lcd.print("==> ");
break;
case 3:
lcd.print("===> ");
break;
case 4:
lcd.print("====> ");
break;
case 5:
lcd.print("=====> ");
break;
case 6:
lcd.print("======>");
break;
}
}
void dhtwork() {
sensors.requestTemperatures();
float f = sensors.getTempFByIndex(0);
if (isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
maxtmp = analogRead(potpin);
maxtmp = map(maxtmp, 1, 1022, 20, 180);
Step = constrain(map(f, -20, maxtmp, 255, 0), 0, 255);
Serial.print(F("MaxTemp: "));
Serial.print(maxtmp);
Serial.print(F(" Temperature: "));
Serial.print(f);
Serial.print(" ");
lcd.setCursor(0,0);
lcd.print("T:");
lcd.print(f);
lcd.print(" ");
lcd.setCursor(9,0);
lcd.print("Max:");
lcd.print(maxtmp);
lcd.print(" ");
}
void loop() {
dhtwork();
setit();
delay(25);
if ( millis() > myTime + 125000 ) {
lcd.setBacklight(LOW);
}
}
void disableStepper() {
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
}
void setit() {
// added some histerysis, so were looking for at leats 3 degrees before a change.
if (Step > stepCount + MinSteps ) {
lcd.setBacklight(HIGH);
myTime = millis();
openit();
}
if (Step < stepCount - MinSteps ) {
lcd.setBacklight(HIGH);
myTime = millis();
closeit();
}
Serial.print("Calculated step:");
Serial.println(Step);
lcd.setCursor(0,1);
lcd.print("Step ");
lcd.print(Step);
if ( Step >= 100 ) {
lcd.print(" ");
} else {
lcd.print(" ");
}
delay(5);
progressBar();
}
void openit() {
while (stepCount - MinSteps < Step) {
myStepper.step(1);
stepCount++;
delay(5);
disableStepper(); // only for locking style iac, if power is needed to prevent it moving comment out.
}
}
void closeit() {
while (stepCount + MinSteps > Step) {
myStepper.step(-1);
stepCount--;
delay(5);
disableStepper();
}
}
Comments