if, else statements and map

Hi.

I am trying to make multiple LED’s turn on at different brightnesses depending on the amount of force exerted on an FSR connected to my circuit. The code also needs to tell me the value of the force exerted.
However, when I am doing my if, else statements I keep getting the errors:

/tmp/480419218/final_project_round_2/final_project_round_2.ino: In function ‘void loop()’:

/tmp/480419218/final_project_round_2/final_project_round_2.ino:42:6: error: ‘else’ without a previous ‘if’

/tmp/480419218/final_project_round_2/final_project_round_2.ino:48:6: error: ‘else’ without a previous ‘if’

/tmp/480419218/final_project_round_2/final_project_round_2.ino:54:6: error: ‘else’ without a previous ‘if’

exit status 1

This is my code.

const int sensorPin = A0;
const float baselineForce = 0.1;
const int LED1 = 2;
const int LED2 = 3;
const int LED3 = 4;
const int LED4 = 5;

int ledState = LOW;
int LED ;

void setup() {
Serial.begin(9600); // open a serial port

for(int pinNumber = 2; pinNumber<6; pinNumber++) {
pinMode(pinNumber, OUTPUT);
digitalWrite(pinNumber, LOW);
}
}

void loop() {{
int sensorVal = analogRead(sensorPin);
Serial.print ("Sensor Value: ");
Serial.print (sensorVal);
// convert the voltage to light output
digitalWrite(sensorPin, ledState);
digitalWrite(LED, ledState);
float voltage = (sensorVal/1024.0) * 5.0;
Serial.print(“sensorVal”);

if (map(sensorVal, 0.1, 1032, 0.1, 258));
{
digitalWrite(sensorPin, ledState);
digitalWrite(LED1, ledState);
}

else if (map(sensorVal, 0.1, 1032, 258.1, 516));
{
digitalWrite(sensorPin, ledState);
digitalWrite(LED2, ledState);
}

else if (map(sensorVal, 0.1, 1032, 516.1, 774));
{
digitalWrite(sensorPin, ledState);
digitalWrite(LED3, ledState);
}

else if (map(sensorVal, 0.1, 1032, 774.1, 1024));
{
digitalWrite(sensorPin, ledState);
digitalWrite(LED4, ledState);
}
delay(100);

}

}

I’ve only been coding a couple of weeks but I can not figure out what I am doing wrong. Any help you can give would be much appreciated.

Thank you!

Errors:

  1. The statement if() is finished “;”. You can use “;” for single statement or “{}” for more, but not both.
  2. There were missing semicolons for another two statements.

Here is code which is able to compile:

const int sensorPin = A0;
  const float baselineForce = 0.1;
  const int LED1 = 2;
  const int LED2 = 3;
  const int LED3 = 4;
  const int LED4 = 5;

 int ledState = LOW;
 int LED ;

void setup() {
  Serial.begin(9600);  // open a serial port  


 for(int pinNumber = 2; pinNumber<6; pinNumber++) {
    pinMode(pinNumber, OUTPUT);
    digitalWrite(pinNumber, LOW);
 }
}

void loop() {{
  int sensorVal = analogRead(sensorPin);
  Serial.print ("Sensor Value: ");
  Serial.print (sensorVal);
     // convert the voltage to light output
  digitalWrite(sensorPin, ledState);
  digitalWrite(LED, ledState);
  float voltage = (sensorVal/1024.0) * 5.0;
  Serial.print("sensorVal");
     
     if (map(sensorVal, 0.1, 1032, 0.1, 258))
     {
     digitalWrite(sensorPin, ledState);
     digitalWrite(LED1, ledState);
     }
     
     else if (map(sensorVal, 0.1, 1032, 258.1, 516))
     {
       digitalWrite(sensorPin, ledState);
       digitalWrite(LED2, ledState);
     }
     
     else if (map(sensorVal, 0.1, 1032, 516.1, 774))
      {
       digitalWrite(sensorPin, ledState);
       digitalWrite(LED3, ledState);
      }
     
     else if (map(sensorVal, 0.1, 1032, 774.1, 1024))
     {
       digitalWrite(sensorPin, ledState);
       digitalWrite(LED4, ledState);
     }
     delay(100);
     
}

}

BTW: Use code tags for posting code - see forum rules.

if (map(sensorVal, 0.1, 1032, 0.1, 258))

While that will compile I don’t think it is what you want to do.
That if will trigger every time it results in a value of 1 or greater. It is normal to add an == or > or < and some other constant or variable to compair the map value to.

Also note that the map function only takes and returns integers, so putting things like 0.1 is the same as putting zero.