Marble Sorter PLEASE HELP (student code)

#1

Hello forum users! I am new here and have a project due next week Monday, I have built a robot and coded it so that it can sort 3 types of marbles (Metal, wood and clear plastic) I am using a light sensor to differentiate between the 3. The values are as followed, Plastic = less then 600, Wood = between 600 and 1000 and metal = over 1000. I am trying to use if else else statements to achieve this. The sensor value determines the rotation of a servo with a plate on it for where the marbles should go… The code is in Vex Cortex 2.0 and PLTW Natural language and is available below. I get errors where both else statements appear. I have searched for the past 5 days trying to find a solution for myself but I have now realized I need more help.

#pragma config(Sensor, in1, Color, sensorReflection)
#pragma config(Sensor, dgtl1, Bump, sensorTouch)
#pragma config(Motor, port1, Light, tmotorVexFlashlight, openLoop, reversed)
#pragma config(Motor, port3, Gate, tmotorVex269_MC29, openLoop)
#pragma config(Motor, port4, Plate, tmotorServoStandard, openLoop)
//!!Code automatically generated by ‘ROBOTC’ configuration wizard !!//

task main()
{
turnFlashlightOn (Light, 127);

if (SensorValue (Bump) == 1) //If bump is pressed then program starts
{
	while (1 == 1)
	{
		if (SensorValue (Light <= 600)) // If the light sensor is less then 600
			setServo (Plate, -60); // The servo goes to -60
		wait (1); //Gives servo time to move
		startMotor (Gate, 25); //Starts gear to move marble in cup
		wait (.5); // Lets motor push marble
		stopMotor (Gate); //Stops motor
	}
	{
		else if (SensorValue (Light =< 600 || <= 1000 )); //If the light sensor is greater then 600 or less then 1000


			setServo (Plate, 1); // The servo goes to 1
			wait (1); //Gives servo time to move
			startMotor (Gate, 25); //Starts gear to move marble in cup
			wait (.5); // Lets motor push marble
			stopMotor (Gate); //Stops motor
		}
		else
		{

			if (SensorValue (Light >= 1000) //If the light sensor is greater then 1000
			{
				setServo (Plate, 80); // The server goes to 80
				wait (1); //Gives servo time to move
				startMotor (Gate, 25); //Starts gear to move marble in cup
				wait (.5); // Lets motor push marble
				stopMotor (Gate); //Stops motor
			}
		}
	}
}

The errors I get are:
File “C:\Users\NAME\Desktop\Marble Sorter.c” compiled on May 09 2019 09:58:13
Warning:Mismatched typedefs. Converting typedef ‘short’ to typedef ‘tMotor’, value ‘0x0258’
Error:Unexpected ‘else’. Ignored.
Error:Unexpected ‘<’ during parsing
Error:Expected->’)’. Found ‘||’
Error:Unexpected ‘<=’ during parsing
Error:Unexpected ‘)’ during parsing
Error:Unexpected ‘else’. Ignored.
Warning:Mismatched typedefs. Converting typedef ‘short’ to typedef ‘tMotor’, value ‘0x03E8’
Warning:Ummatched left parenthesis ‘(’ at end of line. Automatically inserted.

0 Likes

#3

What did i do wrong? Sorry I am new to forum posting

1 Like

#4

I’ll take a first pass at this, people with more recent ROBOTC experience should feel free to correct me.

(also sorry for the deleted post above, I hit ‘enter’ too early and decided to start over rather than leaving a very incomplete response up for several minutes)

Here’s a corrected version of your code:

#pragma config(Sensor, in1, Color, sensorReflection)
#pragma config(Sensor, dgtl1, Bump, sensorTouch)
#pragma config(Motor, port1, Light, tmotorVexFlashlight, openLoop, reversed)
#pragma config(Motor, port3, Gate, tmotorVex269_MC29, openLoop)
#pragma config(Motor, port4, Plate, tmotorServoStandard, openLoop)
//!!Code automatically generated by ‘ROBOTC’ configuration wizard !!//

task main()
{
    turnFlashlightOn(Light, 127);

    if (SensorValue (Bump) == 1) //If bump is pressed then program starts
    {
	    while (1 == 1)
	    {
		    if (SensorValue(Light) <= 600) // If the light sensor is less then 600
            {
			    setServo (Plate, -60); // The servo goes to -60
		        wait (1); //Gives servo time to move
		        startMotor (Gate, 25); //Starts gear to move marble in cup
		        wait (.5); // Lets motor push marble
		        stopMotor (Gate); //Stops motor
	        }
		    else if ((SensorValue(Light) > 600) || (SensorValue(Light) <= 1000 )) //If the light sensor is greater then 600 or less then 1000
            {
			    setServo (Plate, 1); // The servo goes to 1
			    wait (1); //Gives servo time to move
			    startMotor (Gate, 25); //Starts gear to move marble in cup
			    wait (.5); // Lets motor push marble
			    stopMotor (Gate); //Stops motor
		    }
		    else  //If the light sensor is greater then 1000
			{
				setServo (Plate, 80); // The server goes to 80
				wait (1); //Gives servo time to move
				startMotor (Gate, 25); //Starts gear to move marble in cup
				wait (.5); // Lets motor push marble
				stopMotor (Gate); //Stops motor
			}
		}
	}
}

And here’s a summary of the changes/corrections I made:

  • if statments containing more than one line of code need to be wrapped in curly braces.
  • You don’t need a new opening curly brace before each else if, those statements are all in the same block (the infinite while loop).
  • When comparing the sensor values to various presets, the comparison operator goes outside the call to SensorValue. SensorValue(Light) is the value of the light sensor; SensorValue(Light) > 600 evaluates to true if that value is greater than 600.
  • In the first else if conditional, you need to reference SensorValue twice, since you are performing a logical combination of two boolean values. Also the boolean operator should be ‘and’ (&&) rather than ‘or’ (||). So the condition you’re checking for is “(The sensor value is greater than 600) AND (The sensor value is less than or equal to 1000)”
  • The last else if can just be an else. If the sensor value is not less than 600, or between 600 and 1000, then it must be greater than 1000
  • Each new code block (section of code contained in curly braces) is indented 4 spaces further than its parent block for readability. Some people prefer to use a different number of spaces, or tab characters, that’s fine too, but many programmers are really passionate about what the correct choice is :stuck_out_tongue: .
5 Likes

#5

The revised code looks good to me.

0 Likes