1st part of the exercises

Dependencies:   MODSERIAL mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "MODSERIAL.h"
00003 
00004 Ticker potmeterticker;
00005 DigitalIn button1(D2);
00006 DigitalIn button2(D3);
00007 AnalogIn potmeter(A0);
00008 DigitalOut led(D4);
00009 MODSERIAL pc(USBTX, USBRX);
00010 volatile float pwmvalue;
00011 volatile double pwm_pct;
00012 volatile int on_time_us; // The time the LED should be on, in microseconds
00013 volatile int off_time_us;
00014 
00015 
00016 void ReadPotmeterValues()
00017 {
00018     int frequency_Hz = 10000; 
00019     pwm_pct = potmeter.read() * 100;
00020     on_time_us = (int) ((pwm_pct/100.0) * (1.0/frequency_Hz) * 1.0e7);
00021     off_time_us = (int) (( (100.0-pwm_pct)/100.0) * (1.0/frequency_Hz) * 1.0e7);
00022 }
00023 
00024 int main()
00025 {
00026     pc.baud(115200);
00027     pc.printf("Hello World!\r\n");
00028     potmeterticker.attach(ReadPotmeterValues, 0.5);
00029     
00030     while (true) {
00031         led = 1;
00032         pc.printf("%i\r\n",on_time_us);
00033         wait_us(on_time_us);
00034         led = 0; // Turn led off
00035         wait_us(off_time_us);
00036         //ReadPotmeterValues();
00037     }
00038 }