Robin Newman’s Lego© Mindstorms© Page A beginner’s journey 4

The program starts by calling the zeroturntable block. It then sets the position variable to 0. It then starts the main loop which first calls the closegate block (it will already be closed after the first run round the loop) and then rotates the gate motor another 180 degrees to allow the next ball to enter below the colour sensor. The colour number detected is then read and stored in the lastcolour variable.

Now the program checks to see if a ball was detected. In the following two way switch if the colour is outside the range blue,green,yellow,red then no ball was detected and the switch says No Object Detected, rotates the gate motor 180 degrees to close the upper rod, calls the zeroturntable block to park the arm, and then stops the program.

If a coloured ball was detected then the other branch of the switch does nothing, and the next section of the program works out where the turntable has to be rotated. 5 positions are defined, each one one rotation of the turntable motor apart. If, for example a green (3) ball is detected, the program subtracts one from this number (stored in lastcolour) and then subtracts the position variable value from this (initially 0) giving an answer of 3-1-0=2 The position variable is then updated to hold this number, and it is also fed into the setposition myblock where it will produce a true direction output and a 720 degrees (2 x 360) output. These are fed to the turntable motor A causing it to rotate the turntable 2 position jumps to the green position. If the next colour detect was say Blue (2) then the process would subtract 1 from this and then subtract the current position 2 giving an answer of -1. This would be fed into the setposition block giving a false or reverse direction output and 360 degrees (1 x 360) causing the turntable to move back one position to the blue location.

After the turntable has moved the closegate block is called again. This closes the upper rod gate whilst opening the lower one and releasing the ball to run down the runway to the receptacle. A 30 second wait is then allowed for this to happen, before the main loop restarts. more...