Hello everyone!
I have already received some help on this to get things up and running, but now I need to clean this mess up! Also the last bit doesn't work as I want it to.
I will first explain what this code is supposed to do:
Setup():
- User is asked to select (with poti) the amount of required "steps"
- User pushes button
- The selected amount of "steps" is saved to a variable
- Depending on the amount of "steps" the user is asked to enter the Temperatures for these "steps"
- They will get stored into an array
- Next the user is asked to enter the Time for these "steps"
- They will get stored into a different array
Loop():
We now have all the required information. Once the button is pushed the main programm will start.
Now here comes the tricky part. I have connected a DS18B20 Digital temperature sensor to monitor the temperature at all times.
- Let´s say the temperature for "step" 1 is supposed to be 30°C but the actual temperature is 28°C...
The container will be externally heated until it reaches the "step" 1 temperature. - As soon as this temperature is reached, a timer has to be started with the time that was supplied in the setup().
- Once the time has run out, the index will be increased and we switch to the next "step".
- Here we repeat as before, we heat up until desired temperature has been met and then start a new timer.
Also, if the timer has been started and the actual temperature drops BELOW the desired temperature, we only want to start heating again, the timer should remain running!
Currently my code is really really messy and I would be thankful if someone could take a look at it. Especially the setup() could need a little cleaning. I would like to somehow combine the 3 while loops if possible at all.
The timers still aren't working, could someone also help me on that one?
Thank you so much!
Code:
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define ONE_WIRE_BUS 3
#define arraySize 5
#define potPin 0
#define buttonPin 2
int Rs_pin = 0;
int Rw_pin = 1;
int En_pin = 2;
int Bl_pin = 3;
int D4_pin = 4;
int D5_pin = 5;
int D6_pin = 6;
int D7_pin = 7;
int I2C_ADDR = 0x27;
int bootTime = 0;
int buttonState = 0;
int lastButtonState = 0;
int index = 0;
int amountSteps = 0;
long previousMillis = 0;
int startCountdown = false;
int stepTemperature[arraySize];
int stepTime[arraySize];
boolean menu = true;
boolean startStep = false;
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,
Rs_pin,D4_pin,D5_pin,
D6_pin,D7_pin,Bl_pin,POSITIVE);
DeviceAddress insideThermometer = { 0x28, 0x4A, 0x8D, 0x60, 0x05, 0x00, 0x00, 0x26 };
void setup(void) {
Serial.begin(115200);
sensors.begin();
sensors.setResolution(insideThermometer, 12);
lcd.begin(16,2);
pinMode(buttonPin, INPUT);
while(menu) {
buttonState = digitalRead(buttonPin);
lcd.setCursor(0, 0);
lcd.print("Amount Steps?");
lcd.setCursor(14, 1);
lcd.print(map(analogRead(potPin), 0, 1000, 5, 1));
if (buttonState != lastButtonState) {
if (buttonState == 1) {
amountSteps = map(analogRead(potPin), 0, 1000, 5, 1);
menu = false;
lcd.clear();
}
lastButtonState = buttonState;
}
delay(20);
}
lcd.clear();
index = 0;
while(index < amountSteps) {
buttonState = digitalRead(buttonPin);
lcd.setCursor(0, 0);
lcd.print(index + 1);
lcd.setCursor(1, 0);
lcd.print(". Step");
lcd.setCursor(8, 0);
lcd.print((char)223);
lcd.setCursor(9, 0);
lcd.print("C =");
lcd.setCursor(14, 0);
if (map(analogRead(potPin), 0, 1023, 90, 0) < 10) {
lcd.print(" ");
}
lcd.print(map(analogRead(potPin), 0, 1023, 90, 0));
if (buttonState != lastButtonState) {
if (buttonState == 1) {
stepTemperature[index] = map(analogRead(potPin), 0, 1023, 90, 0);
index++;
}
lastButtonState = buttonState;
}
delay(20);
}
lcd.clear();
index = 0;
while(index < amountSteps) {
buttonState = digitalRead(buttonPin);
lcd.setCursor(0, 0);
lcd.print(index + 1);
lcd.setCursor(1, 0);
lcd.print(". Step");
lcd.setCursor(8, 0);
lcd.print("Min =");
lcd.setCursor(14, 0);
if (map(analogRead(potPin), 0, 1023, 90, 0) < 10) {
lcd.print(" ");
}
lcd.print(map(analogRead(potPin), 0, 1023, 90, 0));
if (buttonState != lastButtonState) {
if (buttonState == 1) {
stepTime[index] = map(analogRead(potPin), 0, 1023, 90, 0);
index++;
}
lastButtonState = buttonState;
}
delay(20);
}
lcd.clear();
}
void loop(void) {
buttonState = digitalRead(buttonPin);
getTemperature(true);
if (buttonState != lastButtonState) {
if (buttonState == 1) {
startStep = true;
lcd.clear();
}
lastButtonState = buttonState;
}
delay(20);
index = 0;
if (startStep) {
while (index < amountSteps) {
unsigned long currentMillis = millis();
getTemperature(true);
Serial.print("Step ");
Serial.print(index + 1);
Serial.println(" active!");
if (getTemperature(false) < stepTemperature[index]) {
Serial.println("Temperature has not been reached!");
} else {
Serial.println("Temperature reached!");
bootTime = millis();
startCountdown = true;
}
Serial.println(currentMillis);
Serial.println(previousMillis);
if(startCountdown && (currentMillis - previousMillis > ((500 * 10) + bootTime))) {
previousMillis = currentMillis;
startCountdown = false;
bootTime = 0;
index++;
}
}
startStep = false;
}
}
float getTemperature(bool printTemperature) {
sensors.setWaitForConversion(false);
sensors.requestTemperatures();
sensors.setWaitForConversion(true);
float tempC = sensors.getTempC(insideThermometer);
if (printTemperature) {
lcd.setCursor(0, 0);
lcd.print("Temp C:");
lcd.setCursor(8, 0);
lcd.print(tempC);
}
return tempC;
}