Getting your ChickBot Arduino moving

Like the first light & tone program, there are some exciting bits which you need to take on trust. Copy & paste, get your ChickBot moving, make changes and you’ll soon be on your way.

Copy & paste may seem like a bit of a cheat, but once you understand a module, you don’t need to prove that you can type it out by hand.

Fire up the Arduino software which starts out with a blank program that you can add to. Like before, it should look like this:

void setup() {
  // put your setup code here, to run once:
}
void loop() {
  // put your main code here, to run repeatedly:
}

We need to setup the outputs so add this to the setup section:

DDRD = B11111111;
DDRB = B11111111;

Again we’ll explain the details at the end, once you’ve got your ChickBot moving.

Copy & paste is best – the commands have to be written exactly otherwise they won’t work.

Your setup section should now look like this:

void setup() {
  // put your setup code here, to run once:
  DDRD = B11111111; 
  DDRB = B11111111;
}

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:

void RightStep(int stp) {
byte rightSteps[] = { B00100000, B00110000, B00010000, B00011000, B00001000, B00001100, B00000100, B00100100 };
byte rightDMask = B00111100;
PORTD = (PORTD & ~rightDMask) | (rightSteps[stp] & rightDMask);
}

void LeftStep(int stp) {
byte leftStepsD[] = { B01000000, B11000000, B10000000, B10000000, B00000000, B00000000, B00000000, B01000000 };
byte leftStepsB[] = { B00000000, B00000000, B00000000, B00000001, B00000001, B00000011, B00000010, B00000010 };
byte leftDMask = B11000000;
byte leftBMask = B00000011;
PORTD = (PORTD & ~leftDMask) | (leftStepsD[stp] & leftDMask);
PORTB = (PORTB & ~leftBMask) | (leftStepsB[stp] & leftBMask);
}

Your WHOLE program should now look like this:

void setup() {
 // put your setup code here, to run once:
 DDRD = B11111111; // set PORTD 0-7 to outputs
 DDRB = B11111111; // set PORTB 8-13 to outputs 
}

void loop() {
 // put your main code here, to run repeatedly:
 
}

// Set the coil outputs for right hand stepper motor
void RightStep(int stp) {
 byte rightSteps[] = { B00100000, B00110000, B00010000, B00011000, B00001000, B00001100, B00000100, B00100100 };
 byte rightDMask = B00111100;
 PORTD = (PORTD & ~rightDMask) | (rightSteps[stp] & rightDMask);
}

void LeftStep(int stp) {
 byte leftStepsD[] = { B01000000, B11000000, B10000000, B10000000, B00000000, B00000000, B00000000, B01000000 };
 byte leftStepsB[] = { B00000000, B00000000, B00000000, B00000001, B00000001, B00000011, B00000010, B00000010 };
 byte leftDMask = B11000000;
 byte leftBMask = B00000011;
 PORTD = (PORTD & ~leftDMask) | (leftStepsD[stp] & leftDMask);
 PORTB = (PORTB & ~leftBMask) | (leftStepsB[stp] & leftBMask);
}

Now we can add in the code that calls these two modules with the correct step sequence. Again, more code that needs explaining, but let’s get moving first! Copy & paste this in to the loop section:

for (int count = 0; count < 1000; count = count + 1) {
RightStep(count % 8);
LeftStep(count % 8);
delay(1);
}

Your loop section should now look like this:

void loop() {
  // put your main code here, to run repeatedly:
  for (int count = 0; count < 1000; count = count + 1) {
    RightStep(count % 8);
    LeftStep(count % 8);
    delay(1);
  }
}

 

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:

while(digitalRead(12) == LOW) {}

And your setup section should look like this:

void setup() {
 // put your setup code here, to run once:
 DDRD = B11111111; // set PORTD 0-7 to outputs
 DDRB = B11111111; // set PORTB 8-13 to outputs 
 while(digitalRead(12) == LOW) {}
}

Coming up next

The next thing to try is getting your ChickBot to play a little tune.

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