Expected unqualified-id before 'else' in TinkerCad

I am new to Arduino. Although I verified in the IDE that my code has no error, but when I pasted it into my TinkerCad circuit it shows the following:


const int pinL_Sensor = A5;      //pin A5: left sensor 
const int pinB_Sensor = A4;      //pin A4: bumper sensor
const int pinR_Sensor = A3;      //pin A3: right sensor 

const int pinL_PWM = 9;          //pin D9: left motor speed
const int pinL_DIR = 10;         //pin D10: left motor direction

const int pinR_PWM = 11;         //pin D11: right motor speed
const int pinR_DIR = 12;         //pin D12: right motor direction

//define variables to be used in script

int bumperSensor = 1;  // not sensing white
int leftSensor = 1;    // not sensing white
int rightSensor = 1;   // not sensing white

int countBumper = 0;   // bumper not triggered yet
int countLeftSplit = 0; // number of left turns at split (max = 3)

// the setup function runs once when you press reset or power the board

void setup ()
{
  // define pins as input and output
  pinMode(pinB_Sensor, INPUT);
  pinMode(pinL_Sensor, INPUT);
  pinMode(pinR_Sensor, INPUT);
  
  pinMode(pinL_DIR, OUTPUT);
  pinMode(pinR_DIR, OUTPUT);
  
  pinMode(pinL_PWM, OUTPUT);
  pinMode(pinR_PWM, OUTPUT);
  
  // initialize output pins
  digitalWrite(pinL_DIR, HIGH);   //forward direction    
  digitalWrite(pinR_DIR, HIGH);   //forward direction
  analogWrite(pinL_PWM, 0);    //stop at the start position 
  analogWrite(pinR_PWM, 0);    //stop at the start position 
}

// the loop function runs over and over again forever

void loop() {

  // Arduino is reading the sensor measurements
  bumperSensor = digitalRead(pinB_Sensor);
  leftSensor = digitalRead(pinL_Sensor);
  rightSensor = digitalRead(pinR_Sensor);
  
  // Car is reset at the start position
  if ( bumperSensor && countBumper == 0 ) {
    analogWrite(pinL_PWM, 0);
    analogWrite(pinR_PWM, 0);
  }

  // Bumper Sensor is triggered at the start position
  else if ( !bumperSensor && countBumper == 0) {
    analogWrite(pinL_PWM, 200);
    analogWrite(pinR_PWM, 200);
    countBumper = countBumper + 1;
    delay(350);     //to let the car leave the start position to avoid sensors miscount 00 at the start position 
  }
  
  // Car is tracking on the white line
  else if ( bumperSensor && countBumper == 1) 
  { 
    if ( !leftSensor && !rightSensor ) {     //rotate left until both sensor detects black again
      countLeftSplit += 1;
      while ( !leftSensor && !rightSensor ) {
        leftSensor = digitalRead(pinL_Sensor);
        rightSensor = digitalRead(pinR_Sensor);
        analogWrite(pinL_PWM, 200);
        analogWrite(pinR_PWM, 200);
        digitalWrite(pinL_DIR, LOW);
        digitalWrite(pinR_DIR, HIGH); 
        }
       
      }
    
    if ( !leftSensor && rightSensor ) {
        analogWrite(pinL_PWM, 200);
        analogWrite(pinR_PWM, 240);
        digitalWrite(pinL_DIR, LOW);
        digitalWrite(pinR_DIR, HIGH);  
      }
    
    if ( leftSensor && !rightSensor ) {
        analogWrite(pinL_PWM, 200);
        analogWrite(pinR_PWM, 200);
        digitalWrite(pinL_DIR, HIGH);
        digitalWrite(pinR_DIR, LOW);  
      }
    
    if ( leftSensor && rightSensor ) {
        analogWrite(pinL_PWM, 200);
        analogWrite(pinR_PWM, 200);
        digitalWrite(pinL_DIR, HIGH);
        digitalWrite(pinR_DIR, HIGH);
      }
  }
  
  //finish navigating the 3rd split, switching to right angled turns
  else if ( countBumper == 1 && countLeftSplit == 3 ) {
    analogWrite(pinL_PWM, 120);
    analogWrite(pinR_PWM, 120);
    digitalWrite(pinL_DIR, HIGH);
    digitalWrite(pinR_DIR, HIGH);
    delay(500);
    analogWrite(pinL_PWM, 0);
    analogWrite(pinR_PWM, 0);
    delay(1000);                // stop for one second
    if ( leftSensor && !rightSensor ) {
      while ( !leftSensor && !rightSensor ) {
      leftSensor = digitalRead(pinL_Sensor);
      rightSensor = digitalRead(pinR_Sensor);
      analogWrite(pinL_PWM, 150);
      analogWrite(pinR_PWM, 150);
      digitalWrite(pinL_DIR, HIGH);
      digitalWrite(pinR_DIR, LOW);
      }
    }
    else if ( !leftSensor && rightSensor ) {
      while ( !leftSensor && !rightSensor ) {
      leftSensor = digitalRead(pinL_Sensor);
      rightSensor = digitalRead(pinR_Sensor);
      analogWrite(pinL_PWM, 150);
      analogWrite(pinR_PWM, 150);
      digitalWrite(pinL_DIR, LOW);
      digitalWrite(pinR_DIR, HIGH);
      }
    }
    else if ( leftSensor && rightSensor ) {
      
      analogWrite(pinL_PWM, 150);
      analogWrite(pinR_PWM, 150);
      digitalWrite(pinL_DIR, HIGH);
      digitalWrite(pinR_DIR, HIGH);
    }
  }
  // Car is hitting the white wall
  else if ( !bumperSensor && countBumper == 1)
  {
    countBumper = countBumper + 1;
    analogWrite(pinL_PWM, 255);
    analogWrite(pinR_PWM, 255);
    digitalWrite(pinL_DIR, LOW);
    digitalWrite(pinR_DIR, LOW);  
    delay(750);     // to let the car keep moving backward for 45 cm
  }
  
  // Car stop after backward for 1 second 
  else if ( bumperSensor && countBumper == 2)
  {
    analogWrite(pinL_PWM, 0);
    analogWrite(pinR_PWM, 0);
  }
}
3:1: error: expected unqualified-id before 'else'




 exit status 1

