Hi friends. I want to show air quality value in value widget but it’s some conflicting showing the value.
the code:
void air()
{
if (analogRead(A0) <= 250){
Blynk.virtualWrite(V16, "GOOD");
}
else if (analogRead(A0) >= 300)
Blynk.virtualWrite(V16, "NORMAL");
if (analogRead(A0) >= 350){
Blynk.virtualWrite(V16, "ABNORMAL");
}
else if (analogRead(A0) > 400)
Blynk.virtualWrite(V16, "BAD");
}
the problem: when A0 value is higher than 350 then not showing ABNORMAL and showing NORMAL value and when the A0 is higher than 400 only showing ABNORMAL and NORMAL
Look into some sites that teach programming… then you can do simple coding and catch errors like this without needing to ask all the time (and quicker then waiting )
Learning Programming or Blynk is no different then qualifying for your “Network engineer. CISCO CCIE”… study, study, study
you do not believe it, but my work is such that I do not have time to focus on programming . yeah it’s very bad for a network engineer can not be solving the simple programming errors. I need more times to focusing on it
@ErfanDL There’s a problem with your logic for the if statements. There are gaps where there are no criteria to fit certain readings. For example, values between 251 and 300 and a value of 400 have no matching if criteria.
Personally, I prefer to use a Switch Case statement with ranges rather than if statements. Here’s a re-work of @Toro_Blanco’s code to use Switch Case (and fixing the gaps in your criteria).
void air()
{
int airQuality = analogRead(A0);
Serial.println(airQuality);
switch (airQuality)
{
case 1 ... 250:
Blynk.virtualWrite(V16, "GOOD");
break;
case 251 ... 300:
Blynk.virtualWrite(V16, "NORMAL");
break;
case 351 ... 400:
Blynk.virtualWrite(V16, "ABNORMAL");
break;
case 401 ... 999:
Blynk.virtualWrite(V16, "BAD");
break;
default:
Blynk.virtualWrite(V16, airQuality);
break;
}
}
The default option at the end will print the analogue read value to Blynk if its outside the range of 1 to 999. Not sure if it’s needed, but could help to debug dodgy readings.