04-07 OrangesAndLemons
From Manuals
(Difference between revisions)
(Created page with 'This example uses a PWM to play the tune "Oranges and Lemons" on a piezo buzzer using a PWM. /*Program Example 4.7: Plays the tune "Oranges and Lemons" on a piezo buzzer, using…') |
|||
| (2 intermediate revisions not shown) | |||
| Line 1: | Line 1: | ||
This example uses a PWM to play the tune "Oranges and Lemons" on a piezo buzzer using a PWM. | This example uses a PWM to play the tune "Oranges and Lemons" on a piezo buzzer using a PWM. | ||
| + | The signals used for this example are listed below: | ||
| + | {| border="1" cellpadding="5" cellspacing="2" align="center" style="text-align: center;" | ||
| + | |+ align="bottom"|'''Signals Used and Connector Locations for Example 04-07''' | ||
| + | |'''Signal''' | ||
| + | |'''LPC4330 PIN NAME''' | ||
| + | |'''BAM210E''' | ||
| + | |'''BAM210''' | ||
| + | |'''BAM200E''' | ||
| + | |'''BAM200''' | ||
| + | |'''Used for''' | ||
| + | |- | ||
| + | |PWM1 | ||
| + | |P1_7 | ||
| + | |J9-3 | ||
| + | |J9-3 | ||
| + | |S2-3 | ||
| + | |S2-3 | ||
| + | |Buzzer | ||
| + | |- | ||
| + | |} | ||
| + | |||
| + | The following schematic can be used to build the circuit with a BAM210E or BAM210. | ||
| + | [[Image:BAM210_04_07_SCH.png|center|alt=BAM210|frame|<div align="center">'''Schematic for example 04-07''']] | ||
| + | |||
| + | <div style="text-align: left;"> | ||
/*Program Example 4.7: Plays the tune "Oranges and Lemons" on a piezo buzzer, using PWM | /*Program Example 4.7: Plays the tune "Oranges and Lemons" on a piezo buzzer, using PWM | ||
*/ | */ | ||
#include "mbed.h" | #include "mbed.h" | ||
PwmOut buzzer(PWM1); | PwmOut buzzer(PWM1); | ||
| - | + | ||
//frequency array | //frequency array | ||
float frequency[]= {659,554,659,554,440,494,554,587,494,659,554,440}; | float frequency[]= {659,554,659,554,440,494,554,587,494,659,554,440}; | ||
| Line 21: | Line 46: | ||
} | } | ||
| + | The following image shows the built circuit on a BAM210E. | ||
[[Image:BAM210_OangesAndLemons.jpeg|center|]] | [[Image:BAM210_OangesAndLemons.jpeg|center|]] | ||
| - | |||
| - | |||
| - | |||
| - | |||
Current revision as of 19:13, 17 September 2014
This example uses a PWM to play the tune "Oranges and Lemons" on a piezo buzzer using a PWM.
The signals used for this example are listed below:
| Signal | LPC4330 PIN NAME | BAM210E | BAM210 | BAM200E | BAM200 | Used for |
| PWM1 | P1_7 | J9-3 | J9-3 | S2-3 | S2-3 | Buzzer |
The following schematic can be used to build the circuit with a BAM210E or BAM210.
/*Program Example 4.7: Plays the tune "Oranges and Lemons" on a piezo buzzer, using PWM
*/
#include "mbed.h"
PwmOut buzzer(PWM1);
//frequency array
float frequency[]= {659,554,659,554,440,494,554,587,494,659,554,440};
float beat[]= {1,1,1,1,1,0.5,0.5,1,1,1,1,2}; //beat array
int main()
{
while (1) {
for (int i=0; i<=11; i++) {
buzzer.period(1/(2*frequency[i])); // set PWM period
buzzer=0.5; // set duty cycle
wait(0.4f*beat[i]); // hold for beat period
}
}
}
The following image shows the built circuit on a BAM210E.


