Issues with IDE on Starter Project Three

First has there been a change in if, else if, code that I am unaware of? The reference library shows the similar patter for if(yada yada); else if(more yada);

Yet when I compile this code:

<const int sensorPin = A0;
const float baselineTemp = 20.0;

void setup() {
Serial.begin(9600);
for(int pinNumber = 2; pinNumber<5; pinNumber++){
pinMode(pinNumber, OUTPUT);
digitalWrite(pinNumber, LOW);
}
}

void loop() {
int sensorVal = analogRead(sensorPin);
Serial.print("Sensor Value: “);
Serial.print(sensorVal);
float voltage = (sensorVal/1024.0) * 5.0;
Serial.print(”, Volts: “);
Serial.print(voltage);
Serial.print(”, degrees C: ");
float temperature = (voltage - .5) * 100;
Serial.println(temperature);
if(temperature < baselineTemp+2){
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
}else if(temperature >= baselineTemp+2 &&
temperature < baselineTemp+4){
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
}else if(temperature >= baselineTemp+4 &&
temperature < baselineTemp+6){
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
}else if(temperature >= baselineTemp+6){
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
}
delay(100);
}

I get an error message saying "else if " with no previous "if"

What did I do wrong?

Posting in the wrong section
Not posting in code tags.
Not posting the actual error messages

1 Like

No.

You miss counted the number of braces.

Did you not see the pop up that told your code was not correctly posted or did you just ignore it?

Likewise did you not read the description of the beginning tutorial section you posted in? That section is for posting tutorials FOR beginners. So I have moved your post to a more appropriate section.

Wow, no mercy.

Yes, my first post and a grand welcome.

I did copy and past the code, and saw the message and wondered why it was telling me I was posting code. My apologies, about ignoring the code warning, but you said post examples. I will read up on who to post code.

Thanks for the feedback.

The code compiles fine for me, when replacing the " marks with that on shift + 2

1 Like