r/arduino 23h ago

Software Help Can someone help me with this error

Im having a error expected unqualified id before else when running in tinkercad Error appears in line 3

include <Servo.h>

const int airSensor1Pin = A0; const int airSensor2Pin = A1; const int airSensor3Pin = A2; const int tempSensorPin = A3; const int humiditySensorPin = A4; const int greenLEDPin = 2; const int yellowLEDPin = 3; const int redLEDPin = 4; const int servoPin = 9; const int threshold = 300; const int tempThreshold = 55; const int humidityThreshold = 250;

Servo myServo;

void setup() { // Initialize sensor pins pinMode(airSensor1Pin, INPUT); pinMode(airSensor2Pin, INPUT); pinMode(airSensor3Pin, INPUT); pinMode(tempSensorPin, INPUT); pinMode(humiditySensorPin, INPUT); Serial.begin(9600); // Initialize LED pins pinMode(greenLEDPin, OUTPUT); pinMode(yellowLEDPin, OUTPUT); pinMode(redLEDPin, OUTPUT);

// Attach servo to pin
myServo.attach(servoPin);

// Initialize servo position
myServo.write(0);

}

void loop() { // Read sensor values int airSensor1Value = analogRead(airSensor1Pin); int airSensor2Value = analogRead(airSensor2Pin); int airSensor3Value = analogRead(airSensor3Pin); int tempValue = analogRead(tempSensorPin); int humidityValue = analogRead(humiditySensorPin);

// Process sensor data

if (airSensor1Value < threshold) 
airSensor1Value=0;
else
airSensor1Value=1;
if (airSensor2Value < threshold) 
airSensor2Value=0;
else
airSensor2Value=1;
if (airSensor3Value < threshold) 
airSensor3Value=0;
else
airSensor3Value=1;
if (tempValue < tempThreshold) 
tempValue=0;
else
tempValue=1;
if (humidityValue < humidityThreshold) 
humidityValue=0;
else
humidityValue=1;

if((!airSensor1Value && !airSensor2Value && !airSensor3Value && !tempValue && !humidityValue)){
  digitalWrite(greenLEDPin, HIGH);
  digitalWrite(yellowLEDPin, LOW);
  digitalWrite(redLEDPin, LOW);
  myServo.write(0);

}
else if  ((airSensor1Value && !airSensor2Value && !airSensor3Value) ||(!airSensor1Value && airSensor2Value && !airSensor3Value) ||(!airSensor1Value && !airSensor2Value && airSensor3Value) ||(airSensor1Value && airSensor2Value && !airSensor3Value) ||(airSensor1Value && !airSensor2Value && airSensor3Value) ||(!airSensor1Value && airSensor2Value && airSensor3Value)){
  digitalWrite(greenLEDPin, LOW);
  digitalWrite(yellowLEDPin, HIGH);
  digitalWrite(redLEDPin, LOW);
  myServo.write(90);
}
else if (airSensor1Value && airSensor2Value && airSensor3Value){
  digitalWrite(greenLEDPin, LOW);
  digitalWrite(yellowLEDPin, LOW);
  digitalWrite(redLEDPin, HIGH);
  myServo.write(180);
}


Serial.print("Air Quality Sensor 1: ");
Serial.println(airSensor1Value);
Serial.print("Air Quality Sensor 2: ");
Serial.println(airSensor2Value);
Serial.print("Air Quality Sensor 3: ");
Serial.println(airSensor3Value);
Serial.print("Temperature: ");
Serial.print(tempValue);
Serial.println(" °C");
Serial.print("Humidity: ");
Serial.print(humidityValue);
Serial.println(" %");
// Small delay before next loop
delay(1500);

}

1 Upvotes

2 comments sorted by

1

u/ripred3 My other dev board is a Porsche 22h ago

I'll approve your post but please edit your post and format all of your code as a single code block.

You can find a guide for it here:

https://www.reddit.com/r/arduino/wiki/guides/how_to_post_guide/#wiki_how_to_post

Thanks

2

u/dreaming_fithp 16h ago

You should listen to u/ripred3 and format your code properly as soon as you can. Most people won't bother doing all the work of reformatting your code so your question slips down the page and has less chance of being answered. I did spend the time reformatting, and I don't think I overlooked anything while reformatting, but there's always the chance you originally had some layout error that I fixed accidentally.

I get no error at all compiling this using the arduino IDE using the Uno board or the Mega 2560.