I am running switch case loop to take the value of setpoint to send it to my AC to change the temperature set point but the loop is contiuosly running what I want to activate the switch loop only if ESP8266 get the new value of setpoint.
here is my code what I m using:
int set = root["set_point"];
switch(set) {
case 31:
Serial.println("SET TEMP 31");
break;
case 30:
Serial.println("SET TEMP 30");
break;
case 29:
Serial.println("SET TEMP 29");
break;
}
}
Please don't tell me that you have a case for each target temperature which in any case only prints a message. If so then you can improve the code a lot by just printing some text and the value of the set variable
It sounds like you only want to print the message once when the value changes. Save the value when you print it and don't print it again unless the next value read is different to the old one.
i am starting as serial print for debug my case and still want to continue the code but i want to know how can run the switch/case loop when value changed
Switch/case is NOT a loop. It just goes straight through.
It's the rest of the code that you didn't show that is doing the looping.
But in general arranging for some code to run only if you get a value that is different from the last time you got that value is just: Save the old value then a simple if statement to check if the new value is different from the saved one. The switch/case really isn't needed UKHeliBob has said.