Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
PlayingSoundWithSpeaker.cpp@1:f2ac268b3b17, 2021-06-04 (annotated)
- Committer:
- kgabriel
- Date:
- Fri Jun 04 16:33:22 2021 +0000
- Revision:
- 1:f2ac268b3b17
Good Program
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| kgabriel | 1:f2ac268b3b17 | 1 | // Add mbed library (Mandatory) |
| kgabriel | 1:f2ac268b3b17 | 2 | #include "mbed.h" |
| kgabriel | 1:f2ac268b3b17 | 3 | |
| kgabriel | 1:f2ac268b3b17 | 4 | // Add a yellow LED on pin 5 and initialize it to 1 (ON) |
| kgabriel | 1:f2ac268b3b17 | 5 | DigitalOut yellowLed(p5, 1); |
| kgabriel | 1:f2ac268b3b17 | 6 | |
| kgabriel | 1:f2ac268b3b17 | 7 | // Add a red LED on pin 5 and initialize it to 0 (OFF) |
| kgabriel | 1:f2ac268b3b17 | 8 | DigitalOut redLed(p6, 0); |
| kgabriel | 1:f2ac268b3b17 | 9 | |
| kgabriel | 1:f2ac268b3b17 | 10 | // Add a switch BUTTON on pin 8 |
| kgabriel | 1:f2ac268b3b17 | 11 | DigitalIn switchButton(p8); |
| kgabriel | 1:f2ac268b3b17 | 12 | |
| kgabriel | 1:f2ac268b3b17 | 13 | // Add a speaker |
| kgabriel | 1:f2ac268b3b17 | 14 | PwmOut speaker(p21); |
| kgabriel | 1:f2ac268b3b17 | 15 | |
| kgabriel | 1:f2ac268b3b17 | 16 | |
| kgabriel | 1:f2ac268b3b17 | 17 | int acceptSwitchOnAction = 1; |
| kgabriel | 1:f2ac268b3b17 | 18 | |
| kgabriel | 1:f2ac268b3b17 | 19 | void play_tone(float frequency, float volume, int interval, int rest) { |
| kgabriel | 1:f2ac268b3b17 | 20 | speaker.period(1.0 / frequency); |
| kgabriel | 1:f2ac268b3b17 | 21 | speaker = volume; |
| kgabriel | 1:f2ac268b3b17 | 22 | wait_ms(interval); |
| kgabriel | 1:f2ac268b3b17 | 23 | speaker = 0.0; |
| kgabriel | 1:f2ac268b3b17 | 24 | wait(rest); |
| kgabriel | 1:f2ac268b3b17 | 25 | } |
| kgabriel | 1:f2ac268b3b17 | 26 | |
| kgabriel | 1:f2ac268b3b17 | 27 | void control_leds() { |
| kgabriel | 1:f2ac268b3b17 | 28 | if (switchButton) { // When SwitchButton ON |
| kgabriel | 1:f2ac268b3b17 | 29 | if (acceptSwitchOnAction) { |
| kgabriel | 1:f2ac268b3b17 | 30 | int yellowBlinkingTimeCounter = 0; |
| kgabriel | 1:f2ac268b3b17 | 31 | while (yellowBlinkingTimeCounter < 5) { |
| kgabriel | 1:f2ac268b3b17 | 32 | yellowLed = !yellowLed; // inverse the state of the LED |
| kgabriel | 1:f2ac268b3b17 | 33 | yellowBlinkingTimeCounter++; // Increment counter |
| kgabriel | 1:f2ac268b3b17 | 34 | play_tone(250, 0.2, 500, 0); ; // subprogram play sound |
| kgabriel | 1:f2ac268b3b17 | 35 | wait_ms(500); |
| kgabriel | 1:f2ac268b3b17 | 36 | } |
| kgabriel | 1:f2ac268b3b17 | 37 | yellowLed = 0; // Yellow LED is OFF |
| kgabriel | 1:f2ac268b3b17 | 38 | acceptSwitchOnAction = 0; |
| kgabriel | 1:f2ac268b3b17 | 39 | } |
| kgabriel | 1:f2ac268b3b17 | 40 | } else { // When SwitchButton OFF |
| kgabriel | 1:f2ac268b3b17 | 41 | yellowLed = 1; // Yellow LED is ON |
| kgabriel | 1:f2ac268b3b17 | 42 | acceptSwitchOnAction = 1; |
| kgabriel | 1:f2ac268b3b17 | 43 | } |
| kgabriel | 1:f2ac268b3b17 | 44 | redLed = !yellowLed; // Inverse the state of yellow LED |
| kgabriel | 1:f2ac268b3b17 | 45 | } |
| kgabriel | 1:f2ac268b3b17 | 46 | |
| kgabriel | 1:f2ac268b3b17 | 47 | int main() { |
| kgabriel | 1:f2ac268b3b17 | 48 | while (1) { // Do forever |
| kgabriel | 1:f2ac268b3b17 | 49 | control_leds(); // subprogram control leds |
| kgabriel | 1:f2ac268b3b17 | 50 | if (redLed) { |
| kgabriel | 1:f2ac268b3b17 | 51 | play_tone(250, 0.2, 500, 0); // subprogram play sound |
| kgabriel | 1:f2ac268b3b17 | 52 | } else { |
| kgabriel | 1:f2ac268b3b17 | 53 | wait_ms(500); // Sleep (duty-cycling) |
| kgabriel | 1:f2ac268b3b17 | 54 | } |
| kgabriel | 1:f2ac268b3b17 | 55 | } |
| kgabriel | 1:f2ac268b3b17 | 56 | } |