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.
Fork of TSI_sample by
main.cpp
00001 #include "mbed.h" 00002 #include "TSISensor.h" 00003 00004 // Example program for lab 5 00005 // ------------------------- 00006 // A value is read from the touch sensor and use 00007 // to control two LEDs 00008 // The value is also output to the serial interface 00009 00010 Serial pc(USBTX, USBRX); // tx, rx 00011 TSISensor tsi; 00012 DigitalOut leds[4] = { PTA13, PTD5, PTD0, PTD2 }; 00013 00014 Thread thread1(osPriorityNormal, 1000); // thread for LED1 00015 Thread thread2(osPriorityNormal, 1000); // thread for LED2 00016 Thread thread3(osPriorityNormal, 1000); // thread for LED3 00017 Thread thread4(osPriorityNormal, 1000); // thread for LED4 00018 00019 void led1_thread() { 00020 while (true) { 00021 Thread::signal_wait(0x1); 00022 leds[0] = !leds[0]; 00023 } 00024 } 00025 void led2_thread() { 00026 while (true) { 00027 Thread::signal_wait(0x1); 00028 leds[1] = !leds[1]; 00029 } 00030 } 00031 void led3_thread() { 00032 while (true) { 00033 Thread::signal_wait(0x1); 00034 leds[2] = !leds[2]; 00035 } 00036 } 00037 void led4_thread() { 00038 while (true) { 00039 Thread::signal_wait(0x1); 00040 leds[3] = !leds[3]; 00041 } 00042 } 00043 00044 int main(void) { 00045 enum states { none, leftOut, leftIn, rightIn, rightOut }; 00046 states state = none; 00047 00048 for (int i=0; i<4; i++) { // turn off all leds 00049 leds[i] = 0; 00050 } 00051 00052 thread1.start(&led1_thread); 00053 thread2.start(&led2_thread); 00054 thread3.start(&led3_thread); 00055 thread4.start(&led4_thread); 00056 00057 while (true) { 00058 uint8_t pos = tsi.readDistance() ; // Distance is between 0 and 39 00059 // When no touch --> 0 00060 // Left --> low value Right --> high value 00061 pc.printf("%d", pos) ; 00062 pc.putc(' ') ; 00063 00064 switch (state) { 00065 case none: 00066 pc.printf("none\n\r"); 00067 if (pos > 3 && pos < 9) { 00068 state = leftOut; 00069 thread1.signal_set(0x1); 00070 } else if (pos > 13 && pos < 19) { 00071 state = leftIn; 00072 thread2.signal_set(0x1); 00073 } else if (pos > 23 && pos < 29) { 00074 state = rightIn; 00075 thread3.signal_set(0x1); 00076 } else if (pos > 33) { 00077 state = rightOut; 00078 thread4.signal_set(0x1); 00079 } 00080 break; 00081 case leftOut: 00082 pc.printf("leftOut\n\r"); 00083 if (pos < 3 || pos > 9) { 00084 state = none; 00085 } 00086 break; 00087 case leftIn: 00088 pc.printf("leftIn\n\r"); 00089 if (pos < 13 || pos > 19) { 00090 state = none; 00091 } 00092 break; 00093 case rightIn: 00094 pc.printf("rightIn\n\r"); 00095 if (pos < 23 || pos > 29) { 00096 state = none; 00097 } 00098 break; 00099 case rightOut: 00100 pc.printf("rightOut\n\r"); 00101 if (pos < 33) { 00102 state = none; 00103 } 00104 break; 00105 } 00106 00107 Thread::wait(200); // This polling rate is too slow - increase it 00108 // The slower rate maks it easier to output on the terminal 00109 } 00110 }
Generated on Fri Jul 15 2022 11:30:59 by
1.7.2
