Ruben Lucas
/
BrightButton
Test script for PwmOut control LED with buttons
main.cpp@0:2ba57c4723ea, 2018-09-26 (annotated)
- Committer:
- rubenlucas
- Date:
- Wed Sep 26 12:35:57 2018 +0000
- Revision:
- 0:2ba57c4723ea
- Child:
- 1:9654405c52f4
lowered the increase/decrease of percentage and added a ticker to smoothen the working
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rubenlucas | 0:2ba57c4723ea | 1 | #include "mbed.h" |
rubenlucas | 0:2ba57c4723ea | 2 | #include "math.h" |
rubenlucas | 0:2ba57c4723ea | 3 | |
rubenlucas | 0:2ba57c4723ea | 4 | PwmOut Led(D3); |
rubenlucas | 0:2ba57c4723ea | 5 | InterruptIn Brighter(D1); |
rubenlucas | 0:2ba57c4723ea | 6 | InterruptIn Dimmer(D0); |
rubenlucas | 0:2ba57c4723ea | 7 | Ticker checkTicker; |
rubenlucas | 0:2ba57c4723ea | 8 | |
rubenlucas | 0:2ba57c4723ea | 9 | volatile float DutyCycle = 0.2; // set start cycle percentage of 20% (this is free of choice) |
rubenlucas | 0:2ba57c4723ea | 10 | volatile float Percentage; |
rubenlucas | 0:2ba57c4723ea | 11 | |
rubenlucas | 0:2ba57c4723ea | 12 | void BrighterFcn() |
rubenlucas | 0:2ba57c4723ea | 13 | { |
rubenlucas | 0:2ba57c4723ea | 14 | if (DutyCycle == 1.0) // check if duty cycle is already at max |
rubenlucas | 0:2ba57c4723ea | 15 | {return;} |
rubenlucas | 0:2ba57c4723ea | 16 | else |
rubenlucas | 0:2ba57c4723ea | 17 | {DutyCycle = DutyCycle + 0.025;} // raise duty cycle with 2.5% |
rubenlucas | 0:2ba57c4723ea | 18 | } |
rubenlucas | 0:2ba57c4723ea | 19 | |
rubenlucas | 0:2ba57c4723ea | 20 | void DimmerFcn() |
rubenlucas | 0:2ba57c4723ea | 21 | { |
rubenlucas | 0:2ba57c4723ea | 22 | if (DutyCycle == 0.0) // check if duty cycle is already at minimum |
rubenlucas | 0:2ba57c4723ea | 23 | {return;} |
rubenlucas | 0:2ba57c4723ea | 24 | else |
rubenlucas | 0:2ba57c4723ea | 25 | {DutyCycle = DutyCycle - 0.025;} // lower duty cycle with 2.5% |
rubenlucas | 0:2ba57c4723ea | 26 | } |
rubenlucas | 0:2ba57c4723ea | 27 | |
rubenlucas | 0:2ba57c4723ea | 28 | void CheckDutyCycle() |
rubenlucas | 0:2ba57c4723ea | 29 | { |
rubenlucas | 0:2ba57c4723ea | 30 | Percentage = DutyCycle; |
rubenlucas | 0:2ba57c4723ea | 31 | } |
rubenlucas | 0:2ba57c4723ea | 32 | |
rubenlucas | 0:2ba57c4723ea | 33 | int main() |
rubenlucas | 0:2ba57c4723ea | 34 | { |
rubenlucas | 0:2ba57c4723ea | 35 | float frequency = 10000; // 10 kHz |
rubenlucas | 0:2ba57c4723ea | 36 | Led.period(1/frequency); // set fixed period |
rubenlucas | 0:2ba57c4723ea | 37 | Brighter.rise(&BrighterFcn); //call function to raise dutycycle percentage if button is pressed and released |
rubenlucas | 0:2ba57c4723ea | 38 | Dimmer.rise(&DimmerFcn); //call function to lower dutycycle percentage if button is pressed and released |
rubenlucas | 0:2ba57c4723ea | 39 | checkTicker.attach(&CheckDutyCycle, 0.05); // call for percentage with 20 Hz to make the working smoother |
rubenlucas | 0:2ba57c4723ea | 40 | |
rubenlucas | 0:2ba57c4723ea | 41 | while (true) |
rubenlucas | 0:2ba57c4723ea | 42 | { |
rubenlucas | 0:2ba57c4723ea | 43 | Led.write(Percentage); |
rubenlucas | 0:2ba57c4723ea | 44 | wait(0.1f); |
rubenlucas | 0:2ba57c4723ea | 45 | } |
rubenlucas | 0:2ba57c4723ea | 46 | } |