I don't understand what's going on

The error is not in the portion that you posted.

Posting screenshots of code is near worthless. Posting incomplete code is not good either. Read the forum guidelines to see how to properly post code and some information on how to get the most from this forum.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Please include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.

Okay, I replaced it.

Your code compiles fine for me, too. IDE ver. 1.8.19, Win 10.

This looks to be a Tinkercad problem. This forum deals with the Arduino IDE and real hardware. Though there may be some here that knows Tinkercad well enough to help you, the odds are not in favor. I suggest that you try the Tinkercad forum.

This is not the first time that I have seen a situation where the Tinkercad simulator and the real world clash.

Confirmed, error in Tinkercad, no error in the Arduino IDE, no error in Wokwi.

I can reduce the sketch and still cause the error in Tinkercad:

bool a = random(0,2);
bool b = random(0,2);

void setup ()
{
}

void loop() 
{
  if (a) 
  {
    analogWrite(5, 0);
  }
  else if (a) 
  {
    if (a) 
    {
      analogWrite(5,0);
    }
    else if (a) 
    {
      analogWrite(5,0);
    }
    else if (a && b) 
    {
      analogWrite(5,0);
    }
  }
}

The last (a && b) is essential to cause this bug.

Maybe Tinkercad uses an older pre-processor and a older version of the compiler.

Wokwi has no problem with the sketch: https://wokwi.com/
You need to log in to be able to store a project.

@

Your other topic on the same subject deleted.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn and Use The Forum

It will help you get the best out of the forum in the future.

  • Your OS and version can be valuable information, please include it along with extra security you are using.

  • Always list the version of the IDE you are using and the board version if applicable.

  • Use quote or add error messages as an attachment NOT a picture.

  • How to insert an image into your post. ( Thanks @sterretje )

  • Add your sketch where applicable but please use CODE TAGS ( </> )

  • Add a SCHEMATIC were needed even if it is hand drawn

  • Add working links to any specific hardware as needed (NOT links to similar items)

  • Remember that the people trying to help cannot see your problem so give as much information as you can

COMMON ISSUES

  • Ensure you have FULLY inserted the USB cables.

  • Check you have a COMMON GROUND where required. ( Thanks @Perry)

  • Where possible use USB 2.0 ports or a USB 2.0 POWERED HUB to rule out USB 3.0 issues.

  • Try other computers where possible.

  • Try other USB leads where possible.

  • You may not have the correct driver installed. CH340/341 or CP2102 or FT232 VCP Drivers - FTDI

  • There may be a problem with the board check or remove your wiring first.

  • Remove any items connected to pins 0 and 1.

COMPUTER RELATED

  • Close any other serial programs before opening the IDE.

  • Ensure you turn off any additional security / antivirus just to test.

  • There may be a problem with the PC try RESTARTING it.

  • You may be selecting the wrong COM port.

  • Avoid cloud/network based installations where possible OR ensure your Network/Cloud software is RUNNING.

  • Clear your browsers CACHE.

  • Close the IDE before using any other serial programs.

  • Preferably install IDE’s as ADMINISTRATOR or your OS equivalent

ARDUINO SPECIFIC BOARDS

  • CH340/341 based clones do not report useful information to the “get board info” button.

  • NANO (Old Types) some require you to use the OLD BOOTLOADER option.

  • NANO (ALL Types) See the specific sections lower in the forum.

  • NANO (NEW Types) Install your board CORE’s.

  • Unless using EXTERNAL PROGRAMMERS please leave the IDE selection at default “AVRISP mkII”.

  • Boards using a MICRO usb connector need a cable that is both DATA and CHARGE. Many are CHARGE ONLY.

CREATE editor install locations.

  • On macOs ~/Applications/ArduinoCreateAgent-1.1/ArduinoCreateAgent.app/Contents/MacOS/config.ini

  • On Linux ~/ArduinoCreateAgent-1.1/config.ini

  • On Windows C:\Users[your user]\AppData\Roaming\ArduinoCreateAgent-1.1

Performing the above actions may help resolve your problem without further help.

Language problem ?

Try a language closer to your native language:

Thanks to all those who helped and added to this list.

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