进阶制作

利用Arduino制作键盘乐器

字号+ 作者:duino123.com 来源:未知 2016-04-26 16:11 我要评论( )

利用Arduino制作键盘乐器

利用Arduino制作键盘乐器

SIK Piano Keyboard

我们利用arduino检测手指接触柔性电位带(soft pot)的不同区域来让蜂鸣器发出不同的声调。由于我们选的电位带比较短所以只分成8段,对应C大调音阶。

1.所需材料

Jumper Wires Standard 7" M/M - 20 AWG (30 Pack)
If you need to knock up a quick prototype there's nothing like having a pile of jumper wires to speed things up, and let's face it: sometimes you want…
Piezo Speaker - PC Mount 12mm 2.048kHz
This is a small 12mm round speaker that operates around the audible 2kHz range. You can use these speakers to create simple music or user interfaces. …
Resistor 10k Ohm 1/6th Watt PTH
1/6th Watt, +/- 5% tolerance PTH resistors. Commonly used in breadboards and perf boards, these 10K resistors make excellent pull-ups, pull-downs, and…
SoftPot Membrane Potentiometer - 50mm
These are very thin variable potentiometers. By pressing down on various parts of the strip, the resistance linearly changes from 100Ohms to 10,000Ohm…
Breadboard - Self-Adhesive (White)
This is your tried and true white solderless breadboard. It has 2 power buses, 10 columns, and 30 rows - a total of 400 tie in points. All pins are sp…
Arduino
we use many Arduinos and we're always looking for the simplest, most stable one. Each board is a bit different and no one board has everyt…

2.需要的工具

  • Scissors
  • Masking tape
  • Hobby knife
  • Ruler
  • Pen or Sharpie
  • Cardboard (a small box is preferable)

Other required tools

3.准备知识

Before continuing with this project, we suggest you be familiar with a few concepts:

  • Reading a Soft Potentiometer
  • Using a Piezo Buzzer

4.硬件连接

准备面包板

Before connecting anything, we recommend separating the breadboard into its three parts. Use a pair of scissors to cut the adhesive backing. This part is optional, but it helps the breadboard fit into the project enclosure later.

Cutting the breadboard

元件连接

Once you are ready to connect the components, check out the Fritzing diagram below.

Pay special attention to the component’s markings indicating how to place it on the breadboard. Polarized components can only be connected to a circuit in one direction.

SIK keyboard Fritzing diagram

Having a hard time seeing the circuit? Click on the Fritzing diagram to see a bigger image.

Once you are done, the soft pot should be sticking straight up from the breadboard.

Completed wiring

制作键盘

The “keyboard” is actually the soft potentiometer. We will divide up the soft pot into eight (8) segments, and the resistance we read with the Arduino from touching the soft pot will determine the key being pressed.

To create something that looks like a keyboard, place a piece of masking tape over the soft pot so it covers the entire length with some overhand (we will use the overhang to attach the soft pot to the box).

Tape on the soft pot

Place a ruler next to the touchable (silver) area on the soft pot. Starting from the breadboard end, mark every 6mm. Fill in or note that the first 6mm section is not to be used.

Marking out the keyboard

Starting from the breadboard side, write the note of each key in between the marks. We’ll use C, D, E, F, G, A, B, C.

Marking notes on the keys

制作盒子

While we can play our keyboard on the breadboard, it might be more fun to have it in a project box to add a little stability.

Peel the backing off the two pieces of the breadboard to which we attached components.

Peel off the breadboard backing

Attach the large breadboard piece to the side of the cardboard box with the soft pot lying flat on the box’s floor. Stick the smaller breadboard piece (the one with the buzzer) to the box’s floor opposite the soft pot. Stick the overhang tape on the soft pot to the floor of the box.

Attach the breadboard pieces to the inside of the box

Fold a piece of tape over (into a loop), and stick it to the RedBoard.

Sticking tape to the RedBoard

Place the RedBoard in between the soft pot and top breadboard piece.

Arduino in place

Use the scissors to remove the top of the box.

Cut off the box top

Use the hobby knife to cut a hole in the side of the box so you can pass the USB cable through for power.

Cut a hole in the box

Cut a large notch in the front of the box just above the keyboard. This allows for easier access to the keyboard.

Cut a hole in the front to access the keyboard

5.代码

 

If you would like to use the Arduino IDE, copy the following code into the editor and click the Upload button.

 

/*
 * Description:
 *  Use the soft touch potentiometer as a keyboard segmented into
 *  8 keys: C, D, E, F, G, A, B, C. When each key is pressed, the 
 *  corresponding note is played through a buzzer.
 * 
 * Hardware Connections:
 *  Arduino | Soft Pot | Buzzer
 *  ---------------------------
 *    5V    |   pin 3  |   
 *    A0    |   pin 2  |   
 *    GND   |   pin 1  |   
 *    9     |          |   +
 *    GND   |          |   -
 *    
 *  You will also need to attach a 10k resistor from pin 2 to
 *  pin 1 (GND) on the soft pot.
 *  
 * License:
 *  Public Domain
 */

