View unanswered posts | View active topics It is currently Fri Nov 22, 2019 11:26 am






Reply to topic  [ 3 posts ] 
Unexpected else 
Author Message
Rookie

Joined: Wed Apr 29, 2015 3:52 pm
Posts: 2
Post Unexpected else
Hi!
I'm brand new to programming. I am having problems with else in while loops. I can't figure it out. Everything works except for when I try to compile it to my robot. Then it says that the else in red print is unexpected. If you know anything that could help us that would be great! Thanks!
Code:
 
#pragma config(Sensor, dgtl1,  rightEncoder,   sensorQuadEncoder)
#pragma config(Sensor, dgtl3,  leftEncoder,    sensorQuadEncoder)
#pragma config(Sensor, dgtl8,  sonarSensor,    sensorSONAR_cm)
#pragma config(Motor,  port2,           rightMotor,    tmotorVex393_MC29, openLoop, reversed)
#pragma config(Motor,  port3,           leftMotor,     tmotorVex393_MC29, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

task main()
{
   wait1Msec(2000);


      
while(1 == 1)      // Creates an infinite loop, since "true" always evaluates to true
   {
      if(SensorValue[sonarSensor] > 50)
      {
         {
   wait1Msec(2000);                     // Robot waits for 2000 milliseconds before executing program

   SensorValue[rightEncoder] = 0;     // Set the encoder so that it starts counting at 0
   SensorValue[leftEncoder]  = 0;     // Set the encoder so that it starts counting at 0

         while(1 == 1)      // Creates an infinite loop, since "true" always evaluates to true
            {
            if(SensorValue[rightEncoder] == SensorValue[leftEncoder]) // If rightEncoder has counted the same amount as leftEncoder:
               {
               // Move Forward
               motor[rightMotor] = 80;          // Right Motor is run at power level 80
               motor[leftMotor]  = 80;          // Left Motor is run at power level 80
               }
               else if(SensorValue[rightEncoder] > SensorValue[leftEncoder])   // If rightEncoder has counted more encoder counts
               {
                  // Turn slightly right
                  motor[rightMotor] = 60;          // Right Motor is run at power level 60
                  motor[leftMotor]  = 80;          // Left Motor is run at power level 80
               }
               else   // Only runs if leftEncoder has counted more encoder counts
               {
                  // Turn slightly left
                  motor[rightMotor] = 80;          // Right Motor is run at power level 80
                  motor[leftMotor]  = 60;          // Left Motor is run at power level 60
               }
            }
         }
      [color=#FF0000]else if(SensorValue[sonarSensor] < 50)   // If it sees a wall[/color]
      {
            // Turn right
         motor[rightMotor] = -80;          // Right Motor is run at power level 80 backwards
         motor[leftMotor]  = 80;          // Left Motor is run at power level 80
      }
      else
      {
         // Turn  left
         motor[rightMotor] = 80;          // Right Motor is run at power level 80
         motor[leftMotor]  = -80;          // Left Motor is run at power level 80 backwards
      }
   }
}
}         


Wed Apr 29, 2015 4:07 pm
Profile
Moderator
Moderator

Joined: Tue May 19, 2015 3:07 pm
Posts: 91
Post Re: Unexpected else
So to help with debugging I am going to move all the encoder stuff into its own function


Code:
#pragma config(Sensor, dgtl1,  rightEncoder,   sensorQuadEncoder)
#pragma config(Sensor, dgtl3,  leftEncoder,    sensorQuadEncoder)
#pragma config(Sensor, dgtl8,  sonarSensor,    sensorSONAR_cm)
#pragma config(Motor,  port2,           rightMotor,    tmotorVex393_MC29, openLoop, reversed)
#pragma config(Motor,  port3,           leftMotor,     tmotorVex393_MC29, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//
void EncoderStuff(){
    wait1Msec(2000);                     // Robot waits for 2000 milliseconds before executing program

   SensorValue[rightEncoder] = 0;     // Set the encoder so that it starts counting at 0
   SensorValue[leftEncoder]  = 0;     // Set the encoder so that it starts counting at 0

         while(1 == 1)      // Creates an infinite loop, since "true" always evaluates to true
            {
            if(SensorValue[rightEncoder] == SensorValue[leftEncoder]) // If rightEncoder has counted the same amount as leftEncoder:
               {
               // Move Forward
               motor[rightMotor] = 80;          // Right Motor is run at power level 80
               motor[leftMotor]  = 80;          // Left Motor is run at power level 80
               }
               else if(SensorValue[rightEncoder] > SensorValue[leftEncoder])   // If rightEncoder has counted more encoder counts
               {
                  // Turn slightly right
                  motor[rightMotor] = 60;          // Right Motor is run at power level 60
                  motor[leftMotor]  = 80;          // Left Motor is run at power level 80
               }
               else   // Only runs if leftEncoder has counted more encoder counts
               {
                  // Turn slightly left
                  motor[rightMotor] = 80;          // Right Motor is run at power level 80
                  motor[leftMotor]  = 60;          // Left Motor is run at power level 60
               }
            }
}





//new main task
task main()
{
   wait1Msec(2000);


     
while(1 == 1)      // Creates an infinite loop, since "true" always evaluates to true
   {
      if(SensorValue[sonarSensor] > 50)
      {
         {
              EncoderStuff();
         }
      else if(SensorValue[sonarSensor] < 50)   // If it sees a wall[/color]
      {
            // Turn right
         motor[rightMotor] = -80;          // Right Motor is run at power level 80 backwards
         motor[leftMotor]  = 80;          // Left Motor is run at power level 80
      }
      else
      {
         // Turn  left[/color]
         motor[rightMotor] = 80;          // Right Motor is run at power level 80
         motor[leftMotor]  = -80;          // Left Motor is run at power level 80 backwards
      }
   }
}
}     


Now it is very easy to see that

Code:
 if(SensorValue[sonarSensor] > 50)
      {
         {

Has 2 open brackets. Else statements require an if to have directly preceded the else statement. Removing one of the opening brackets as well as one of the closing brackets at the end of the program should give you the desired outcome.


Tue May 19, 2015 3:48 pm
Profile
Rookie

Joined: Wed Apr 29, 2015 3:52 pm
Posts: 2
Post Re: Unexpected else
Thank you so much! I get it now.


Tue May 26, 2015 7:49 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 3 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.