Monday, December 14, 2009

Lilpad Piezo code

int PadPin1 = 1;
int PadPin2 = 2;
int PadPin3 = 3;
int PadPin4 = 4; // these are analog pins for input
int speakerPin = 6; // set the speaker pin on digital pin 6
int padLimit = 50; // the limit/ threshold at which to activate the logic
int howHard = 50; // variable to store how hard the sensor is hit, the lower the value the harder the hit
int Cnote = 1915; // set the pulse on/off time for each note.
int Dnote = 1700;
int Enote = 1519;
int Fnote = 1432;

void setup(){
pinMode(speakerPin, OUTPUT); // set the speaker as output
pinMode(PadPin1, INPUT); // set the piezo speakers as inputs
pinMode(PadPin2, INPUT);
pinMode(PadPin3, INPUT); // set the piezo speakers as inputs
pinMode(PadPin4, INPUT);
}

void soundwave(int note, int howHard ) { // function that takes 2 parameters, the note we want to play and how hard the sensor has been hit
unsigned long endSoundWave = micros() + (35000 * howHard); // start a count from the microseconds currently registered since the program first ran. And add on an arbitary value multiplied by howHard value
while(micros() < endSoundWave){ // while the count is not reached
analogWrite(speakerPin, 1023); // set the speaker to on/ high
delayMicroseconds(note); // wait the number of microseconds for the note
analogWrite(speakerPin, 0); // set the speaker to off/ low
delayMicroseconds(note); // wait for the same number again to complete our oscillation/ soundwave.
} // repeat for the length of time.
}

void loop(){

if (analogRead(PadPin1) < padLimit) { // if the sensor is hit and the reading is less than our threshold // turn an LED on.
while (analogRead(PadPin1) < padLimit) { // while the analog reading is less than the limit
if (analogRead(PadPin1) < howHard) { // if the reading valueis less than our howHard value (1024)
howHard = padLimit - analogRead(PadPin1); // then rewrite howHard with the new value
} // this allows us to capture the peak value.
soundwave(Cnote, howHard); // now generate the soundwave with our note and how had the sensor was hit
}
}

if (analogRead(PadPin2) < padLimit){
while (analogRead(PadPin2) < padLimit) {
if (analogRead(PadPin2) < howHard) {
howHard = padLimit - analogRead(PadPin2);
}
soundwave(Dnote, howHard);
}
}

if (analogRead(PadPin3) < padLimit) { // if the sensor is hit and the reading is less than our threshold // turn an LED on.
while (analogRead(PadPin3) < padLimit) { // while the analog reading is less than the limit
if (analogRead(PadPin3) < howHard) { // if the reading valueis less than our howHard value (1024)
howHard = padLimit - analogRead(PadPin3); // then rewrite howHard with the new value
} // this allows us to capture the peak value.
soundwave(Enote, howHard); // now generate the soundwave with our note and how had the sensor was hit
}
}
if (analogRead(PadPin4) < padLimit) { // if the sensor is hit and the reading is less than our threshold // turn an LED on.
while (analogRead(PadPin4) < padLimit) { // while the analog reading is less than the limit
if (analogRead(PadPin4) < howHard) { // if the reading valueis less than our howHard value (1024)
howHard = padLimit - analogRead(PadPin4); // then rewrite howHard with the new value
} // this allows us to capture the peak value.
soundwave(Fnote, howHard); // now generate the soundwave with our note and how had the sensor was hit
}
}
}

No comments:

Post a Comment