Nibbles and Bits
The Care and Feeding of My Pet Arduino


by Budd Churchward - WB7FHC - NIBBLES AND BITS LIBRARY

'My Dog Has Fleas' - A Binary Music Machine
« 1 2 3 4 5 6 7 8  9  10 11 12 »

Section 9 - Four Buttons → Fifteen Notes [Changing the Sketch]
Begin by deleting the lines highlighted in orange as you see on the right. Then add the two lines highlighted in yellow in their place.

We are creating two sets of data. They are often called 'arrays'.

You will see that they are the same values we saw in our frequency chart. Notice that each set begins with the value '0'. The reason for this has to do with how data is accessed in an array.

Our sketch will point to a frequency with a number representing its position in the array. It is important to know that we start numbering these positions with zero. We are not going to have a note numbered '0' so we have put a zero in the first spot of each array as a place holder.

/* Barnacle Budd's Binary Organ - Part 1: MyDogHasFleas v. 0.1
   (c) 2011, Budd Churchward - WB7FHC
   04-10-11
  
  visit www.xxxx.com for hook up instructions
 */

 int button4=7;      // declare top button on pin 7 
 int button3=6;      // declare next button on pin 6 
 int button2=5;      // declare next button on pin 5 
 int button1=4;      // delcare bottom button on pin 4 
 
 int speaker=11;     // declare speaker output on pin 11 
 
 int MY=392;         // freq. of note: G 
 int DOG=262;        // freq. of note: C 
 int HAS=330;        // freq. of note: E 
 int FLEAS=440;      // freq. of note: A 
 
 int duration = 500; // adjust note duration here 
 
 int naturals[] = {0,131,147,165,175,196,220,247,262,294,330,349,392,440,494,523};
 int pentatonic[] = {0,139,156,185,208,233,277,311,370,415,466,554,622,740,831,932}; 



void setup () remains the same with our new version but we will be replacing all of void loop ().

Gut out the lines highlighted in orange and replace them with the two highlighted in yellow.

If you compile this code now, you are going to see errors. In the next section we will write this new procedure called: buildNote().

Next Section »

 void setup() {
   // define 4 input and 1 output pins:
   pinMode(button4,INPUT);
   pinMode(button3,INPUT);
   pinMode(button2,INPUT);
   pinMode(button1,INPUT);
   
   pinMode(speaker,OUTPUT);
 }
 
 void loop() {
   // if button 4 is down play note: G  
   if (digitalRead(button4)) tone(speaker,MY,duration); 
   // if button 3 is down play note: C  
   if (digitalRead(button3)) tone(speaker,DOG,duration); 
   // if button 2 is down play note: E  
   if (digitalRead(button2)) tone(speaker,HAS,duration); 
   // if button 1 is down play note: A  
   if (digitalRead(button1)) tone(speaker,FLEAS,duration); 

  delay(200); 
  buildNote();