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