Driving the 4 coils in the stepper motors that have an 8 step sequence requires some pretty tricky code. We are using two blocks of code that allow use to control the right and the left stepper motor independently. It’s definitely copy & paste time! This section needs to be pasted BELOW the loop section:

You can then click the Upload button which will prepare the program and send it to your ChickBot. Be ready, as soon as it gets the program, all being well your ChickBot will start moving. Due to a quirk in the way that the outputs & the coils are linked, it will go backwards!

Let’s go further!

The ChickBot moves the number of steps that are set in the FOR loop – the 1000 is the number of steps that we programmed in. The stepper motor requires 4096 steps for a full rotation. Using the power of Maths, with a wheel diameter of 39mm, πD tells us that the circumference is 123mm. So 33.3 steps per millimetre or 333 steps per cm. If you change the 1000 to 3330, we can expect your ChickBot to move 10cm.

Feel free to change the number to see change the distance. You can get it to start moving again after it’s done a run by pressing the reset button that’s in the middle of the Nano board.

You may want to add the ‘wait for button press’ line from your first program to the bottom of the setup section – this will allow you to program the ChickBot without it starting to move as soon as it’s recieved the program, it will wait for the button on the top right to be pressed before it starts. This is the code:

Coming up next

Then we’ll add all these parts together so you can get your ChickBot turning, dancing and singing!

WORK IN PROGRESS

Now you have an example program, here’s some information on the commands we used so you can experiment with timings and tones and maybe move the button press check as well.

In english, this is saying, while pin 12 is LOW (the button makes it HIGH), do the code between { and } and then check again. As there is nothing between the { and the }, it doesn’t do anything, so it just loops round and round while