Description
An extension of my lab 4 project, I added the speaker to the Pooh bear and now when he is squeezed (depending on how hard) his nose lights up and he 'screams.'
Components Used
Arduino Board
Bread board
1 220 ohm resistor
1 10K ohm resistor
1 LED
1 FSR
1 Piezo speaker
wiring
1 pooh bear
Arduino Code
int potPin = 5; // select the input pin for the potentiometer
int speakerPin = 7;
int ledPin = 9; // select the pin for the LED
int val = 0; // variable to store the value coming from the sensor
void setup() {
pinMode(speakerPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(speakerPin, LOW);
val = analogRead(potPin); // read the value from the sensor, between 0 - 1024
analogWrite(ledPin, val/4); // analogWrite can be between 0-255 (led brightness based on force)
val = val*2; //used for delaying sound from speaker
/*
if (val > 512) {
digitalWrite(speakerPin, HIGH);
}
else {
digitalWrite(speakerPin, LOW);
}
*/
for(int i=0; i<500; i++) { // play it for 50 cycles
digitalWrite(speakerPin, HIGH);
delayMicroseconds(val);
digitalWrite(speakerPin, LOW);
delayMicroseconds(val);
}
}
Images