Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Assignment1bioboard by
Diff: main.cpp
- Revision:
- 2:a3a8e6d31108
- Parent:
- 1:92b779fed823
--- a/main.cpp Thu Sep 20 12:35:41 2018 +0000
+++ b/main.cpp Thu Sep 20 13:03:27 2018 +0000
@@ -2,37 +2,57 @@
#include "MODSERIAL.h"
Ticker potmeterticker;
-DigitalIn button1(D2);
-DigitalIn button2(D3);
+InterruptIn button1(D2);
+InterruptIn button2(D3);
AnalogIn potmeter(A0);
DigitalOut led(D4);
MODSERIAL pc(USBTX, USBRX);
volatile float pwmvalue;
-volatile double pwm_pct;
-volatile int on_time_us; // The time the LED should be on, in microseconds
-volatile int off_time_us;
+volatile int pwm_pct = 50;
+volatile int on_time_us=5000; //The time the LED should be on, in microseconds
+volatile int off_time_us=5000;
-void ReadPotmeterValues()
+void Increasebright()
{
int frequency_Hz = 10000;
- pwm_pct = potmeter.read() * 100;
- on_time_us = (int) ((pwm_pct/100.0) * (1.0/frequency_Hz) * 1.0e7);
- off_time_us = (int) (( (100.0-pwm_pct)/100.0) * (1.0/frequency_Hz) * 1.0e7);
+ if (pwm_pct <= 95){
+ pwm_pct = pwm_pct + 5;
+ }
+ on_time_us = (int) ((pwm_pct/100.0) * (1.0/frequency_Hz) * 1.0e8);
+ off_time_us = (int) (( (100.0-pwm_pct)/100.0) * (1.0/frequency_Hz) * 1.0e8);
+}
+
+
+void Decreasebright()
+{
+ int frequency_Hz = 10000;
+ if (pwm_pct >= 5){
+ pwm_pct = pwm_pct - 5;
+ }
+ on_time_us = (int) ((pwm_pct/100.0) * (1.0/frequency_Hz) * 1.0e8);
+ off_time_us = (int) (( (100.0-pwm_pct)/100.0) * (1.0/frequency_Hz) * 1.0e8);
}
int main()
{
pc.baud(115200);
pc.printf("Hello World!\r\n");
- potmeterticker.attach(ReadPotmeterValues, 0.5);
+
+ button1.fall(&Increasebright);
+ button2.fall(&Decreasebright);
+
+ int n = 0;
while (true) {
led = 1;
- pc.printf("%i\r\n",pwm_pct);
wait_us(on_time_us);
led = 0; // Turn led off
wait_us(off_time_us);
//ReadPotmeterValues();
+ if (n % 100 == 0){
+ pc.printf("%i\r\n",pwm_pct);
+ }
+ n++;
}
}
\ No newline at end of file
