Marble Sorter code help

my partner and I are finishing our marble sorter for a final grade in our PoE class we have the hardware portion pout together and some of the code has been put together, but the line follower and finishing motor wont work here’s the code and a picture of the hardware

#pragma config(Sensor, in1, cupReader, sensorPotentiometer)
#pragma config(Sensor, in2, marbleSorter, sensorLineFollower)
#pragma config(Sensor, dgtl1, startButton, sensorTouch)
#pragma config(Motor, port2, sorterGear, tmotorVex393HighSpeed_MC29, openLoop)
#pragma config(Motor, port3, cupGears, tmotorVex393HighSpeed_MC29, openLoop)
#pragma config(Motor, port4, startServo, tmotorServoStandard, openLoop)
//!!Code automatically generated by ‘ROBOTC’ configuration wizard !!//

/*
Project Title: Marble Sorter
Team Members:Colton M. Gabe W.
Date:3/28/19

Task Description: Create a machine that can determine the difference in 4 different marbles when inserted into the machine.

*/

//Funtion Declarations

void moveMarbles();
void sortMarbles();

task main()
{
moveMarbles();
sortMarbles();
}

void moveMarbles()
{
while(1==1)
{
motor[startServo]=-127;
wait1Msec(1000);
motor[startServo]=127;
wait1Msec(5000);
}
}

void sortMarbles()
{

while(1==1)
{
	if(SensorValue(marbleSorter)>2044)		//if the marble is opaque
	{
		if(SensorValue(cupReader)==4095)		//if the wood cup is under the reader
		{
			while(SensorValue(cupReader)!= 0)		//while the cup under the reader is not the opaque cup
			{
				motor[cupGears]=20;								//move motor
			}
			motor[sorterGear]=20;			//let the marble go
			wait1Msec(500);						//wait half sec
			motor[sorterGear]=0;			//stop motor
			wait1Msec(500);						//wait half sec
			motor[sorterGear]=-20;		//reset marble stopping gear
			wait1Msec(500);						//wait half sec
			motor[sorterGear]=0;			//stop motor
		}
		else if(SensorValue(cupReader)==2070)		//if the aluminium cup is under the reader
		{
			while(SensorValue(cupReader)!= 0)			//while the cup under the reader is not the opaque cup
			{
				motor[cupGears]=-20;						//move motor
			}
			motor[sorterGear]=20;				//let the marble go
			wait1Msec(500);							//wait half sec
			motor[sorterGear]=0;				//stop motor
			wait1Msec(500);							//wait half sec
			motor[sorterGear]=-20;			//reset marble stopping gear
			wait1Msec(500);							//wait half sec
			motor[sorterGear]=0;				//stop motor
		}
		else										//if the cup under reader is opaque cup
		{
			motor[sorterGear]=20;				//let the marble go
			wait1Msec(500);							//wait half sec
			motor[sorterGear]=0;				//stop motor
			wait1Msec(500);							//wait half sec
			motor[sorterGear]=-20;			//reset marble stopping gear
			wait1Msec(500);							//wait half sec
			motor[sorterGear]=0;				//stop motor
		}
	}



	else if(SensorValue(marbleSorter)<2100 && SensorValue(marbleSorter)>1388) //determines if the marble is aluminium
	{
		if(SensorValue(cupReader)==0)						//if the cup under reader is opaque
		{
			while(SensorValue(cupReader)!= 2070)		//while the cup under reader is not wood
			{
				motor[cupGears]=20;							//move motor
			}
			motor[sorterGear]=20;			//let the marble go
			wait1Msec(500);						//wait half sec
			motor[sorterGear]=0;			//stop motor
			wait1Msec(500);						//wait half sec
			motor[sorterGear]=-20;		//reset marble stopping gear
			wait1Msec(500);						//wait half sec
			motor[sorterGear]=0;			//stop motor
		}
		else if(SensorValue(cupReader)==4095)			//if the cup under the reader is 
		{
			while(SensorValue(cupReader)!= 2070)
			{
				motor[cupGears]=20;
			}
			motor[sorterGear]=20;
			wait1Msec(500);
			motor[sorterGear]=0;
			wait1Msec(500);
			motor[sorterGear]=-20;
			wait1Msec(500);
			motor[sorterGear]=0;
		}
		else
		{
			motor[sorterGear]=20;
			wait1Msec(500);
			motor[sorterGear]=0;
			wait1Msec(500);
			motor[sorterGear]=-20;
			wait1Msec(500);
			motor[sorterGear]=0;
		}
	}



	else										//if marble is wood
	{
		if(SensorValue(cupReader)==0)
		{
			while(SensorValue(cupReader)!= 4095)
			{
				motor[cupGears]=20;
			}
			motor[sorterGear]=20;
			wait1Msec(500);
			motor[sorterGear]=0;
			wait1Msec(500);
			motor[sorterGear]=-20;
			wait1Msec(500);
			motor[sorterGear]=0;
		}
		else if(SensorValue(cupReader)==2070)
		{
			while(SensorValue(cupReader)!= 4095)
			{
				motor[cupGears]=20;
			}
			motor[sorterGear]=20;
			wait1Msec(500);
			motor[sorterGear]=0;
			wait1Msec(500);
			motor[sorterGear]=-20;
			wait1Msec(500);
			motor[sorterGear]=0;
		}
		else
		{
			motor[sorterGear]=20;
			wait1Msec(500);
			motor[sorterGear]=0;
			wait1Msec(500);
			motor[sorterGear]=-20;
			wait1Msec(500);
			motor[sorterGear]=0;
		}
	}
}

}

