NUCLEO-F042K6 Simple demo with buttons intterupt changing PWM DC - with autoincrement when button pressed longer than 1s
Dependencies: mbed
Diff: main.cpp
- Revision:
- 3:5edb1ea41029
- Parent:
- 2:b5b8bda0bcda
--- a/main.cpp Sat Feb 17 15:40:57 2018 +0000 +++ b/main.cpp Sat Feb 17 17:26:47 2018 +0000 @@ -3,9 +3,9 @@ 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 + Uses two buttons connected to PA_0 - plus and PA_1 - minus with internal 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 @@ -20,7 +20,7 @@ *******************************************************************************/ -InterruptIn buttonPlus(PA_0); // deffition of sinterrupt +InterruptIn buttonPlus(PA_0); // deffition of interrupt InterruptIn buttonMinus(PA_1); // deffition of interrupt PwmOut PWM(PA_8); // definition of PWM pin @@ -29,8 +29,6 @@ Ticker tick; // Ticker definition -Serial pc(PB_6, PB_7, 9600); // Serial port definition - bool autoIncrement=false; // true - do autoincrement // false - do not autoincrement @@ -45,10 +43,10 @@ plus=true; // incrementation if (PWM.read()+0.01 <= 1) { PWM.write(PWM.read()+0.01); - pc.printf("Presed plus button. DC: %f.\n",PWM.read()); + printf("Presed plus button. DC: %f.\n",PWM.read()); }else{ PWM.write(1); - pc.printf("Presed plus button. Already maximum DC.\n"); + printf("Presed plus button. Already maximum DC.\n"); } } @@ -59,7 +57,7 @@ tim.stop(); tim.reset(); autoIncrement=false; - pc.printf("Plus button released.\n"); + printf("Plus button released.\n"); } // called after falling edge on PA_1 @@ -70,10 +68,10 @@ plus=false; // decrementation if (PWM.read()-0.01 >= 0) { PWM.write(PWM.read()-0.01); - pc.printf("Presed minus button. DC: %f\n",PWM.read()); + printf("Presed minus button. DC: %f\n",PWM.read()); }else{ PWM.write(0); - pc.printf("Presed minus button. Already minimum DC.\n"); + printf("Presed minus button. Already minimum DC.\n"); } } @@ -84,7 +82,7 @@ tim.stop(); tim.reset(); autoIncrement=false; - pc.printf("Minus button released.\n"); + printf("Minus button released.\n"); } @@ -110,6 +108,10 @@ buttonPlus.rise(&releasedPlus); buttonMinus.rise(&releasedMinus); + // internal pull ups + buttonPlus.mode(PullUp); + buttonMinus.mode(PullUp); + // set ticker interrupt tick.attach(&checkTimer,0.01); @@ -117,11 +119,11 @@ // check if auto increment/decrement if(autoIncrement && plus && PWM.read()+0.01 <= 1){ PWM.write(PWM.read()+0.01); - pc.printf("Autoincrement. DC: %f.\n",PWM.read()); + printf("Autoincrement. DC: %f.\n",PWM.read()); } else if(autoIncrement && !plus && PWM.read()-0.01 >= 0){ PWM.write(PWM.read()-0.01); - pc.printf("Autodecrement. DC: %f.\n",PWM.read()); + printf("Autodecrement. DC: %f.\n",PWM.read()); } wait_ms(500); }