Thursday, October 22, 2009

10/20

Prototype 1: Melody

Description:
Here we hooked the piezo element we picked up directly to the arduino board and entered the melody tutorial code off the arduino website. and it worked the first time to our surprise.

Input:
Code Sequence
The calculation of the tones is made following the mathematical operation:
      timeHigh = period / 2 = 1 / (2 * toneFrequency)

where the different tones are described as in the table:

note    frequency    period    timeHigh

c 261 Hz 3830 1915
d 294 Hz 3400 1700
e 329 Hz 3038 1519
f 349 Hz 2864 1432
g 392 Hz 2550 1275
a 440 Hz 2272 1136
b 493 Hz 2028 1014
C 523 Hz 1912 956


Output:
Piezo Buzzer, Annoying Sound

Setup:














Code:
int speakerPin = 9;


int length = 15; // the number of notes
char notes[] = "ccggaagffeeddc "; // a space represents a rest
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
int tempo = 300;

void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
}

void playNote(char note, int duration) {
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };

// play the tone corresponding to the note name
for (int i = 0; i < 8; i++) {
if (names[i] == note) {
playTone(tones[i], duration);
}
}
}

void setup() {
pinMode(speakerPin, OUTPUT);
}

void loop() {
for (int i = 0; i < length; i++) {
if (notes[i] == ' ') {
delay(beats[i] * tempo); // rest
} else {
playNote(notes[i], beats[i] * tempo);
}

// pause between notes
delay(tempo / 2);
}
}

Result:


Prototype 2: Melody + LED

Description:
Found another melody code that played a slightly different tune in accordance with a LED.

Input:
Code Sequence

OutPut:
Piezo Buzzer + LED, Annoying Sound and Annoying Light

Setup:

Same as Prototype 1 with additional LED in Pin 13

Code:
/* Play Melody

* -----------
*
* Program to play melodies stored in an array, it requires to know
* about timing issues and about how to play tones.
*
* The calculation of the tones is made following the mathematical
* operation:
*
* timeHigh = 1/(2 * toneFrequency) = period / 2
*
* where the different tones are described as in the table:
*
* note frequency period PW (timeHigh)
* c 261 Hz 3830 1915
* d 294 Hz 3400 1700
* e 329 Hz 3038 1519
* f 349 Hz 2864 1432
* g 392 Hz 2550 1275
* a 440 Hz 2272 1136
* b 493 Hz 2028 1014
* C 523 Hz 1912 956
*
* (cleft) 2005 D. Cuartielles for K3
*/

int ledPin = 13;
int speakerOut = 9;
byte names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};
int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
byte melody[] = "2d2a1f2c2d2a2d2c2f2d2a2c2d2a1f2c2d2a2a2g2p8p8p8p";
// count length: 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
// 10 20 30
int count = 0;
int count2 = 0;
int count3 = 0;
int MAX_COUNT = 24;
int statePin = LOW;

void setup() {
pinMode(ledPin, OUTPUT);
pinMode(speakerOut, OUTPUT);
}

void loop() {
digitalWrite(speakerOut, LOW);
for (count = 0; count < MAX_COUNT; count++) {
statePin = !statePin;
digitalWrite(ledPin, statePin);
for (count3 = 0; count3 <= (melody[count*2] - 48) * 30; count3++) {
for (count2=0;count2<8;count2++) {
if (names[count2] == melody[count*2 + 1]) {
digitalWrite(speakerOut,HIGH);
delayMicroseconds(tones[count2]);
digitalWrite(speakerOut, LOW);
delayMicroseconds(tones[count2]);
}
if (melody[count*2 + 1] == 'p') {
// make a pause of a certain size
digitalWrite(speakerOut, 0);
delayMicroseconds(500);
}
}
}
}
}
Result:


Prototype 3: Buzzer + Photoresistor

Description:
This attempt was set up for the piezo buzzer to ring according to a photoresistor. It should vary the pitch according to light levels

Input:
Photoresistor Light Level

Output:
Piezo Buzzer, Really Annoying Sound

Setup:



















Code:
int photosensorPin = 0;

int piezoPin = 9;

int val = 0;

void setup() {
pinMode(piezoPin, OUTPUT);
}

void loop() {
digitalWrite(piezoPin, LOW);
val = analogRead(photosensorPin);
val = val/2;

for( int i=0; i<500; color="#777755">// play it for 50 cycles
digitalWrite(piezoPin, HIGH);
delayMicroseconds(val);
digitalWrite(piezoPin, LOW);
delayMicroseconds(val);
}
}

Result:


Prototype 4: Knock Sensor + LED

Description:
We finally had a breakthrough an got this knocker working. We figured out that our resistor was too tiny after visiting this site, Noticing that our resistor was not a 'brown black green' resistor. The previous experiment's success gave us the idea to replace the resistor with the photoresistor to see if would work then. We also tried attaching all 5 of our tiny resistors to see if it would amount to the 1Mohm, but we either were 5 short or this method does not work.
Once we entered the code the light was blinking uncontrollably and we figured that we had to adjust the threshold level. turned out the default was way to sensitive and we changed the 100 to 20.

Input:
Knocking (vibration)

Output:
LED

Setup:













Code:
/* Knock Sensor

* ----------------
*
* Program using a Piezo element as if it was a knock sensor.
*
* We have to basically listen to an analog pin and detect
* if the signal goes over a certain threshold. If the Threshold
* is crossed, and toggles the LED on pin 13.
*
* Modified from one by (cleft) 2005 D. Cuartielles for K3
*/

int ledPin = 13;
int knockSensor = 2;
byte val = 0;
int statePin = LOW;
int THRESHOLD = 100; // if it's too sensitive then change this value

void setup()
{
pinMode(ledPin, OUTPUT);
}

void loop()
{
val = analogRead(knockSensor);
if (val >= THRESHOLD) { // check the piezo
statePin = !statePin; // sets the pin to the opposite state
digitalWrite(ledPin, statePin); // turn on or off the LED
}
}

Result:







Labels:


Comments:

Post a Comment





<< Home

This page is powered by Blogger. Isn't yours?