finished assignment 1

Dependencies:   MODSERIAL mbed

Fork of Assignment1bioboard by Bram Jonkheer

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 InterruptIn button1(D2);
00006 InterruptIn button2(D3);
00007 AnalogIn potmeter(A0);
00008 DigitalOut led(D4);
00009 MODSERIAL pc(USBTX, USBRX);
00010 volatile float pwmvalue;
00011 volatile int pwm_pct = 50;
00012 volatile int on_time_us=5000; //The time the LED should be on, in microseconds
00013 volatile int off_time_us=5000;
00014 
00015 
00016 void Increasebright()
00017 {
00018     int frequency_Hz = 10000; 
00019     if (pwm_pct <= 95){ 
00020         pwm_pct = pwm_pct + 5;
00021     }
00022     on_time_us = (int) ((pwm_pct/100.0) * (1.0/frequency_Hz) * 1.0e8);
00023     off_time_us = (int) (( (100.0-pwm_pct)/100.0) * (1.0/frequency_Hz) * 1.0e8);
00024 }
00025 
00026 
00027 void Decreasebright()
00028 {
00029     int frequency_Hz = 10000;
00030     if (pwm_pct >= 5){ 
00031         pwm_pct = pwm_pct - 5;
00032     }
00033     on_time_us = (int) ((pwm_pct/100.0) * (1.0/frequency_Hz) * 1.0e8);
00034     off_time_us = (int) (( (100.0-pwm_pct)/100.0) * (1.0/frequency_Hz) * 1.0e8);
00035 }
00036 
00037 int main()
00038 {
00039     pc.baud(115200);
00040     pc.printf("Hello World!\r\n");
00041     
00042     button1.fall(&Increasebright);
00043     button2.fall(&Decreasebright);
00044     
00045     int n = 0;
00046     
00047     while (true) {
00048         led = 1;
00049         wait_us(on_time_us);
00050         led = 0; // Turn led off
00051         wait_us(off_time_us);
00052         //ReadPotmeterValues();
00053         if (n % 100 == 0){
00054             pc.printf("%i\r\n",pwm_pct);       
00055         }
00056         n++;
00057     }
00058 }