UNAL_Procesadores Actividad 2

Dependencies:   mbed TSI

Committer:
contechno
Date:
Wed Oct 09 17:20:23 2019 +0000
Revision:
0:6759fffc4e2b
UNAL_Procesadores; Actividad 2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
contechno 0:6759fffc4e2b 1 // UNAL - Procesadores
contechno 0:6759fffc4e2b 2 // Actividades Oct/2019 - JMP
contechno 0:6759fffc4e2b 3
contechno 0:6759fffc4e2b 4 #include "mbed.h"
contechno 0:6759fffc4e2b 5 #include "TSISensor.h" // Slider-Sensor
contechno 0:6759fffc4e2b 6
contechno 0:6759fffc4e2b 7 #define POLLTIME 0.1 // Time in ms, how often the Touch-Sensor is read out
contechno 0:6759fffc4e2b 8
contechno 0:6759fffc4e2b 9 void toggleWhite(); // Prototype for toggle-function
contechno 0:6759fffc4e2b 10
contechno 0:6759fffc4e2b 11 DigitalOut red(LED1); //red
contechno 0:6759fffc4e2b 12 DigitalOut green(LED2); //Green
contechno 0:6759fffc4e2b 13 DigitalOut blue(LED3); //Blue
contechno 0:6759fffc4e2b 14
contechno 0:6759fffc4e2b 15 Serial pc(USBTX, USBRX); // Init serial
contechno 0:6759fffc4e2b 16
contechno 0:6759fffc4e2b 17 int main() {
contechno 0:6759fffc4e2b 18 TSISensor touchSensor; // Init TSISensor-struct
contechno 0:6759fffc4e2b 19 int pollCtr = 0;
contechno 0:6759fffc4e2b 20 int tmpSliderPerc = 0;
contechno 0:6759fffc4e2b 21
contechno 0:6759fffc4e2b 22 // Reset all LEDs to off (LEDs are low-active)
contechno 0:6759fffc4e2b 23 red = 1;
contechno 0:6759fffc4e2b 24 green = 1;
contechno 0:6759fffc4e2b 25 blue = 1;
contechno 0:6759fffc4e2b 26
contechno 0:6759fffc4e2b 27 while(1) {
contechno 0:6759fffc4e2b 28 if(pollCtr >= (1/POLLTIME)) { // Toggle LED after 1s (every "1/POLLTIME"-loops)
contechno 0:6759fffc4e2b 29 toggleWhite(); // Call function which toggles LED
contechno 0:6759fffc4e2b 30 pollCtr = 0; // Reset counter
contechno 0:6759fffc4e2b 31 }
contechno 0:6759fffc4e2b 32
contechno 0:6759fffc4e2b 33 if((tmpSliderPerc < (int)(touchSensor.readPercentage()*100.0) - 3) || (tmpSliderPerc > (int)(touchSensor.readPercentage()*100.0) + 3)) { // If the Slider-Value has changed while "noise" (+/- 3%) is ignored, print actual value
contechno 0:6759fffc4e2b 34 tmpSliderPerc = (int)(touchSensor.readPercentage()*100.0);
contechno 0:6759fffc4e2b 35 pc.printf("Touchsensor: %d%%\r\n",tmpSliderPerc);
contechno 0:6759fffc4e2b 36 }
contechno 0:6759fffc4e2b 37
contechno 0:6759fffc4e2b 38 wait(POLLTIME);
contechno 0:6759fffc4e2b 39 pollCtr++;
contechno 0:6759fffc4e2b 40 }
contechno 0:6759fffc4e2b 41 }
contechno 0:6759fffc4e2b 42
contechno 0:6759fffc4e2b 43 void toggleWhite() { // Functions toggles all three RGB-LEDs (white light on/off)
contechno 0:6759fffc4e2b 44 red = !red;
contechno 0:6759fffc4e2b 45 blue = !blue;
contechno 0:6759fffc4e2b 46 green = !green;
contechno 0:6759fffc4e2b 47 }