living with the lab robot whistle challenge piezo speaker circuit © 2011 LWTL faculty team.

Download Report

Transcript living with the lab robot whistle challenge piezo speaker circuit © 2011 LWTL faculty team.

living with the lab
robot whistle challenge
piezo speaker circuit
© 2011 LWTL faculty team
living with the lab
wiring and simple programming
the sketch below will create a square wave with a frequency of approximately 2500 Hz . . . human
hearing ranges from about 20Hz to 20,000 Hz
digital
output
(pin 13)
𝑓=
1
1
1
=
= 2500 = 2500𝐻𝑧
𝑝𝑒𝑟𝑖𝑜𝑑 0.0002𝑠 + 0.0002𝑠
𝑠
void setup() {
pinMode(13,OUTPUT);
}
void loop() {
int half_period=200;
digitalWrite(13,HIGH);
delayMicroseconds(half_period);
digitalWrite(13,LOW);
delayMicroseconds(half_period);
}
2
living with the lab
whistling robot challenge
With the piezo speaker installed on the breadboard of
your robot, write a sketch to cause the speaker to make a
whistle (the instructor will tell you what the whistle
should sound like).
Have the instructor check your whistle when you are
satisfied with the sound.
If you don’t finish during class, then complete this outside
of class, and demonstrate your working program in class
next time to the instructor.
HINT: You may need to use several “for” loops where the
pitch varies within the loops.
3
living with the lab
example to get you going
•
•
The program shown uses a single, repeating “for” loop to create a sound
similar to an alarm.
You can use this program as a starting point, but your whistle will sound best
with several for loops.
initial frequency:
1
1
𝑓𝑖𝑛𝑖𝑡𝑖𝑎𝑙 =
=
= 333 𝐻𝑧
𝑝𝑒𝑟𝑖𝑜𝑑 0.0015𝑠 + 0.0015𝑠
final frequency:
1
1
𝑓𝑓𝑖𝑛𝑎𝑙 =
=
= 1000 𝐻𝑧
𝑝𝑒𝑟𝑖𝑜𝑑 0.0005𝑠 + 0.0005𝑠
void loop() {
for(int i=1500; i>500; i=i-8)
{
digitalWrite(13,HIGH);
delayMicroseconds(i);
digitalWrite(13,LOW);
delayMicroseconds(i);
}
delay(150);
}
150 ms
frequency (Hz)
void setup() {
pinMode(13,OUTPUT);
}
150 ms
1000
330
time (s)
4
living with the lab
your frequency vs. time plot
may look more like this
frequency (Hz)
delay
time (s)
5