loading

The following information is a single lesson in a larger project. Find more great projects here.

Return to Previous Lesson: Setting Up the Circuit

Lesson Overview:

Now we'll write our keyboard code!

Step 1: Introduction

In this lesson you will learn how to write the code that connects the buttons to the piezo buzzer. Our version of the Keyboard Instrument circuit is already in the Workplane. It should look a lot like the circuit that you created in the previous lesson!

  1. Continue to the next step.

Step 2: A Few Notes About Arrays

In this program, you’ll need to keep a list of frequencies you want to play when you press each of your buttons. You can start out with the frequencies for middle C, D, E and F (262Hz, 294Hz, 330Hz, and 349Hz). To do this, you’ll need a new kind of variable called an array.

An array is a way to store different values that are related to each other, like the frequencies in a musical scale, using only one name. It makes your programs more efficient!

Two examples of array declaration are below.

Important: the code presented in this step is NOT a part of the Keyboard Instrument program!

  1. When you declare an array, name a datatype (like int or float). The number in [square brackets] is the number of elements in the array.
  2. After you declare the array, you can assign a specific value within it. Reference the individual element using the array name and then the index of the item you want to address. The index refers to the order in which the items appear when the array is created. The first item in the array is item 0, the second is item 1, and so forth. Below, we set the first entry in an array called switches[] using the "==" operator.
  3. Continue to the next step to start writing the Keyboard Instrument code.

Step 3: Create an Array of Frequencies

In the header of your code, set up an array of four notes using the following frequencies: 262, 294, 330, and 349 Hz. Make this array a global variable by declaring it before the setup() function. All of these numbers are integers, so we can use data type int.

  1. Copy the code below into the Code Editor: int notes[ ] = {262,294,330,349};
  2. Continue to the next step.
  3. Stuck? HINT: A global variable is one that is declared outside of a function. Its definition applies to the entire program.

Step 4: Begin Serial Communication

In your setup() function, start serial communication with the computer.

Notice that this is the entire setup() section!

  1. Copy the code below into the Code Editor. void setup(){ Serial.begin(9600); }

  2. Continue to the next step.

Step 5: Read and Print the Analog Value

In the loop() function, declare a local variable to hold the value read from pin A0. Each button input will have a different value associated with it (between 0 and 1023).

To see these values, add the line Serial. println(keyVal) to send them to the computer.

  1. Copy the code below into the Code Editor. void loop(){ int keyVal = analogRead(A0); Serial.println(keyVal);

  2. The readout in the serial monitor will have one number in each line that will change when you press each button.

  3. Continue to the next step.

Step 6: Determine Which Note to Play

Using an if()...else statement, you can assign each value read from A0 to a different musical tone. All if() statements are shown below.

When you cal the tone() function, you will select a frequency from the notes[] array that you created at the beginning of the program.

It’s possible your circuit is a little “noisy” and the input values may fluctuate a little bit while pressing a switch. To accommodate for this variation, it’s a good idea to check for a small range of values instead of an exact value calculated from the circuit diagram.

You can use the && comparison to see if multiple conditions are true.

  1. Copy the code below into the Code Editor. if(keyVal ==1023){ tone(8, notes[0]); } else if(keyVal >= 990 && keyVal <=1010){ tone(8, notes[1]); } else if(keyVal >= 505 && keyVal <= 515){ tone(8, notes[2]); } else if(keyVal >= 5 && keyVal <=10){ tone(8, notes[3]); }
  2. If you press the first button, notes[0] will play. If you press the second, notes[1] will play, if you press the third, notes[2] will play, and if you press the fourth, notes[3] will play. Clearly, the array method of programming is pretty efficient!
  3. Continue to the next step.
  4. Stuck? HINT: The values included in the example program are ballpark figures for these resistor sizes. As all resistors have some error, so these may not work exactly for you. Use can use the information from the serial monitor after running the program to adjust the if() statements, if necessary.

Step 7: When to Stop Playing

To stop playing notes when there is no button being pressed, call the noTone() function, providing the pin number to stop playing sound on.

  1. Copy the code below into the Code Editor. else{ noTone(8); } } //end of loop()

  2. This is the end of the program! Remember to inclued the last {curly bracket}.

  3. Continue to the next step.

Step 8: Use It! (simulator)

Go ahead and press the Upload & Run button to start the program! Since the buzzer in the simulator does not make any noise, you will need to watch the input values change in the serial monitor.

  1. Run the program, and watch the results in the serial monitor as you press each button.

  2. Try pressing each button and watch the input values in the Serial Monitor. You should see 1023, 1001, 512, and 10.

  3. Continue to the next step.

Step 9: Use It! (Arduino Kit)

Things are a little more fun with the Arduino kit because the buzzer actually makes some noise!

  1. Upload your sketch to the Arduino Uno!
  2. If your resistors are close in value to those in the example program, you should hear some sounds from the piezo when you press the buttons.
  3. If you don't hear a sound, check the serial monitor to make sure each of the buttons is in a range that corresponds to the notes in the if()...else statement. If you’re hearing a sound that seems to stutter, try increasing the range in the if() statements a little bit.
  4. Now play some tunes on your button keyboard!
  5. Continue to the next step.

Step 10: Experiment With the Circuit

What would happen if you press two buttons at the same time? You can try it out on the virtual circuit, or with your Arduino kit.

Take at look at the circuit diagram for the resistor ladder to interpret your results.

  1. When buttons are pressed at the same time, their resistors are hooked up in parallel to each other in the voltage divider, creating a new combined resistance, and therefore a new input value for pin A0. (see hint for an example)
  2. How would you use this information to create a new custom input?
  3. Continue to the next step.
  4. Stuck? HINT: For example, if buttons 2 and 3 are pressed their combined resistance is 220 ohm and 10 k-ohm resistor in parallel. 1/R_combined = 1/220 + 1/10,000 R_combined = 250 ohms

Step 11: Think About It...

If you replace the buttons and resistor ladder with analog sensors, can you use the additional information they give you to create a more dynamic instrument? You could use the value to change the duration of a note or create a sliding scale of sounds like in Project 6, the Light Theremin.

  1. In your program, you can also experiment with different frequencies to expand your musical output. You can find frequencies of musical notes on this page: arduino.cc/frequencies

  2. Continue to the next step.

Step 12: Review

In this project you used an analog input with 4 buttons attached to activate a sound buzzer with the tone() function. Resistor ladders are an easy way to get a lot of digital inputs into a system by plugging them all into a single analog input. Each button input results in a different voltage delivered to the analog pin.

You also used arrays for the first time in this project. Arrays are useful for grouping similar types of information together, and can make your programs a lot more concise!

  1. Continue to the next lesson to learn how to create a paper interface for your Keyboard Instrument.

Next Lesson:Making a Keyboard Enclosure

About This Instructable

913views

0favorites

License:

More by circuits:Introduction Arduino Project 15: Hacking Buttons Arduino Project 14: Tweak the Arduino Logo 
Add instructable to: