1st part of the exercises

Dependencies:   MODSERIAL mbed

Committer:
bjonkheer
Date:
Thu Sep 20 12:17:36 2018 +0000
Revision:
0:704340a59525
werkend

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bjonkheer 0:704340a59525 1 #include "mbed.h"
bjonkheer 0:704340a59525 2 #include "MODSERIAL.h"
bjonkheer 0:704340a59525 3
bjonkheer 0:704340a59525 4 Ticker potmeterticker;
bjonkheer 0:704340a59525 5 DigitalIn button1(D2);
bjonkheer 0:704340a59525 6 DigitalIn button2(D3);
bjonkheer 0:704340a59525 7 AnalogIn potmeter(A0);
bjonkheer 0:704340a59525 8 DigitalOut led(D4);
bjonkheer 0:704340a59525 9 MODSERIAL pc(USBTX, USBRX);
bjonkheer 0:704340a59525 10 volatile float pwmvalue;
bjonkheer 0:704340a59525 11 volatile double pwm_pct;
bjonkheer 0:704340a59525 12 volatile int on_time_us; // The time the LED should be on, in microseconds
bjonkheer 0:704340a59525 13 volatile int off_time_us;
bjonkheer 0:704340a59525 14
bjonkheer 0:704340a59525 15
bjonkheer 0:704340a59525 16 void ReadPotmeterValues()
bjonkheer 0:704340a59525 17 {
bjonkheer 0:704340a59525 18 int frequency_Hz = 10000;
bjonkheer 0:704340a59525 19 pwm_pct = potmeter.read() * 100;
bjonkheer 0:704340a59525 20 on_time_us = (int) ((pwm_pct/100.0) * (1.0/frequency_Hz) * 1.0e7);
bjonkheer 0:704340a59525 21 off_time_us = (int) (( (100.0-pwm_pct)/100.0) * (1.0/frequency_Hz) * 1.0e7);
bjonkheer 0:704340a59525 22 }
bjonkheer 0:704340a59525 23
bjonkheer 0:704340a59525 24 int main()
bjonkheer 0:704340a59525 25 {
bjonkheer 0:704340a59525 26 pc.baud(115200);
bjonkheer 0:704340a59525 27 pc.printf("Hello World!\r\n");
bjonkheer 0:704340a59525 28 potmeterticker.attach(ReadPotmeterValues, 0.5);
bjonkheer 0:704340a59525 29
bjonkheer 0:704340a59525 30 while (true) {
bjonkheer 0:704340a59525 31 led = 1;
bjonkheer 0:704340a59525 32 pc.printf("%i\r\n",on_time_us);
bjonkheer 0:704340a59525 33 wait_us(on_time_us);
bjonkheer 0:704340a59525 34 led = 0; // Turn led off
bjonkheer 0:704340a59525 35 wait_us(off_time_us);
bjonkheer 0:704340a59525 36 //ReadPotmeterValues();
bjonkheer 0:704340a59525 37 }
bjonkheer 0:704340a59525 38 }