NUCLEO-F042K6 Simple demo with buttons intterupt changing PWM DC - with autoincrement when button pressed longer than 1s

Dependencies:   mbed

Revision:
1:127786980842
Parent:
0:602adf33625c
Child:
2:b5b8bda0bcda
--- a/main.cpp	Sun Feb 11 23:55:14 2018 +0000
+++ b/main.cpp	Mon Feb 12 22:19:42 2018 +0000
@@ -1,5 +1,24 @@
-#include "mbed.h"
-// pull up
+#include "mbed.h" // import of mbed library (required)
+/*******************************************************************************
+
+  EXAMPLE DESCRIPTION
+
+  Uses two buttons connected to PA_0 - plus and PA_1 - minus with pull up 
+  resistors. On PA_8 is PWM output with variable duty cycle (DC) and frequncy of
+  500 Hz. DC can be changed true buttons. On press of button DC is incremented 
+  or decremented by 0.01. Interrupt is attached to falling edge on appropriate
+  pin and process of incrementation/decrementation is driven throu this
+  interrupt. Programm also demonstrates auto incrementation/decrementation while
+  button is pressed for longer than 1 s. After button is pressed timer is 
+  started. Ticker periodically  (every 10 ms) calls function that checks timer
+  value. If the timer had counted more than 1 s, It is stopped and auto 
+  incementation/decrementation is stared (+-0.01 every 500 ms). After button
+  release auto incementation/decrementation is stopped and timer reset. 
+  Programm is written in way, that there can NOT be simultaneously active both
+  auto incrementation and decrementation. Note that there are also prints to 
+  UART to check states of buttons and change of DC.
+  
+*******************************************************************************/
 
 InterruptIn buttonPlus(PA_0); // deffition of interrupt
 InterruptIn buttonMinus(PA_1); // deffition of interrupt
@@ -8,16 +27,22 @@
 
 Timer tim; // Timer definition
 
-Ticker tick;
+Ticker tick; // Ticker definition
+
+bool autoIncrement=false; // true - do autoincrement
+                          // false - do not autoincrement
 
-bool autoIncrement=false;
-bool plus=true;
+bool plus=true; // true -  auto increment
+                // false - auto decrement
 
+// called after falling edge on PA_0
+// starts timer for auto increment check
+// increments DC
 void pressedPlus() {
     tim.start(); // start Timer
-    plus=true;
+    plus=true; // incrementation
     if (PWM.read()+0.01 <= 1) {
-        PWM.write(PWM.read()+0.01);
+        PWM.write(PWM.read()+0.01); 
         printf("Presed plus button. DC: %f.\n",PWM.read());
     }else{
         PWM.write(1);
@@ -25,6 +50,9 @@
     }
 }
 
+// called after rising edge on PA_0
+// stops and resets auto increment timer
+// stops auto increment
 void releasedPlus(){
     tim.stop();
     tim.reset();
@@ -32,9 +60,12 @@
     printf("Plus button released.\n");
 }
 
+// called after falling edge on PA_1
+// starts timer for auto decrement check
+// decrements DC
 void pressedMinus() {
     tim.start(); // start Timer
-    plus=false;
+    plus=false; // decrementation
     if (PWM.read()-0.01 >= 0) {
         PWM.write(PWM.read()-0.01);
         printf("Presed minus button. DC: %f\n",PWM.read());
@@ -44,6 +75,9 @@
     }
 }
 
+// called after rising edge on PA_1
+// stops and resets auto decrement timer
+// stops auto decrement
 void releasedMinus(){
     tim.stop();
     tim.reset();
@@ -51,6 +85,9 @@
     printf("Minus button released.\n");
 }
 
+
+// checks timer value
+// if > 1 s starts auto incerement/decrement
 void checkTimer(){
     if(tim.read_ms()>1000){
         tim.stop();
@@ -65,15 +102,17 @@
     PWM.period_ms(2); // 500 Hz
     PWM.write(0); // duration of active pulse
     
-    // Set button interrupt
+    // Set buttons interrupts
     buttonPlus.fall(&pressedPlus);
     buttonMinus.fall(&pressedMinus);
     buttonPlus.rise(&releasedPlus);
     buttonMinus.rise(&releasedMinus);
     
+    // set ticker interrupt
     tick.attach(&checkTimer,0.01);
     
     while (1) {
+        // check if auto increment/decrement
         if(autoIncrement && plus && PWM.read()+0.01 <= 1){
             PWM.write(PWM.read()+0.01);
             printf("Autoincrement. DC: %f.\n",PWM.read());