Step 8Arduino, Speaker, and Code
The code is as follows:
int noteIndex=999;
int sensorPin = 2; // select the input pin for the potentiometer
int speakerPin = 9;
int sensorValueOld = PI ;
int sensorValue = 0; // variable to store the value coming from the sensor
int length = 15; // the number of notes
char notes[] = "cDfcDFfDc"; // a space represents a rest
int beats[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
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', 'C', 'd', 'D', 'e', 'f', 'F', 'g', 'G', 'a', 'A', 'b','S'};
int tones[] = {
1915, 1804, 1700, 1607,
1519, 1432, 1351, 1275,
1204, 1136, 1073, 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() {
// declare the ledPin as an OUTPUT:
pinMode(speakerPin, OUTPUT);
pinMode(sensorPin, INPUT);
digitalWrite(sensorPin,HIGH);
Serial.begin(9600);
}
void loop() {
// read the value from the sensor:
sensorValue = digitalRead(sensorPin);
if(sensorValueOld == sensorValue){
// do nothing!
} else { // value has changed! Lazy Susan has moved!
Serial.println(sensorValue);
// point to next note in sequence
noteIndex++;
if(noteIndex>=strlen(notes)){noteIndex=0;}
playNote(notes[noteIndex], beats[noteIndex] * tempo);
delay(tempo / 2);
}
sensorValueOld = sensorValue;
/*
if (sensorValue != 0){
for (int i = 0; i < length; i++) {
if (notes[i] == ' ') {
delay(beats[i] * random(PI,tempo)); // rest
}
else {
playNote(notes[i], beats[i] * tempo);
}
// pause between notes
delay(tempo / 2);
}
}
*/
}
| « Previous Step | Download PDFView All Steps | Next Step » |














































and also... someone suggested that i should even use a motor from an electric fan or from a ceiling fan in order to make it spin... my teacher told us that any engine's speed can be reset from slow to fast.. so... should i stick with this idea? since the materials are too hard to find?
kindly reply soon... i really really really need your help~!!!! i hope you can really help me though,... firthermore, thank you for working on with such a nice project~! it really really inspired me to do so much~!
LOVE,
Soraya Danica D. Dimayuga, age 16, from the philippines
Thanks for your interest! Feel free to copy and modify this! I'm glad we can help! Here is the reply to your questions. hope it helps!
You can get the arduino board online : http://arduino.cc/en/Main/ I believe a foam board and materials in step 7 can be easily found in art/crafts stores. We simply got most of them from a hallway of free/reusable pile. A servo should be good to make it spin. you can find it online or at stores like radio shark too. The foam is very light so I don't think you have to worry about the motor too much unless you want to put really heavy food on it.