Hey all, I'm trying to make a dial using the rotary encoder that comes with the mega2560 starter kit that controls the 'temperature' of a fake hot plate that I'm making for an escape room.
The intent of this is to control the temperature (determined from the dial spinning clockwise or counter clockwise) of the hot plate, and once the desired temperature (20 in this case) is reached and dial stays at that temperature for a few seconds another light turns on.
Now my issue is that I made a timer using an increment and a integer called "T" and everything works fine until I comment out the two Serial.println(T) inside the while loop. Once ran like this, and the temperature reaches 20 the ledMelt turns on with no delay.
I didn't want to use the delay function as I want it to detect if the dial is turned while at the desired temperature as this is a prop and I don't want someone who's just spinning the dial to get this effect of ledMelt. I'm aware I could just keep the Serial.println uncommented and move on, but I plan on adding more stuff later on and I'd like to clean up the serial monitor a bit for the future.
Additionally, it is day two of coding, please be kind
#define inputCLK 4
#define inputDT 5
#define ledHeat 9
#define ledMelt 11
int I;
int T;
int MP=25;
int Temp=0;
int currentStateCLK;
int lastStateCLK;
String encdir="";
void setup() {
pinMode(inputCLK,INPUT);
pinMode(inputDT,INPUT);
pinMode(ledHeat,OUTPUT);
pinMode(ledMelt, OUTPUT);
Serial.begin(9600);
lastStateCLK=digitalRead(inputCLK);
}
//end of setup
void loop() {
currentStateCLK=digitalRead(inputCLK);
if(currentStateCLK!=lastStateCLK){
if(digitalRead(inputDT) != currentStateCLK){
Temp--;
encdir="cooling...";
//fade led to lower power here
}
else{
Temp ++;
encdir= "Heating...";
//fade led to higher power here
digitalWrite(ledHeat,HIGH);
}
Serial.print("Temperature: ");
Serial.print(encdir);
Serial.print(Temp);
Serial.println(" Degrees ");
}
while(currentStateCLK==lastStateCLK and Temp==20){
if(digitalRead(inputDT) != lastStateCLK){
T=0;
break;
}
if(digitalRead(inputCLK) != lastStateCLK){
T=0;
break;
}
if(T == 1000){
int I=0;
T=T+I;
//Serial.println(T);
digitalWrite(ledMelt, HIGH);
break;
}
else{
int I=1;
T=T+I;
//Serial.println(T);
}
//end of while loop
}
if(digitalRead(ledMelt)==HIGH and digitalRead(inputCLK!=lastStateCLK)){
digitalWrite(ledMelt, LOW);
}
lastStateCLK=currentStateCLK;
}