// Constants
const int SENSOR_PIN = 0;   // Analog input pin for soft pot
const int BUZZER_PIN = 9;   // PWM digital output pin for buzzer
const int DURATION = 10;    // Time (ms) to play a note

// This function is run only once as soon as the Arduino boots
void setup() 
{
  
  // Set the buzzer pin as an output
  pinMode(BUZZER_PIN, OUTPUT);
}

// This gets run over and over right after the setup() function
void loop() 
{
  int sensorValue;
  char note = 0;
  int freq;

  // Read the value (0 - 1023) from the ADC
  sensorValue = analogRead(SENSOR_PIN);

  // Map the key pressed to a note
  note = findNote(sensorValue);

  // If it's a note, play it!
  if ( note != 0 ) {
    freq = getFrequency(note);
    tone(BUZZER_PIN, freq, DURATION);
    delay(DURATION);
  }
}

// Given an ADC value (0 - 1023), map it to a note
char findNote(int val)
{

  // Return the note based on the key pressed
  if ( (val > 10) && (val <= 160) )
  {
    return 'c';
  }
  if ( (val > 160) && (val <= 250) )
  {
    return 'd';
  }
  if ( (val > 250) && (val <= 350) )
  {
    return 'e';
  }
  if ( (val > 350) && (val <= 450) )
  {
    return 'f';
  }
  if ( (val > 450) && (val <= 560) )
  {
    return 'g';
  }
  if ( (val > 560) && (val <= 690) )
  {
    return 'a';
  }
  if ( (val > 690) && (val <= 850) )
  {
    return 'b';
  }
  if ( (val > 850) && (val <= 1023) )
  {
    return 'C';
  }

  // Return 0 to show that no key was pressed
  return 0;
}

// Translate a note (a, b, c, d, e, f, g) to its frequency
int getFrequency(char note) 
{
  int i;
  const int numNotes = 8;  // number of notes we're storing

  // Arrays containing our notes and frequencies
  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
  int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523};

  // Step though the notes
  for (i = 0; i < numNotes; i++)  // Step through the notes
  {

    // If it matches a note in our list, return the frequency
    if (names[i] == note)
    {
      return(frequencies[i]);
    }
  }

  // If we looked through everything and didn't find a note,
  // return 0, as we still need to return something.
  return(0);
}

Code to Note

We pass the findNote(int val) function the analog-to-digital (ADC) value read from the soft pot. This information is stored in the val parameter. If we touch the soft pot towards the breadboard end, it will produce a lower value than if we touched it at the other end. The ADC value on the Arduino can be between 0 and 1023 (inclusive). So, we would read a value close to 10 if we touched it on the breadboard end and a value close to 1023 on the other end.

Since we divided up the soft pot’s length into 6 mm segments, we also need to divide up the values we might receive from the soft pot. Let’s say we touched the soft pot on the third segment (the one we labeled “E”), and the ADC value was 296 as a result. The first two if statements would return false, but the third one would be true, since 296 falls between 250 and 350. So, ‘e’ is returned as the note we pressed on the soft pot.

6.结果

Once you have uploaded the code to the RedBoard, try lightly pressing on the soft potentiometer. You should hear some musical notes from the speaker!

Playing the keyboard instrument

Want to play a song? How about Twinkle, Twinkle, Little Star (‘C’ is the first ‘C’ on the left in this case):

C, C, G, G, A, A, G (hold) F, F, E, E, D, D, C (hold)

7.调试

No Sound

Given the size and shape of the piezo buzzer it is easy to miss the right holes on the breadboard. Try double checking its placement. Additionally, check the wiring for the soft pot.

Can’t Press More than One Key

The soft potentiometer is incapable of detecting more than one press at a time. What happens if you try to press two keys at once?

8.延伸

Taking the Next Steps

Now that you’ve built your keyboard and had a chance to play a song, we can look at taking the project a bit further.

  • If you haven’t already, try playing another song.
  • Right now, the keyboard is set up to play a C major scale. Change it so it plays an E major scale, and then play Twinkle, Twinkle, Little Star in the key of E major. (hint: see this notes to frequencies chart)
  • Change the keyboard so that it has 12 notes (of your choosing) instead of 8.


Frequencies for equal-tempered scale, A4 = 440 Hz

Other tuning choices, A4 =
 432   434   436   438   440   442   444   446 

Speed of Sound = 345 m/s = 1130 ft/s = 770 miles/hr

