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.
main.cpp@0:8c4301046871, 2019-09-20 (annotated)
- Committer:
- BRD
- Date:
- Fri Sep 20 07:46:16 2019 +0000
- Revision:
- 0:8c4301046871
First;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| BRD | 0:8c4301046871 | 1 | // For this example, you need the BioRobotics shield. |
| BRD | 0:8c4301046871 | 2 | // Connect POT2 (frequency)to A0 |
| BRD | 0:8c4301046871 | 3 | // Connect POT1 (pwm) to A1 |
| BRD | 0:8c4301046871 | 4 | #include "mbed.h" |
| BRD | 0:8c4301046871 | 5 | #include <math.h> |
| BRD | 0:8c4301046871 | 6 | |
| BRD | 0:8c4301046871 | 7 | DigitalOut gpo(D0); |
| BRD | 0:8c4301046871 | 8 | DigitalOut led(LED_RED); |
| BRD | 0:8c4301046871 | 9 | AnalogIn freq(A0); |
| BRD | 0:8c4301046871 | 10 | AnalogIn pwm(A1); |
| BRD | 0:8c4301046871 | 11 | Ticker ReadPotmetersTicker; |
| BRD | 0:8c4301046871 | 12 | volatile double frequency_Hz=1; |
| BRD | 0:8c4301046871 | 13 | volatile double pwm_pct = 50; |
| BRD | 0:8c4301046871 | 14 | volatile int on_time_us; // The time the LED should be on, in microseconds |
| BRD | 0:8c4301046871 | 15 | volatile int off_time_us; |
| BRD | 0:8c4301046871 | 16 | |
| BRD | 0:8c4301046871 | 17 | void ReadPotmeterValues(void) |
| BRD | 0:8c4301046871 | 18 | { |
| BRD | 0:8c4301046871 | 19 | frequency_Hz = 1 + pow((double)freq.read(),4) * 50; // Frequency ranges from 1 to 50 Hz |
| BRD | 0:8c4301046871 | 20 | pwm_pct = pwm.read() * 100; |
| BRD | 0:8c4301046871 | 21 | on_time_us = (int) ((pwm_pct/100.0) * (1.0/frequency_Hz) * 1.0e6); |
| BRD | 0:8c4301046871 | 22 | off_time_us = (int) (( (100.0-pwm_pct)/100.0) * (1.0/frequency_Hz) * 1.0e6); |
| BRD | 0:8c4301046871 | 23 | } |
| BRD | 0:8c4301046871 | 24 | |
| BRD | 0:8c4301046871 | 25 | int main() |
| BRD | 0:8c4301046871 | 26 | { |
| BRD | 0:8c4301046871 | 27 | ReadPotmetersTicker.attach(ReadPotmeterValues, 0.1); |
| BRD | 0:8c4301046871 | 28 | |
| BRD | 0:8c4301046871 | 29 | while (true) |
| BRD | 0:8c4301046871 | 30 | { |
| BRD | 0:8c4301046871 | 31 | led = 0; // Turn led on |
| BRD | 0:8c4301046871 | 32 | wait_us(on_time_us); |
| BRD | 0:8c4301046871 | 33 | led = 1; // Turn led on |
| BRD | 0:8c4301046871 | 34 | wait_us(off_time_us); |
| BRD | 0:8c4301046871 | 35 | } |
| BRD | 0:8c4301046871 | 36 | } |