Adjust the brightness of a LED using potmeter input and FastPWM output

Dependencies:   FastPWM mbed

Committer:
sjoerdbarts
Date:
Fri Oct 07 09:43:52 2016 +0000
Revision:
1:db6539dc8ea9
Parent:
0:fe2a62ab1017
Reduce duty cycle to 50 Hz

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sjoerdbarts 0:fe2a62ab1017 1 #include "mbed.h"
sjoerdbarts 0:fe2a62ab1017 2 #define SERIAL_BAUD 115200 // baud rate for serial communication
sjoerdbarts 0:fe2a62ab1017 3 #include "FastPWM.h"
sjoerdbarts 0:fe2a62ab1017 4
sjoerdbarts 0:fe2a62ab1017 5 // This program controlls the brightness of a LED using PWM with the potmeter as input
sjoerdbarts 0:fe2a62ab1017 6
sjoerdbarts 0:fe2a62ab1017 7 // Serial connection with PC
sjoerdbarts 0:fe2a62ab1017 8 Serial pc(USBTX,USBRX);
sjoerdbarts 0:fe2a62ab1017 9
sjoerdbarts 0:fe2a62ab1017 10 // Led
sjoerdbarts 0:fe2a62ab1017 11 FastPWM led(D7);
sjoerdbarts 0:fe2a62ab1017 12
sjoerdbarts 0:fe2a62ab1017 13 // PotmeterIn
sjoerdbarts 0:fe2a62ab1017 14 AnalogIn pot(A0);
sjoerdbarts 0:fe2a62ab1017 15
sjoerdbarts 0:fe2a62ab1017 16 // Initial duty value and period
sjoerdbarts 0:fe2a62ab1017 17 volatile float duty = 0.0;
sjoerdbarts 1:db6539dc8ea9 18 volatile float frequency_pwm = 100.0;
sjoerdbarts 1:db6539dc8ea9 19 const int Timeset_duty_cycle = 50;
sjoerdbarts 0:fe2a62ab1017 20
sjoerdbarts 0:fe2a62ab1017 21 void set_dutycycle() {
sjoerdbarts 0:fe2a62ab1017 22 duty=pot.read();
sjoerdbarts 0:fe2a62ab1017 23 pc.printf("\r\n Potvalue = %f \r\n",duty);
sjoerdbarts 0:fe2a62ab1017 24 led.write(duty/3);
sjoerdbarts 0:fe2a62ab1017 25 }
sjoerdbarts 0:fe2a62ab1017 26
sjoerdbarts 0:fe2a62ab1017 27 int main()
sjoerdbarts 0:fe2a62ab1017 28 {
sjoerdbarts 0:fe2a62ab1017 29 // Set baud connection with PC
sjoerdbarts 0:fe2a62ab1017 30 pc.baud(SERIAL_BAUD);
sjoerdbarts 0:fe2a62ab1017 31 pc.printf("\r\n ***THERMONUCLEAR WARFARE COMMENCES*** \r\n");
sjoerdbarts 0:fe2a62ab1017 32
sjoerdbarts 0:fe2a62ab1017 33 // Make tickers and attach
sjoerdbarts 0:fe2a62ab1017 34 Ticker tick_set_brightness;
sjoerdbarts 0:fe2a62ab1017 35 tick_set_brightness.attach(set_dutycycle,1/Timeset_duty_cycle);
sjoerdbarts 0:fe2a62ab1017 36
sjoerdbarts 0:fe2a62ab1017 37 // Set period of LED
sjoerdbarts 1:db6539dc8ea9 38 led.period(1.0/frequency_pwm);
sjoerdbarts 0:fe2a62ab1017 39
sjoerdbarts 0:fe2a62ab1017 40 while(true){
sjoerdbarts 0:fe2a62ab1017 41
sjoerdbarts 0:fe2a62ab1017 42 };
sjoerdbarts 0:fe2a62ab1017 43 }