First version. Demo of PWM setup and speaker operation. Fine grain control of the PWM parameters is an advantage Mbed has over the Arduino environment.
main.cpp@107:61b9c99a4e27, 2021-09-20 (annotated)
- Committer:
- CSTritt
- Date:
- Mon Sep 20 03:01:02 2021 +0000
- Revision:
- 107:61b9c99a4e27
- Parent:
- 105:ed03c03b353e
- Child:
- 108:eee3167b25b4
First 2021 v5 version.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
CSTritt | 107:61b9c99a4e27 | 1 | /* |
CSTritt | 107:61b9c99a4e27 | 2 | 21_myBlink_v5 - Turns on an LED on and off for, repeatedly. |
CSTritt | 107:61b9c99a4e27 | 3 | Last Revised: 9/19/21 (v. 1.0) |
CSTritt | 107:61b9c99a4e27 | 4 | Based on mbed example. Modified by Dr. C. S. Tritt. This example code is in |
CSTritt | 107:61b9c99a4e27 | 5 | the public domain. |
CSTritt | 107:61b9c99a4e27 | 6 | */ |
CSTritt | 107:61b9c99a4e27 | 7 | // #include is a directive that "pastes" a file into your code. |
CSTritt | 107:61b9c99a4e27 | 8 | // Use this specific #include at the beginning of each mbed program. |
CSTritt | 107:61b9c99a4e27 | 9 | // mbed.h contains/points to the full definitions of our simple statements. |
Jonathan Austin |
0:2757d7abb7d9 | 10 | #include "mbed.h" |
CSTritt | 107:61b9c99a4e27 | 11 | // Define the object board_LED to be a digital output connected to LED1, |
CSTritt | 107:61b9c99a4e27 | 12 | // which is the little green LED built into the Nucleo board. |
CSTritt | 107:61b9c99a4e27 | 13 | DigitalOut board_LED(LED1); |
CSTritt | 107:61b9c99a4e27 | 14 | const int WAIT_ON = 1400; // On time in seconds. |
CSTritt | 107:61b9c99a4e27 | 15 | const int WAIT_OFF = 600; // Off time in seconds. |
CSTritt | 107:61b9c99a4e27 | 16 | /* |
CSTritt | 107:61b9c99a4e27 | 17 | The "main" function defines your main program -- it executes as soon as |
CSTritt | 107:61b9c99a4e27 | 18 | you program the board. Functions can return (compute and give back) a |
CSTritt | 107:61b9c99a4e27 | 19 | value. The main function could return an integer error code, so it begins |
CSTritt | 107:61b9c99a4e27 | 20 | with int. Functions can also accept inputs. The main function cannot |
CSTritt | 107:61b9c99a4e27 | 21 | however, so its round parentheses are empty. |
CSTritt | 107:61b9c99a4e27 | 22 | */ |
CSTritt | 107:61b9c99a4e27 | 23 | int main() { // This curly brace marks the beginning of the main function. |
CSTritt | 107:61b9c99a4e27 | 24 | // while() will repeat a set of actions as long as the statement inside |
CSTritt | 107:61b9c99a4e27 | 25 | // its round parentheses is true. 1 is the definition of true, so |
CSTritt | 107:61b9c99a4e27 | 26 | // while(1) and while(true) repeat forever. |
CSTritt | 107:61b9c99a4e27 | 27 | while(true) { // This curly brace marks the start of the repeated actions. |
CSTritt | 107:61b9c99a4e27 | 28 | board_LED = 1; // Turn on LED by storing a 1 in board_LED. |
CSTritt | 107:61b9c99a4e27 | 29 | ThisThread::sleep_for(WAIT_ON); // Int value in mS. |
CSTritt | 107:61b9c99a4e27 | 30 | board_LED = 0; // Turn off LED by storing a 0 in board_LED. |
CSTritt | 107:61b9c99a4e27 | 31 | ThisThread::sleep_for(WAIT_OFF); // Int value in mS. |
CSTritt | 107:61b9c99a4e27 | 32 | } // end of repeated actions |
CSTritt | 107:61b9c99a4e27 | 33 | } // end of main function |