2014年12月27日土曜日

Arduinoとタッチセンサで電子ピアノ

Arduinoとタッチセンサを組み合わせて、ピアノを作ってみるという記事を見つけました。さっそく、まねをして自分でも動作を確かめてみることにします。


回路

Arduino Uno とブレッドボードを使って下記のような回路を組んでみました。

タッチセンサに接続する抵抗は1M〜10MΩ、スピーカーには220Ωを使いました。

Arduinoのコード

使用しているライブラリは Capacitive Sensing Library です。音を出すにはtone関数を使いました。

#include 
#include "pitches.h" 
/*
* pitches.hは、下記サイト内のCapSensePiano.zipをダウンロードして使いました。
* http://www.instructables.com/id/Capacitive-Touch-Arduino-Keyboard-Piano
*/

/*
 * CapitiveSense Library Demo Sketch
 * Paul Badger 2008
 * Uses a high value resistor e.g. 10M between send pin and receive pin
 * Resistor effects sensitivity, experiment with values, 50K - 50M. Larger resistor values yield larger sensor values.
 * Receive pin is the sensor pin - try different amounts of foil/metal on this pin
 */
// CapacitiveSensorLibrary内にあるExamples(Paulさんのデモスケッチ)です。
// 最後にちょっと追記しました。
 
// Each key corresponds to a note, which are defined here. Uncomment the scale that you want to use:
int notes[]={NOTE_C4,NOTE_D4,NOTE_E4,NOTE_F4,NOTE_G4,NOTE_A4,NOTE_B4,NOTE_C5}; 

#define BUZZER_PIN      A4   // The output pin for the piezo buzzer
#define CAP_THRESHOLD   150  // Capactive reading that triggers a note (adjust to fit your needs)

CapacitiveSensor   cs_4_2 = CapacitiveSensor(4,2);        // 10M resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil if desired
CapacitiveSensor   cs_4_6 = CapacitiveSensor(4,6);        // 10M resistor between pins 4 & 6, pin 6 is sensor pin, add a wire and or foil
CapacitiveSensor   cs_4_8 = CapacitiveSensor(4,8);        // 10M resistor between pins 4 & 8, pin 8 is sensor pin, add a wire and or foil

void setup()                    
{
   cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF);     // turn off autocalibrate on channel 1 - just as an example
   Serial.begin(9600);
}

void loop()                    
{
    long start = millis();
    long total1 =  cs_4_2.capacitiveSensor(30);
    long total2 =  cs_4_6.capacitiveSensor(30);
    long total3 =  cs_4_8.capacitiveSensor(30);

    Serial.print(millis() - start);        // check on performance in milliseconds
    Serial.print("\t");                    // tab character for debug windown spacing

    Serial.print(total1);                  // print sensor output 1
    Serial.print("\t");
    Serial.print(total2);                  // print sensor output 2
    Serial.print("\t");
    Serial.println(total3);                // print sensor output 3

    delay(10);                             // arbitrary delay to limit data to serial port 
    
    if(total1 > CAP_THRESHOLD) {
      tone(BUZZER_PIN, notes[2]); // Plays the note corresponding to the key pressed
    }else if(total2 > CAP_THRESHOLD) {
      tone(BUZZER_PIN, notes[4]); // Plays the note corresponding to the key pressed
    }else if(total3 > CAP_THRESHOLD) {
      tone(BUZZER_PIN, notes[6]); // Plays the note corresponding to the key pressed
    }else{
      noTone(BUZZER_PIN);
    }
}

実際の動作


なかなか感度が良くて、連打にも耐えられます。電子ドラムにしたら楽しいかもしてませんね。ではまた!

0 件のコメント:

コメントを投稿