Sebastian Barrera
/
Lab5_part2
lab 5 part 2
main.cpp
- Committer:
- sebbarpar
- Date:
- 2020-03-12
- Revision:
- 7:a910936bf4f8
- Parent:
- 6:71ef35e456ab
- Child:
- 8:ca714e821245
File content as of revision 7:a910936bf4f8:
#include "mbed.h" #include "TSISensor.h" // Example program for lab 5 // ------------------------- // A value is read from the touch sensor and use // to control two LEDs // The value is also output to the serial interface Serial pc(USBTX, USBRX); // tx, rx PwmOut redLED(LED_RED); PwmOut greenLED(LED_GREEN); TSISensor tsi; Timer t; enum pos{neutral, lo, li, ri, ro}; Thread green ; // thread for red LED Thread red ; // thread for green LED # define lout 0x01 # define lin 0x02 # define rin 0x04 # define rout 0x08 EventFlags signals; void changebrightnessred() { // method to run in thread int flags; float i=0; while (true) { signals.wait_any(lin,false); flags=signals.wait_any(lout|rout,true); if (flags==lout){ if (i!=0) i--; } if (flags==rout){ if (i!=9) i++; } pc.printf("flag: %d, red: %d",flags, i); redLED= i/10; } } void changebrightnessgreen() { // method to run in thread int flags; float i=0; while (true) { signals.wait_any(rin,false); flags=signals.wait_any(lout|rout,true); if (flags==lout){ if (i!=0) i--; } if (flags==rout){ if (i!=9) i++; } pc.printf("green: %d", i); greenLED= i/10; } } int main(void) { redLED = true ; // turn off greenLED = true ; // turn off red.start(changebrightnessgreen) ; // start the red thread green.start(changebrightnessred) ; // start the green thread int pos=neutral; while (true) { uint8_t d = tsi.readDistance() ; // Distance is between 0 and 39 // When no touch --> 0 // Left --> low value Right --> high value switch (pos){ case neutral: if (d>3 && d<9){pos=lo; signals.set(lout); t.start();} if (d>13 && d<19){pos=li; signals.set(lin);} if (d>23 && d<29){pos=ri; signals.set(rin);} if (d>33){pos=ro; signals.set(rout); t.start();} break; case lo: if (t.read()>1) signals.set(lout); if (d<3 || d>9) {pos=neutral; t.reset(); t.stop();} break; case li: if (d<13 || d>19) {pos=neutral; signals.clear(lin);} break; case ri: if (d<23 || d>29) {pos=neutral; signals.clear(rin);} break; case ro: if (t.read()>1) signals.set(rout); if (d<33) {pos=neutral; t.reset(); t.stop();} break; } pc.printf("%d", d) ; pc.putc(' ') ; ThisThread::sleep_for(100) ; // This polling rate is too slow - increase it // The slower rate maks it easier to output on the terminal } }