// Fast ball sorter program // Philo - 2009 task main () { SetSensorColorFull(IN_1); // initialize the color sensor OnFwd (OUT_A, 100); // Start splitter motor while (true) { long time=CurrentTick (); // Wait for detection of a colored ball while( (Sensor(IN_1) == INPUT_WHITECOLOR) || (Sensor(IN_1) == INPUT_BLACKCOLOR)) { if(CurrentTick()-time > 800) // No ball detected for some time { Off(OUT_ABC); // Stop all motors and play end tune PlayToneEx (TONE_A5, 100, 3, false); Wait(100); PlayToneEx (TONE_C5, 100, 3, false); Wait(100); PlayToneEx (TONE_E5, 100, 3, false); Wait(100); PlayToneEx (TONE_G6, 100, 3, false); Wait(100); Stop (true); } } int Color = Sensor(IN_1); if(Color == INPUT_YELLOWCOLOR) // If yellow color, confirm a bit later, could be a red ball. { Wait(15); if(Sensor(IN_1) == INPUT_REDCOLOR) Color = INPUT_REDCOLOR; } ClearScreen(); PlayToneEx (400, 100, 4, false); // According to detected color, flip the switch points and display color name switch(Color) { case 2: OnFwd (OUT_C, 100); OnFwd (OUT_B, 100); TextOut (0, LCD_LINE3, "Blue"); break; case 3: OnFwd (OUT_C, 100); OnRev (OUT_B, 100); TextOut (0, LCD_LINE3, "Green"); break; case 4: OnRev (OUT_C, 100); OnFwd (OUT_B, 100); TextOut (0, LCD_LINE3, "Yellow"); break; case 5: OnRev (OUT_C, 100); OnRev (OUT_B, 100); TextOut (0, LCD_LINE3, "Red"); break; } Wait(100); // the switch point motors run for 100ms Off(OUT_BC); } }