Annoying 'ELSE' without a previous 'IF' error

I think this is the correct code but keeps showing 'ELSE' without the 'IF' error message.
like this

I believe else if statements are correct. Can anyone tell me what's wrong??

const int LED1 = 41;
const int LED2 = 42;
const int LED3 = 43;
const int LED4 = 44;
const int LED5 = 45;
const int LED6 = 46;
const int LED7 = 47;
const int LED8 = 48;
const int LED9 = 49;
const int LED10 = 50;

const int SW_1 = 31;
const int SW_2 = 31;
const int SW_3 = 31;
const int SW_4 = 31;
const int SW_5 = 31;
const int SW_6 = 31;
const int SW_7 = 31;
const int SW_8 = 38;


const float Voltage = 120;
float POT_NUM[10], LDR_NUM[10];
int Switch1, Switch8, LED=41;
float LDR;

float POT_Total, LDR_Total;
float POT_Average, LDR_Average, BOTH_Average;
float POT_Current, LDR_Current, BOTH_Current;

float POT_Average2, LDR_Average2, BOTH_Average2;

float Result1;//for pot


  void setup() {
  // put your setup code here, to run once:
        Serial.begin(9600);
        for(int i=31; i<39; i++){//Define digit pins as inputs/outputs
        pinMode(i, INPUT);//Setup Switches INPUT
}//ends for-loop digital Switches
        
        for(int i=41; i<51; i++){//Define digit pins as inputs/outputs
        pinMode(i, OUTPUT);//Setup LEDs OUTPUT
}//ends for-loop digital LED

  NAME();
}//ends Void Setup

  void loop() {
  // put your main code here, to run repeatedly:

  Switch1 = digitalRead(31); 
  Switch8 = digitalRead(38);
  if(Switch1==1){
  POT_Average2 = POT_Reading();
  Serial.print("\nThe Average POT Values is: ");
  Serial.print(POT_Average2, 1);
  Serial.print(" ohm");

  POT_Current = Voltage/POT_Average2*1000;
  Serial.print("\nThe Circuit Current with POT is: ");
  Serial.print(POT_Current, 1);
  Serial.print(" mA");
  
}//ends if for Switch1
 
  Switch8 = digitalRead(38);
  else if (Switch8==1){
  LDR_Average2 = LDR_Reading();
  Serial.print("\nThe Average LDR Value is: ");
  Serial.print(LDR_Average2, 1);
  Serial.print(" ohm");

  LDR_Current = Voltage/LDR_Average2*1000;
  Serial.print("\nThe Circuit Current with LDR is: ");
  Serial.print(LDR_Current, 1);
  Serial.print(" mA");
  
}//ends if for Switch8

  else if(Switch1 ==1 && Switch8 == 1){
  LDR_Reading();
  POT_Reading();
  BOTH_Average2= POT_Reading()+LDR_Reading();
  Serial.print("\nThe Average POT & LDR Value is: ");
  Serial.print(BOTH_Average2, 1);
  Serial.print(" ohm");

  BOTH_Current = Voltage/BOTH_Average2*1000;
  Serial.print("\nThe Circuit Current with POT & LDR is: ");
  Serial.print(BOTH_Current, 1);
  Serial.print(" mA");
    
  
}//ends if for Both Switch1 & Switch8

}//ends Void Loop

  float POT_Reading(){

  POT_Total = 0;//To set the total value 0 for repeating computation

  for (int i=0; i<10; i++){
  delay(2000);//Delay to read every 2 seconds
  POT_NUM[i]= analogRead(A7);//Read&Store POT value 6 elements
  digitalWrite(LED, HIGH);
  LED = LED +1;
  POT_Total = POT_Total + POT_NUM[i];
  
}//ends for POT read
  LED=41;//Set back to 41
  
for (int i=0; i<10; i++){
  delay(1000);//Delay to read every 2 seconds
  Serial.print("\n");
  Serial.print(i+1);
  Serial.print(" The POT Value is: ");
  Serial.print(POT_NUM[i], 1);
  Serial.print(" ohm");
  
  digitalWrite(LED, LOW);
  LED = LED +1;
  
}//ends for POT print

  POT_Average = POT_Total /10;
   
  return POT_Average;
}//ends POT function

float LDR_Reading(){

  LDR_Total = 0;//To set the total value 0 for repeating computation

  for (int i=0; i<10; i++){
  delay(2000);//Delay to read every 2 seconds
  LDR_NUM[i]= analogRead(A6);//Read&Store POT value 6 elements
  digitalWrite(LED, HIGH);
  LED = LED +1;
  LDR_Total = LDR_Total + LDR_NUM[i];
  
}//ends for LDR read
  LED=41;//Set back to 41
  
for (int i=0; i<10; i++){
  delay(500);//1 Second-delay between printing the numbers
  Serial.print("\n");
  Serial.print(i+1);
  Serial.print(" The LDR Value is: ");
  Serial.print(LDR_NUM[i], 1);
  Serial.print(" ohm");
  
  digitalWrite(LED, LOW);
  LED = LED +1;
  
}//ends for LDR print

  LDR_Average = LDR_Total /10;
   
  return LDR_Average;
}//ends LDR function

void NAME(){//Begins NAME Fuction

String First ="";
String Last = "";

  Serial.print("\nPlease enter your first name: ");
while(Serial.available()==0){};//waits for user input
First=Serial.readString();//User input stored in First

Serial.print("\nPlease enter your last name: ");
while(Serial.available()==0){};//waits for user input
Last=Serial.readString();//User input stored in Last

Serial.print("\n\tGood morning " +First);
  }

The line marked as an error should not be here.
The else statement must immediately follow the closing parenthesis of "if"

1 Like

Hello fundamentum
Take a view here:
grafik

p.s. b707 was faster :slight_smile:

1 Like

Thank you so much! You are the savior!

You are a rock star! :smile:

You have comment at every closing bracket.
That is not needed in my opinion:

Your code:

for (int i=0; i<10; i++){
  delay(500);//1 Second-delay between printing the numbers
  Serial.print("\n");
  Serial.print(i+1);
  Serial.print(" The LDR Value is: ");
  Serial.print(LDR_NUM[i], 1);
  Serial.print(" ohm");

  digitalWrite(LED, LOW);
  LED = LED +1;

}//ends for LDR print

In my opinion, this is better to read:

  for (int i = 0; i < 10; i++)
  {
    delay(500);             // 1 Second-delay between printing the numbers
    Serial.print("\n");
    Serial.print(i + 1);
    Serial.print(" The LDR Value is: ");
    Serial.print(LDR_NUM[i], 1);
    Serial.print(" ohm");

    digitalWrite(LED, LOW);
    LED = LED + 1;
  }

The brackets are there to make it easier for you.

1 Like

If you bother to comment the obvious, it should be correct.

Please edit your post, select all code and click the </> button to apply code tags; next save your post. It makes it easier to read and copy and prevents the forum software from interpreting it incorrectly.

Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advice on) your project.

1 Like

Thank you so much, I will follow your guide from now on! :grin:

I just learned that from you ! Lol :joy:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.