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.
Dependencies: mbed
main.cpp@1:113784e551f2, 2021-03-06 (annotated)
- Committer:
- awandy
- Date:
- Sat Mar 06 21:27:17 2021 +0000
- Revision:
- 1:113784e551f2
- Parent:
- 0:d575f98b29f8
- Child:
- 2:0bc1b34a5ea4
works now
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| awandy | 0:d575f98b29f8 | 1 | #include "mbed.h" |
| awandy | 1:113784e551f2 | 2 | #include <iostream> |
| awandy | 1:113784e551f2 | 3 | |
| awandy | 1:113784e551f2 | 4 | AnalogIn sen_left(PTB0); |
| awandy | 1:113784e551f2 | 5 | AnalogIn sen_right(PTC2); |
| awandy | 1:113784e551f2 | 6 | |
| awandy | 1:113784e551f2 | 7 | DigitalOut led_out0(D0); // D0 |
| awandy | 1:113784e551f2 | 8 | DigitalOut led_out1(D1); // D1 |
| awandy | 1:113784e551f2 | 9 | DigitalOut led_out2(PTD4); // D3 |
| awandy | 1:113784e551f2 | 10 | DigitalOut led_out3(PTC7); // the last one |
| awandy | 1:113784e551f2 | 11 | |
| awandy | 1:113784e551f2 | 12 | Ticker locator; |
| awandy | 1:113784e551f2 | 13 | const float ti_L = 1e-3; |
| awandy | 1:113784e551f2 | 14 | const float threshold = 0.2; |
| awandy | 1:113784e551f2 | 15 | |
| awandy | 1:113784e551f2 | 16 | float sen_left_val = 0; |
| awandy | 1:113784e551f2 | 17 | float sen_right_val = 0; |
| awandy | 1:113784e551f2 | 18 | int index = 0; // position index |
| awandy | 1:113784e551f2 | 19 | int rem = 0; // remainder for conversion to binary |
| awandy | 1:113784e551f2 | 20 | bool left_sensed = false; |
| awandy | 1:113784e551f2 | 21 | bool right_sensed = false; |
| awandy | 1:113784e551f2 | 22 | bool left_prev = false; |
| awandy | 1:113784e551f2 | 23 | bool right_prev = false; |
| awandy | 0:d575f98b29f8 | 24 | |
| awandy | 1:113784e551f2 | 25 | void locate() { |
| awandy | 1:113784e551f2 | 26 | sen_left_val = sen_left.read(); |
| awandy | 1:113784e551f2 | 27 | sen_right_val = sen_right.read(); |
| awandy | 1:113784e551f2 | 28 | |
| awandy | 1:113784e551f2 | 29 | left_prev = left_sensed; |
| awandy | 1:113784e551f2 | 30 | right_prev = right_sensed; |
| awandy | 1:113784e551f2 | 31 | |
| awandy | 1:113784e551f2 | 32 | left_sensed = sen_left_val >= threshold; // true if black tape detected |
| awandy | 1:113784e551f2 | 33 | right_sensed = sen_right_val >= threshold; |
| awandy | 1:113784e551f2 | 34 | |
| awandy | 1:113784e551f2 | 35 | if (left_prev == left_sensed && right_prev == right_sensed) // skip this sample if nothing changed |
| awandy | 1:113784e551f2 | 36 | return; |
| awandy | 1:113784e551f2 | 37 | else if (left_sensed && right_sensed) // both sensors triggered |
| awandy | 1:113784e551f2 | 38 | index = 0; |
| awandy | 1:113784e551f2 | 39 | else if (left_sensed && !right_sensed || right_sensed && !left_sensed) // only one triggered |
| awandy | 1:113784e551f2 | 40 | index++; |
| awandy | 1:113784e551f2 | 41 | |
| awandy | 1:113784e551f2 | 42 | if (index == 16) |
| awandy | 1:113784e551f2 | 43 | index = 0; |
| awandy | 1:113784e551f2 | 44 | |
| awandy | 1:113784e551f2 | 45 | rem = index % 2; |
| awandy | 1:113784e551f2 | 46 | led_out0.write(rem == 1); |
| awandy | 1:113784e551f2 | 47 | rem = (index / 2) % 2; |
| awandy | 1:113784e551f2 | 48 | led_out1.write(rem == 1); |
| awandy | 1:113784e551f2 | 49 | rem = (index / 4) % 2; |
| awandy | 1:113784e551f2 | 50 | led_out2.write(rem == 1); |
| awandy | 1:113784e551f2 | 51 | rem = (index / 8) % 2; |
| awandy | 1:113784e551f2 | 52 | led_out3.write(rem == 1); |
| awandy | 1:113784e551f2 | 53 | } |
| awandy | 1:113784e551f2 | 54 | |
| awandy | 0:d575f98b29f8 | 55 | |
| awandy | 0:d575f98b29f8 | 56 | int main() { |
| awandy | 1:113784e551f2 | 57 | |
| awandy | 1:113784e551f2 | 58 | locator.attach(&locate,ti_L); |
| awandy | 1:113784e551f2 | 59 | led_out0.write(0); |
| awandy | 1:113784e551f2 | 60 | led_out1.write(0); |
| awandy | 1:113784e551f2 | 61 | led_out2.write(0); |
| awandy | 1:113784e551f2 | 62 | led_out3.write(0); |
| awandy | 1:113784e551f2 | 63 | |
| awandy | 0:d575f98b29f8 | 64 | while(1) { |
| awandy | 1:113784e551f2 | 65 | wait(1); |
| awandy | 1:113784e551f2 | 66 | cout << index << "\r\n" << std::endl; |
| awandy | 1:113784e551f2 | 67 | cout << "left prev " << left_prev << "\r\n" |
| awandy | 1:113784e551f2 | 68 | << "right prev " << right_prev << "\r\n" |
| awandy | 1:113784e551f2 | 69 | << "left current " << left_sensed << "\r\n" |
| awandy | 1:113784e551f2 | 70 | << "right current " << right_sensed << "\r\n" << std::endl; |
| awandy | 0:d575f98b29f8 | 71 | } |
| awandy | 0:d575f98b29f8 | 72 | } |