BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Set range number correctly


#1

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

is any solution ?

thanks


#2

Maybe try this

void air()
{
  int airQuality = analogRead(A0);
  Serial.println(airQuality);

  if ((airQuality > 0)  && (airQuality  <= 250)){
    Blynk.virtualWrite(V16, "GOOD");
  }
    else if ((airQuality > 300)  && (airQuality  <= 349)){
    Blynk.virtualWrite(V16, "NORMAL");
    }
  else if ((airQuality > 350)  && (airQuality  <= 399)){
    Blynk.virtualWrite(V16, "ABNORMAL");
  }
    else if (airQuality  > 400) {
    Blynk.virtualWrite(V16, "BAD");
   }
}

Or it may be due to missing brackets on your else if statements.


EDITED to include range values


#3

Hmm. Thanks I will trying. Do you know how to set a range number in this code? for example when value between 300 to 399 showing ABNORMAL


#4

Maybe like this

if ((airQuality > 0)  && (airQuality  <= 250)){
    Blynk.virtualWrite(V16, "GOOD");
  }

I see how the above code would cause conflicts. If it is 425 than 3 of the 4 statements become true. Therefore a range is needed as you mentioned.

I will edit my original post.


#5

thanks. but when compiling getting error

Arduino: 1.8.5 (Windows 10), Board: "NodeMCU 1.0 (ESP-12E Module), 160 MHz, Flash, 4M (3M SPIFFS), v2 Lower Memory, Disabled, None, All Flash Contents, 115200"

C:\Users\ERFAN\Documents\Arduino\sketch_aug17a\sketch_aug17a.ino: In function 'void air()':

sketch_aug17a:121: error: expected primary-expression before 'if'

   if (if ((airQuality > 0)  && (airQuality  <= 250))){

       ^

sketch_aug17a:121: error: expected ')' before 'if'

sketch_aug17a:124: error: expected primary-expression before 'if'

     else if (if ((airQuality > 300)  && (airQuality  <= 349))){

              ^

sketch_aug17a:124: error: expected ')' before 'if'

sketch_aug17a:127: error: expected primary-expression before 'if'

   else if (if ((airQuality > 350)  && (airQuality  <= 399))){

            ^

sketch_aug17a:127: error: expected ')' before 'if'

exit status 1
expected primary-expression before 'if'

#6

sorry that was a copy and paste error. Should be fixed now.


#7

thanks Toro you saved my night :heart_eyes: I will never forget this kindness. it’s working perfectly


#8

No worries, Glad to help.


#9

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 :stuck_out_tongue_winking_eye: )

Learning Programming or Blynk is no different then qualifying for your “Network engineer. CISCO CCIE”… study, study, study :smiley:

I use this link all the time…

https://www.arduino.cc/reference


#10

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

:rose::rose::rose:


#11

@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.

Pete.


#12

Just a simple oversight on the values. I was trying to use the values he had used in his original post.

see correction below using values shown in your Switch Case, and adding a default option as you had as well.

void air()
{
  int airQuality = analogRead(A0);
  Serial.println(airQuality);

  if ((airQuality > 0)  && (airQuality <= 250)){
    Blynk.virtualWrite(V16, "GOOD");
  }
    else if ((airQuality > 250)  && (airQuality  < 350)){
    Blynk.virtualWrite(V16, "NORMAL");
    }
  else if ((airQuality >= 350)  && (airQuality  < 400)){
    Blynk.virtualWrite(V16, "ABNORMAL");
   }
    else if (airQuality >= 400 && (airQuality <= 999)) {
    Blynk.virtualWrite(V16, "BAD");
   }
   else {
   Blynk.virtualWrite(V16, airQuality);
  }
}

I think this is a great example of how you can accomplish the same task with different methods of coding. Thanks @PeteKnight for highlighting this.

P.S. there is gap in your Swicth Case between 301 and 350.