Your first ChickBot Arduino program

Start up your newly installed Arduino programming software, get your USB lead and ChickBot to hand and we’ll get stuck in!

Like building a house, you can get in to all sorts of trouble trying to build a roof, paint the walls and lay the foundations all at the same time. Everything starts with the foundations and the more solid they are, the better the rest of what you build will be in the future, so don’t rush these early steps.

This program will flash the small light, the Light Emitting Diode or LED, that is in the middle of the Arduino Nano. There are four of them, one LED for power, a couple that flash when the Arduino is communicating over USB and one that you can program.

We’ll then add in the buzzer so it flashes the light and plays a sound.

And then we’ll control it from the push switch that is on the corner of the board.

We will explain what all the commands do once you’ve got something up and running, for now, just play along!

Please read these instructions so you have an overview of what you are doing, then watch the video below.

So, with your Arduino software in front of you, it starts out with a blank program that you can add to. 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 add a line to set pin 13 which the LED is connected to as an output. We only need to do that once at the beginning of the program, so we’ll add it to the setup section:

pinMode(13, OUTPUT);

Feel free to copy & paste from this web page – the commands have to be written exactly otherwise they won’t work, so if you do experience any problems, check that what you’ve typed is what is on this page.

Your setup section should now look like this:

void setup() {
  // put your setup code here, to run once:
  pinMode(13, OUTPUT);
}

And we need to add commands to turn the LED on, wait a short while, turn it off, wait a bit and repeat. This code is this:

digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500);

The delay works in milliseconds, 1000 milliseconds per second, so 500 is the same as half a second. Your loop block should now look like this:

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(13, HIGH);
  delay(500);
  digitalWrite(13, LOW);
  delay(500);
}

Any errors (usually typos) will come up in the black footer area. But all being well, after a few seconds you should see white text telling you some program statistics and once it has downloaded, the LED should start flashing!

 

Let’s add some sound!

The Arduino is a simple command to make a sound:

tone(A0, 262);

And to turn it off:

noTone(A0);

We use the A0 pin because that is the one the buzzer is wired to.

Just add it after the LED on command, so your loop section looks like this:

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(13, HIGH);
  tone(A0, 262);
  delay(500);
  digitalWrite(13, LOW);
  noTone(A0);
  delay(500);
}

Click the download button to try it out. Clearly this will drive everyone crazy, so have fun with it!

Finally, before we get into some of the details so you can expand on this program, let’s get the program to wait before it starts flashing and beeping.

We’ll add a line of code that constantly checks the pushbutton on the corner of the board which is attached in to pin 12 and waits for it to be pressed before continuing:

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

We’ll put this in the setup section so when the program is downloaded, you have to press the button to start things off.

void setup() {
  // put your setup code here, to run once:
  pinMode(13, OUTPUT);
  while(digitalRead(12) == LOW) {}
}

Give it a few moments to download and then press the button. There is a reset button on the Nano, press that and the program will restart – a simple way to silence your beeper!

 

 

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