("Middle C" is C4 )

Note Frequency (Hz) Wavelength (cm)
C0 16.35 2109.89
 C#0/Db0 17.32 1991.47
D0 18.35 1879.69
 D#0/Eb0 19.45 1774.20
E0 20.60 1674.62
F0 21.83 1580.63
 F#0/Gb0 23.12 1491.91
G0 24.50 1408.18
 G#0/Ab0 25.96 1329.14
A0 27.50 1254.55
 A#0/Bb0 29.14 1184.13
B0 30.87 1117.67
C1 32.70 1054.94
 C#1/Db1 34.65 995.73
D1 36.71 939.85
 D#1/Eb1 38.89 887.10
E1 41.20 837.31
F1 43.65 790.31
 F#1/Gb1 46.25 745.96
G1 49.00 704.09
 G#1/Ab1 51.91 664.57
A1 55.00 627.27
 A#1/Bb1 58.27 592.07
B1 61.74 558.84
C2 65.41 527.47
 C#2/Db2 69.30 497.87
D2 73.42 469.92
 D#2/Eb2 77.78 443.55
E2 82.41 418.65
F2 87.31 395.16
 F#2/Gb2 92.50 372.98
G2 98.00 352.04
 G#2/Ab2 103.83 332.29
A2 110.00 313.64
 A#2/Bb2 116.54 296.03
B2 123.47 279.42
C3 130.81 263.74
 C#3/Db3 138.59 248.93
D3 146.83 234.96
 D#3/Eb3 155.56 221.77
E3 164.81 209.33
F3 174.61 197.58
 F#3/Gb3 185.00 186.49
G3 196.00 176.02
 G#3/Ab3 207.65 166.14
A3 220.00 156.82
 A#3/Bb3 233.08 148.02
B3 246.94 139.71
C4 261.63 131.87
 C#4/Db4 277.18 124.47
D4 293.66 117.48
 D#4/Eb4 311.13 110.89
E4 329.63 104.66
F4 349.23 98.79
 F#4/Gb4 369.99 93.24
G4 392.00 88.01
 G#4/Ab4 415.30 83.07
A4 440.00 78.41
 A#4/Bb4 466.16 74.01
B4 493.88 69.85
C5 523.25 65.93
 C#5/Db5 554.37 62.23
D5 587.33 58.74
 D#5/Eb5 622.25 55.44
E5 659.25 52.33
F5 698.46 49.39
 F#5/Gb5 739.99 46.62
G5 783.99 44.01
 G#5/Ab5 830.61 41.54
A5 880.00 39.20
 A#5/Bb5 932.33 37.00
B5 987.77 34.93
C6 1046.50 32.97
 C#6/Db6 1108.73 31.12
D6 1174.66 29.37
 D#6/Eb6 1244.51 27.72
E6 1318.51 26.17
F6 1396.91 24.70
 F#6/Gb6 1479.98 23.31
G6 1567.98 22.00
 G#6/Ab6 1661.22 20.77
A6 1760.00 19.60
 A#6/Bb6 1864.66 18.50
B6 1975.53 17.46
C7 2093.00 16.48
 C#7/Db7 2217.46 15.56
D7 2349.32 14.69
 D#7/Eb7 2489.02 13.86
E7 2637.02 13.08
F7 2793.83 12.35
 F#7/Gb7 2959.96 11.66
G7 3135.96 11.00
 G#7/Ab7 3322.44 10.38
A7 3520.00 9.80
 A#7/Bb7 3729.31 9.25
B7 3951.07 8.73
C8 4186.01 8.24
 C#8/Db8 4434.92 7.78
D8 4698.63 7.34
 D#8/Eb8 4978.03 6.93
E8 5274.04 6.54
F8 5587.65 6.17
 F#8/Gb8 5919.91 5.83
G8 6271.93 5.50
 G#8/Ab8 6644.88 5.19
A8 7040.00 4.90
 A#8/Bb8 7458.62 4.63
B8 7902.13 4.37

(To convert lengths in cm to inches, divide by 2.54) 


如需更详细资料,如有好资源分享,请加入QQ交流群:214461008

欢迎阅读,欢迎转载,不用注明出处!本站只提供入门和进阶资料,做您学习的小伙伴!

请加群:214461008

相关文章
  • 激光雕刻机步骤

    激光雕刻机步骤

    2017-01-17 21:28

  • Arduino与A4988驱动42步进电机

    Arduino与A4988驱动42步进电机

    2017-01-17 09:00

  • 如何利用Arduino播放音乐

    如何利用Arduino播放音乐

    2016-06-02 21:06

  • 移位寄存器在Arduino上的应用

    移位寄存器在Arduino上的应用

    2016-05-13 16:05

网友点评
Arduino
热门教程
关注我们
自动化世界