Little Instrument

Submitted by mschiff on Tue, 03/05/2013 - 22:27
mschiff's picture

Description

For this assignment I made a small instrument.  You can make a sound by tapping or pressing on the force sensor, and you can continuously change the pitch with the potentiometer.

 

Materials

  • Resistor x1
  • Force Sensitive Resistor x1
  • Piezo Buzzer x1
  • Arduino x1
  • Breadboard x1
  • Jumper wires

Code

const int buzzerPin = 7;
const int forcePin = 0;
const int freqPin = 1;

void setup() {
  pinMode(buzzerPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  while (analogRead(forcePin) > 0) {
    playNote(map(analogRead(freqPin), 0, 1023, 260, 12000));
  }
}

void playNote(int freq) {
  digitalWrite(buzzerPin, HIGH);
  delayMicroseconds((float) (1.0/freq)*1000000);
  digitalWrite(buzzerPin, LOW);
  delayMicroseconds((float) (1.0/freq)*1000000);
}

 

0
Your rating: None
Drupal theme by Kiwi Themes.