Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
8 years ago.
Please help code with PWN generation with LEDs and piezo transducer
Use the mbed with its DigitalIn and PwmOut objects to implement a simple musical instrument.
Connect 4 pushbutton switches in active-high configuration to pins p5 through p8 of the mbed. Connect the piezo transducer between pin p21 and GND.
Pressing a switch should generate a particular musical note on the piezo transducer (at maximum volume) and light up a particular LED (at full brightness). When the switch is released, the note should fade out over a 0.1 second period with both the brightness of the LED and the volume of the piezo transducer perceptibly diminishing over time rather than immediately stopping. The following table shows the frequencies for the 4 notes as well as the LED that should light up for each note:
switch pin Frequency (Hz) LED p5 1046 LED1 p6 1175 LED2 p7 1319 LED3 p8 1397 LED4
The piezo transducer will output a note at maximum value when the PWM duty cycle is set to 50% and at reduced volume as the duty cycle approaches either 0% or 100%. Note however that this extremely nonlinear, so differences in volume will only be noticeable at the extremes (approximately 0% - 3% or 97% - 100%)
Would you be able to generate a code?
This is what I have so far for my code
- include "mbed.h"
PwmOut piezo(p21);
int main () {
while(1) {
for (int i=50; i>0;i) {
piezo.period(1/1000.);
piezo=i/100.;
wait(0.1); the sequence takes (50*0.1)
}
}
}
Please help and thank you!
Unfortunately we're not here to do your homework for you. What is the exact problem that you're facing? Can you generate the tone correctly? What have you tried, and what did it not do?
posted by Jan Jongboom 03 Oct 2017