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.
main.cpp
- Committer:
- sebbarpar
- Date:
- 2020-03-16
- Revision:
- 8:ca714e821245
- Parent:
- 7:a910936bf4f8
- Child:
- 9:595d1dbf0086
File content as of revision 8:ca714e821245:
#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;
volatile uint32_t flags_red;
volatile uint32_t flags_green;
void changebrightnessred() { // method to run in thread
float i=0;
while (true) {
signals.wait_any(lin,osWaitForever,false);
signals.clear(rin);
flags_red=signals.wait_any(lout|rout,osWaitForever,true);
if (flags_red==lout){
if (i!=0) i--;
}
if (flags_red==rout){
if (i!=9) i++;
}
signals.clear(lout);
signals.clear(rout);
pc.printf("red: %f \n ", i);
redLED= i/10;
}
}
void changebrightnessgreen() { // method to run in thread
float i=0;
while (true) {
signals.wait_any(rin,osWaitForever,false);
signals.clear(lin);
flags_green=signals.wait_any(lout|rout,osWaitForever,true);
if (flags_green==lout){
if (i!=0) i--;
}
if (flags_green==rout){
if (i!=9) i++;
}
signals.clear(lout);
signals.clear(rout);
pc.printf("green: %f \n", 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); t.reset();}
if (d<3 || d>9) {pos=neutral; t.reset(); t.stop();}
break;
case li:
if (d<13 || d>19) {pos=neutral; }
break;
case ri:
if (d<23 || d>29) {pos=neutral; }
break;
case ro:
if (t.read()>1) {signals.set(rout);t.reset();}
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
}
}