IMG-3291

1 Like

Moved this to the EDR category.

The first problem I could see in your code is that once main task calls moveMarbles() function the control will never come back and will keep looping inside while(1==1) loop.

So sortMarbles() function is never called.

What you want to do instead is to have a one main loop inside the main task and then call from it short functions that will always return:

task main()
{
   while(true)  // this is the main loop
   {
     letMarbleToTheSensor(); // whatever steps are necessary to get marble to the sensor

     int s = SensorValue(marbleSorter);

     if( s>500 && s<800 ) // particular marble type
     {
         moveCups(1000);
     }
     else if( s>1000 && s<1800 ) // another marble type
     {
         moveCups(2000);
     }
     // etc ...

     letMarbleThrough(); // drop marble into the cup

     wait1Msec(10);
   }
}
1 Like

@technik3k
so we have our code working for the most part, but we forgot that we have to include a stop after 16 run throughs, so we tried to add a for loop, but it wont work

for(int counter=0; counter<=16; counter ++) this is what we have then everything else is after inside parenthesis

1 Like

@gabewells903, it is great that your marble sorter works!

But why wouldn’t a simple for() loop count the marbles?
Did it fail work at all or it just didn’t count properly?

In any case, here is how I usually do this type of the loops:

int marbleCounter = 0;

while(true)  // this is the main loop
{
    letMarbleToTheSensor(); // whatever steps are necessary to get marble to the sensor
    int s = SensorValue(marbleSorter);
    if( s in some expected range ) // marble of one of the types is detected
    {
        marbleCounter++;           // only increment the counter when marble is detected
        moveCups(somePosition);
    }
    else if(...)
    ...
    letMarbleThrough(); // drop marble into the cup

    if( marbleCounter > theNumber )
        break; // this will jump out of the loop and end the program

    wait1Msec(10);
}

The trick is to increment the marbleCounter only when you detect the marble and only once per cycle. You will be counting how many times you had detected the marble and moved the cups to catch it.

After “break” statement exits “while(true){}” loop you will need to reset the power to restart the program. An alternative solution may be to add a button and wait until it is pressed instead of breaking out of the loop.

1 Like

A little tip with the markdown on here, if you put 3 grave symbols above and below your code it will keep its formatting.

Example:
```
//Code here
```

becomes

//Code here
1 Like

Discourse also supports BBCode in addition to Markdown:

One pager Markdown reference: https://commonmark.org/help

So the [code] [/code] tag will work as well:

[code]
int counter=0;
while(true)
{
   read_Markdown_specification();
   if( am_I_feeling_stupid() == true )
   {
       counter++;
   }
   else
   {
       break;
   }
}
printf("Counter value for %s = %d", getUserName(), counter);
[/code]
 
Counter value for technik3k = 99
2 Likes