This piece of code sets a threshold and reads all the sensors. Then sends an output to the serial port.
Dependencies: mbed
line_sensors_to_serial.cpp@0:64a47a66c95d, 2014-10-05 (annotated)
- Committer:
- jmar11
- Date:
- Sun Oct 05 02:10:38 2014 +0000
- Revision:
- 0:64a47a66c95d
This code sets a threshold and reads all the sensors
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jmar11 | 0:64a47a66c95d | 1 | /*This piece of code sets a threshold and reads all the sensors |
jmar11 | 0:64a47a66c95d | 2 | **sample output: |
jmar11 | 0:64a47a66c95d | 3 | ** |
jmar11 | 0:64a47a66c95d | 4 | **Please put all the sensors over white and press <enter> |
jmar11 | 0:64a47a66c95d | 5 | ** |
jmar11 | 0:64a47a66c95d | 6 | **The values of the line sensors are below |
jmar11 | 0:64a47a66c95d | 7 | **0 == white 1 == black |
jmar11 | 0:64a47a66c95d | 8 | ** |
jmar11 | 0:64a47a66c95d | 9 | **1 2 3 4 5 6 7 8 |
jmar11 | 0:64a47a66c95d | 10 | **0 0 0 1 1 0 0 0 |
jmar11 | 0:64a47a66c95d | 11 | */ |
jmar11 | 0:64a47a66c95d | 12 | |
jmar11 | 0:64a47a66c95d | 13 | #include "mbed.h" |
jmar11 | 0:64a47a66c95d | 14 | |
jmar11 | 0:64a47a66c95d | 15 | Serial pc(USBTX, USBRX); |
jmar11 | 0:64a47a66c95d | 16 | Timer timer; |
jmar11 | 0:64a47a66c95d | 17 | DigitalInOut sens[8] = {PTB8, PTB9, PTB10, PTB11, PTE2, PTE3, PTE4, PTE5}; //freedom board pins |
jmar11 | 0:64a47a66c95d | 18 | int detect[8]; |
jmar11 | 0:64a47a66c95d | 19 | |
jmar11 | 0:64a47a66c95d | 20 | void readLine(int); |
jmar11 | 0:64a47a66c95d | 21 | int setThresh(int); |
jmar11 | 0:64a47a66c95d | 22 | |
jmar11 | 0:64a47a66c95d | 23 | int main() { |
jmar11 | 0:64a47a66c95d | 24 | int thresh; |
jmar11 | 0:64a47a66c95d | 25 | int timeOut = 0x7FFFFFFF; |
jmar11 | 0:64a47a66c95d | 26 | |
jmar11 | 0:64a47a66c95d | 27 | pc.printf("Please put all the sensors over white and press <enter>\n\n\r"); |
jmar11 | 0:64a47a66c95d | 28 | pc.getc(); |
jmar11 | 0:64a47a66c95d | 29 | |
jmar11 | 0:64a47a66c95d | 30 | thresh = setThresh(timeOut); |
jmar11 | 0:64a47a66c95d | 31 | |
jmar11 | 0:64a47a66c95d | 32 | timeOut = 2 * thresh; |
jmar11 | 0:64a47a66c95d | 33 | |
jmar11 | 0:64a47a66c95d | 34 | pc.printf("The values of the line sensors are below\n\r0 == white 1 == black\n\n\r"); |
jmar11 | 0:64a47a66c95d | 35 | pc.printf("1 2 3 4 5 6 7 8\n\r"); |
jmar11 | 0:64a47a66c95d | 36 | |
jmar11 | 0:64a47a66c95d | 37 | while(1){ |
jmar11 | 0:64a47a66c95d | 38 | readLine(timeOut); |
jmar11 | 0:64a47a66c95d | 39 | |
jmar11 | 0:64a47a66c95d | 40 | for(int i = 0; i < 8; i++){ |
jmar11 | 0:64a47a66c95d | 41 | if(detect[i] > thresh) |
jmar11 | 0:64a47a66c95d | 42 | detect[i] = 1; |
jmar11 | 0:64a47a66c95d | 43 | else |
jmar11 | 0:64a47a66c95d | 44 | detect[i] = 0; |
jmar11 | 0:64a47a66c95d | 45 | |
jmar11 | 0:64a47a66c95d | 46 | pc.printf("%u ", detect[i]); |
jmar11 | 0:64a47a66c95d | 47 | } |
jmar11 | 0:64a47a66c95d | 48 | pc.printf("\r"); |
jmar11 | 0:64a47a66c95d | 49 | wait_ms(50); |
jmar11 | 0:64a47a66c95d | 50 | } |
jmar11 | 0:64a47a66c95d | 51 | } |
jmar11 | 0:64a47a66c95d | 52 | |
jmar11 | 0:64a47a66c95d | 53 | void readLine(int timeOut){ |
jmar11 | 0:64a47a66c95d | 54 | for(int i = 0; i < 8; i++){ |
jmar11 | 0:64a47a66c95d | 55 | sens[i].output(); |
jmar11 | 0:64a47a66c95d | 56 | sens[i] = 1; |
jmar11 | 0:64a47a66c95d | 57 | wait_us(15); |
jmar11 | 0:64a47a66c95d | 58 | sens[i] = 0; |
jmar11 | 0:64a47a66c95d | 59 | sens[i].input(); |
jmar11 | 0:64a47a66c95d | 60 | timer.start(); |
jmar11 | 0:64a47a66c95d | 61 | while(sens[i] == 1 && timer.read_us() < timeOut){ |
jmar11 | 0:64a47a66c95d | 62 | } |
jmar11 | 0:64a47a66c95d | 63 | timer.stop(); |
jmar11 | 0:64a47a66c95d | 64 | detect[i] = timer.read_us(); |
jmar11 | 0:64a47a66c95d | 65 | timer.reset(); |
jmar11 | 0:64a47a66c95d | 66 | } |
jmar11 | 0:64a47a66c95d | 67 | } |
jmar11 | 0:64a47a66c95d | 68 | |
jmar11 | 0:64a47a66c95d | 69 | int setThresh(int timeOut){ |
jmar11 | 0:64a47a66c95d | 70 | long int temp = 0; |
jmar11 | 0:64a47a66c95d | 71 | |
jmar11 | 0:64a47a66c95d | 72 | readLine(timeOut); |
jmar11 | 0:64a47a66c95d | 73 | for(int i = 0; i < 8; i++){ |
jmar11 | 0:64a47a66c95d | 74 | temp += detect[i]; |
jmar11 | 0:64a47a66c95d | 75 | } |
jmar11 | 0:64a47a66c95d | 76 | return 9 * (temp / 8.0); |
jmar11 | 0:64a47a66c95d | 77 | } |