Color sorting

2 replies [Last post]
Title: NooBot
Joined: 04/09/2015
BotPoints: 9
User offline. Last seen 2 years 42 weeks ago.

Having an issue while color sorting. I have a function that basically spins a conveyor belt and then checks the color of the pom in the box. I run this function in a loop in main. It runs once just fine, and works with either color. However, it only runs once. And then the robot stops. It never even reaches the return at the end of main.

Here is the code:

void sort_color(){

if (get_object_count(0)>0){ //if red
set_servo_position(3, 360); //sweeps to left
set_servo_position(3, 1040);

}else if(get_object_count(1)>0){ //if green
set_servo_position(3, 1600); //sweeps to right
set_servo_position(3, 1040);
}else{ //if no pom
mav(0, -1000);
msleep(1200); //spin more to try to find a pom


Any help would be greatly appreciated. Thanks! :)

Title: NooBot
Joined: 04/06/2015
BotPoints: 16
User offline. Last seen 2 years 49 weeks ago.

in your program try adding a timed while loop use the code
double start = seconds();
// While the current time minus the starting time is less than 5 seconds
while(seconds() - start < 5.00)
// do something

this will keep on running your program till the time is up.....

Title: NooBot+
Joined: 06/17/2009
BotPoints: 24
User offline. Last seen 1 year 8 weeks ago.

If I understand you right, your main looks something like this:

int main() {
    while (some stopping condition) {
    return 0;

Is that right? If so, then your sort_camera function should definitely NOT include the statement:


Opening the camera should happen only ONCE in the program's run. I am unsure what happens if you attempt to open it again, but I would not be surprised to discover that the program hangs on the second attempt.

Additionally, your call to enable_servos omits the parentheses. It should be:


and it should also NOT be in the sort_color function, since you want it to happen only ONCE during the program's run.

Additionally, it looks like you omitted an msleep after the first set_servo_position call (doing two in a row without a sleep in between is pointless).

Fix those and then ask again (and submit your entire program, including main) if that does not work.