PWM LED script voor RGB led
Dependencies: mbed
source.cpp@0:89bb4ba0cb68, 2018-03-11 (annotated)
- Committer:
- lennartgroen
- Date:
- Sun Mar 11 16:13:52 2018 +0000
- Revision:
- 0:89bb4ba0cb68
First release of program;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lennartgroen | 0:89bb4ba0cb68 | 1 | #include <mbed.h> |
lennartgroen | 0:89bb4ba0cb68 | 2 | |
lennartgroen | 0:89bb4ba0cb68 | 3 | AnalogIn potmeter(A0); |
lennartgroen | 0:89bb4ba0cb68 | 4 | PwmOut rgbblue(D3); |
lennartgroen | 0:89bb4ba0cb68 | 5 | PwmOut rgbgreen(D5); |
lennartgroen | 0:89bb4ba0cb68 | 6 | PwmOut rgbred(D6); |
lennartgroen | 0:89bb4ba0cb68 | 7 | |
lennartgroen | 0:89bb4ba0cb68 | 8 | float ledgreen(float analogin) //Function for converting analog signal to PWM for green led |
lennartgroen | 0:89bb4ba0cb68 | 9 | { |
lennartgroen | 0:89bb4ba0cb68 | 10 | float pwmout = 1.0; |
lennartgroen | 0:89bb4ba0cb68 | 11 | if (analogin < 0.40f){ |
lennartgroen | 0:89bb4ba0cb68 | 12 | pwmout = 100 / 33 * analogin; |
lennartgroen | 0:89bb4ba0cb68 | 13 | } |
lennartgroen | 0:89bb4ba0cb68 | 14 | else if(analogin > 0.40f){ |
lennartgroen | 0:89bb4ba0cb68 | 15 | pwmout = 3 - (100 /33 * analogin); |
lennartgroen | 0:89bb4ba0cb68 | 16 | } |
lennartgroen | 0:89bb4ba0cb68 | 17 | return pwmout; |
lennartgroen | 0:89bb4ba0cb68 | 18 | } |
lennartgroen | 0:89bb4ba0cb68 | 19 | |
lennartgroen | 0:89bb4ba0cb68 | 20 | float ledred(float analogin) //Function for converting analog signal to PWM for red led |
lennartgroen | 0:89bb4ba0cb68 | 21 | { |
lennartgroen | 0:89bb4ba0cb68 | 22 | float pwmout = 1.0; |
lennartgroen | 0:89bb4ba0cb68 | 23 | if (analogin <0.40f){ |
lennartgroen | 0:89bb4ba0cb68 | 24 | pwmout = 1 - (100/33 * analogin); |
lennartgroen | 0:89bb4ba0cb68 | 25 | } |
lennartgroen | 0:89bb4ba0cb68 | 26 | else if (analogin >0.40f){ |
lennartgroen | 0:89bb4ba0cb68 | 27 | pwmout = 100/ 33 * analogin - 1; |
lennartgroen | 0:89bb4ba0cb68 | 28 | } |
lennartgroen | 0:89bb4ba0cb68 | 29 | return pwmout; |
lennartgroen | 0:89bb4ba0cb68 | 30 | } |
lennartgroen | 0:89bb4ba0cb68 | 31 | |
lennartgroen | 0:89bb4ba0cb68 | 32 | float ledblue(float analogin) //Function for converting analog signal to PWM for blue led |
lennartgroen | 0:89bb4ba0cb68 | 33 | { |
lennartgroen | 0:89bb4ba0cb68 | 34 | float pwmout = 1.0; |
lennartgroen | 0:89bb4ba0cb68 | 35 | if (analogin >0.30f && analogin < 0.60f){ |
lennartgroen | 0:89bb4ba0cb68 | 36 | pwmout = 2 - (100/33 * analogin); |
lennartgroen | 0:89bb4ba0cb68 | 37 | } |
lennartgroen | 0:89bb4ba0cb68 | 38 | else if (analogin >0.60f){ |
lennartgroen | 0:89bb4ba0cb68 | 39 | pwmout = 100/ 33 * analogin - 2; |
lennartgroen | 0:89bb4ba0cb68 | 40 | } |
lennartgroen | 0:89bb4ba0cb68 | 41 | return pwmout; |
lennartgroen | 0:89bb4ba0cb68 | 42 | } |
lennartgroen | 0:89bb4ba0cb68 | 43 | |
lennartgroen | 0:89bb4ba0cb68 | 44 | int main() |
lennartgroen | 0:89bb4ba0cb68 | 45 | { |
lennartgroen | 0:89bb4ba0cb68 | 46 | while(1){ |
lennartgroen | 0:89bb4ba0cb68 | 47 | rgbgreen = ledgreen(potmeter); |
lennartgroen | 0:89bb4ba0cb68 | 48 | rgbred = ledred(potmeter); |
lennartgroen | 0:89bb4ba0cb68 | 49 | rgbblue = ledblue(potmeter); |
lennartgroen | 0:89bb4ba0cb68 | 50 | } |
lennartgroen | 0:89bb4ba0cb68 | 51 | } |