else if stacking

Hi, I am a yr.11 making a rainsensor for my electronics porduct.

I want my product to be able to tell me, when its raining and how heavy the fall is.

Major Components:

  • Arduino Uno eleven
  • MH-RD raindrops module (flying fish)
  • LED (x8)

Steps:

  • no water = all led's low
  • spitting = green led's high
  • slight rain = yellow led's high, green low
  • rain = orange led's high, yellow & green low
  • heavy rain = red led's high, orange, yellow & green led's low.

The rain module gives you values on the AO between 1060~ to 50~, therefore, I have been trying to use if else statements and the AO values in =>, etc. But I keep getting an error message saying that my if else statements don't have a previous if. I would really appreciate your help.

Rough layout and code documents attached.

Kind Regards,
Anonymous.

Rainsensor_test_3_all_LED_s.ino (1.82 KB)

From @ethan_school20's Rainsensor_test_3_all_LED_s.ino:

 if(analogRead(capteur_A) <= 650);
{
 Serial.println("Digital value : It's rainin mf!");
 digitalWrite(12, HIGH);
 digitalWrite(11, HIGH);
 digitalWrite(10, LOW);
 digitalWrite(9, LOW);
 digitalWrite(8, LOW);
 digitalWrite(7, LOW);
 digitalWrite(6, LOW);
 digitalWrite(5, LOW);
 delay(10);
}
 else if (analogRead(capteur_A) <= 800);
{
  Serial.println("Digital value : Spitting");
  digitalWrite(12, LOW);
  digitalWrite(11, LOW);
  digitalWrite(10, HIGH);
  digitalWrite(9, HIGH);
  digitalWrite(8, LOW);
  digitalWrite(7, LOW);
  digitalWrite(6, LOW);
  digitalWrite(5, LOW);
  delay(10);
}
 else if (analogRead(capteur_A) <= 900);
{
 Serial.println("Digital value : Kinda Dry");
 digitalWrite(12, LOW);
 digitalWrite(11, LOW);
 digitalWrite(10, LOW);
 digitalWrite(9, LOW);
 digitalWrite(8, HIGH);
 digitalWrite(7, HIGH);
 digitalWrite(6, LOW);
 digitalWrite(5, LOW);
 delay(10);
}
 else if (analogRead(capteur_A) <= 1050);
{ 
 Serial.println("Digital value : Oryx Weather");
 digitalWrite(12, LOW);
 digitalWrite(11, LOW);
 digitalWrite(10, LOW);
 digitalWrite(9, LOW);
 digitalWrite(8, LOW);
 digitalWrite(7, LOW);
 digitalWrite(6, HIGH);
 digitalWrite(5, HIGH);
 delay(10);
}
 else (analogRead(capteur_A) => 1069);
{
  Serial.println("digital value : off")
  digitalWrite(12, LOW);
  digitalWrite(11, LOW);
  digitalWrite(10, LOW);
  digitalWrite(9, LOW);
  digitalWrite(8, LOW);
  digitalWrite(7, LOW);
  digitalWrite(6, LOW);
  digitalWrite(5, LOW);
}

The problem is this semicolon you added after the if statement:

 if(analogRead(capteur_A) <= 650);

(as well as the ones on the else if statents.

This causes your if statement to be the equivalent of:

 if(analogRead(capteur_A) <= 650){}

The braces surrounding the code on the following lines are unrelated to the if statement. They just define an arbitrary scope in the code.

You can find some information on the correct if syntax here:

The semikolon, ; ends the statement, ends the "if", and the else's, and that's not what You want. The "if" then becomes a "nop", No Operation statement.

Hi Ethan; you may find using a switch - case structure makes your code easier to follow.

you can use a range of values here is a simple example

switch (arr[i])  
        { 
        case 1 ... 6: 
            printf("%d in range 1 to 6\n", arr[i]); 
            break; 
        case 19 ... 20: 
            printf("%d in range 19 to 20\n", arr[i]); 
            break; 
        default: 
            printf("%d not in range\n", arr[i]); 
            break; 
        }