ChickBot Arduino sings!

Let’s get your ChickBot singing! So start the Arduino software (or select New from the File menu if you already have it running). 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:
}

First of all, we need to tell ChickBot how to sing in tune – and to do that, we need to tell it what notes to play. For this example, we’ll be using just a few notes. Later on, you’ll have access to a whole piano’s worth of notes.

Copy and paste this block above the setup section:

#define NOTE_F3 175
#define NOTE_G3 196
#define NOTE_A3 220
#define NOTE_B3 247
#define NOTE_C4 262

This setups a simple way for us to know what note we want but for the ChickBot to know what that notes is as a frequency. We’ll find out a bit more about music and frequencies in another tutorial, for now, just know that C4 is what is commonly known as middle C.

The program should now look like this:

#define NOTE_F3 175
#define NOTE_G3 196
#define NOTE_A3 220
#define NOTE_B3 247
#define NOTE_C4 262

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

Now we can tell ChickBot which notes to play and how long to play them for with this:

int tune[] = { NOTE_C4, 4, NOTE_G3, 8, NOTE_G3, 8, NOTE_A3, 4, NOTE_G3, 4, 0, 4, NOTE_B3, 4, NOTE_C4, 4, 0, 0 };

You can see we have pairs to tell the program the note and its duration using whole, half, quarter (4) and eighths (8) as sheet music To put a space, we use a note of 0 and we put 0, 0 to indicate no note, no duration so the program knows when to end.

This needs to be put after the note definitions and before the setup section.

So far, so good, your program should look like this:

#define NOTE_F3 175
#define NOTE_G3 196
#define NOTE_A3 220
#define NOTE_B3 247
#define NOTE_C4 262

int tune[] = { NOTE_C4, 4, NOTE_G3, 8, NOTE_G3, 8, NOTE_A3, 4, NOTE_G3, 4, 0, 4, NOTE_B3, 4, NOTE_C4, 4, 0, 0 };

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

Now for the big block of code that take the note frequencies that have been added to the tune and plays them for us. Definitely copy & paste time!

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

int pace = 1000;
float space = 1.2;

for (int note = 0; tune[note+1] != 0; note = note + 2) {

int duration = pace / tune[note+1];

tone(A0, tune[note], duration);

int pause = duration * space;

delay(pause);

}

It steps through the pairs, picking up the information on what note and duration to play, until it detects that it has reached the end.

You may have spotted our special ‘wait for the button to be pushed’ code right at the top!

This goes inside the loop section and, hopefully, your program should now look like this:

#define NOTE_F3 175
#define NOTE_G3 196
#define NOTE_A3 220
#define NOTE_B3 247
#define NOTE_C4 262

int tune[] = { NOTE_C4, 4, NOTE_G3, 8, NOTE_G3, 8, NOTE_A3, 4, NOTE_G3, 4, 0, 4, NOTE_B3, 4, NOTE_C4, 4, 0, 0 };

void setup() {
  // put your setup code here, to run once:
}
void loop() {
  // put your main code here, to run repeatedly:
  while(digitalRead(12) == LOW) {}

  int pace = 1000; 
  float space = 1.2; 

  for (int note = 0; tune[note+1] != 0; note = note + 2) {

    int duration = pace / tune[note+1];

    tone(A0, tune[note], duration);

    int pause = duration * space;

    delay(pause);

  }
}

This program doesn’t need any setup, so don’t worry, no code to put in there!

You can then click the Upload button which will prepare the program and send it to your ChickBot. Once it’s done, press the button to hear the tune. Press again to hear again!

How about a different tune? Replace the tune line with this:

int tune[] = { NOTE_F3, 8, NOTE_G3, 8, NOTE_A3, 8, NOTE_C4, 4, NOTE_A3, 8, NOTE_C4, 4, 0, 0 };

If you don’t yet know how to read music, we’ll show you how to do that in another tutorial. If you can read music, you can put your own tunes in – at the bottom of this page is the definitions for two octaves including the sharps. Remember, C4 is middle C.

But now, we will expand on our button pressing, tune playing, moving backwards ChickBot program so you can get it dancing and singing to your commands.

 

#define NOTE_C3 131
#define NOTE_CS3 139
#define NOTE_D3 147
#define NOTE_DS3 156
#define NOTE_E3 165
#define NOTE_F3 175
#define NOTE_FS3 185
#define NOTE_G3 196
#define NOTE_GS3 208
#define NOTE_A3 220
#define NOTE_AS3 233
#define NOTE_B3 247
#define NOTE_C4 262
#define NOTE_CS4 277
#define NOTE_D4 294
#define NOTE_DS4 311
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_FS4 370
#define NOTE_G4 392
#define NOTE_GS4 415
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_B4 494
#define NOTE_C5 523