ece4180final
/
NeoPixel_Proximity
read IR sensor and blink when somethings too close
main.cpp@1:ef61b41abdf0, 2014-04-25 (annotated)
- Committer:
- rhodes42
- Date:
- Fri Apr 25 14:40:42 2014 +0000
- Revision:
- 1:ef61b41abdf0
- Parent:
- 0:90226220dccf
first commit;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rhodes42 | 0:90226220dccf | 1 | #include "mbed.h" |
rhodes42 | 0:90226220dccf | 2 | #include "NeoStrip.h" |
rhodes42 | 0:90226220dccf | 3 | |
rhodes42 | 0:90226220dccf | 4 | DigitalOut myled(LED1); |
rhodes42 | 0:90226220dccf | 5 | AnalogIn ir(p20); |
rhodes42 | 0:90226220dccf | 6 | Serial pc(USBTX, USBRX); |
rhodes42 | 0:90226220dccf | 7 | |
rhodes42 | 0:90226220dccf | 8 | #define N 59 |
rhodes42 | 0:90226220dccf | 9 | |
rhodes42 | 0:90226220dccf | 10 | NeoStrip strip(p18, N); |
rhodes42 | 0:90226220dccf | 11 | |
rhodes42 | 0:90226220dccf | 12 | void blinkOn(); |
rhodes42 | 0:90226220dccf | 13 | void blinkOff(); |
rhodes42 | 0:90226220dccf | 14 | |
rhodes42 | 0:90226220dccf | 15 | |
rhodes42 | 0:90226220dccf | 16 | void blinkOn() |
rhodes42 | 0:90226220dccf | 17 | { |
rhodes42 | 0:90226220dccf | 18 | for (int i = 0; i < N; i++) |
rhodes42 | 0:90226220dccf | 19 | { |
rhodes42 | 0:90226220dccf | 20 | if (i % 2) |
rhodes42 | 0:90226220dccf | 21 | strip.setPixel(i, 0xff, 0, 0); |
rhodes42 | 0:90226220dccf | 22 | else |
rhodes42 | 0:90226220dccf | 23 | strip.setPixel(i, 0, 0, 0); |
rhodes42 | 0:90226220dccf | 24 | } |
rhodes42 | 0:90226220dccf | 25 | wait_ms(50); |
rhodes42 | 0:90226220dccf | 26 | strip.write(); |
rhodes42 | 0:90226220dccf | 27 | } |
rhodes42 | 0:90226220dccf | 28 | |
rhodes42 | 0:90226220dccf | 29 | void blinkOff() |
rhodes42 | 0:90226220dccf | 30 | { |
rhodes42 | 0:90226220dccf | 31 | for (int i = 0; i < N; i++) |
rhodes42 | 0:90226220dccf | 32 | { |
rhodes42 | 0:90226220dccf | 33 | strip.setPixel(i, 0, 0, 0); |
rhodes42 | 0:90226220dccf | 34 | } |
rhodes42 | 0:90226220dccf | 35 | wait_ms(60); |
rhodes42 | 0:90226220dccf | 36 | strip.write(); |
rhodes42 | 0:90226220dccf | 37 | } |
rhodes42 | 0:90226220dccf | 38 | |
rhodes42 | 0:90226220dccf | 39 | int main() { |
rhodes42 | 0:90226220dccf | 40 | |
rhodes42 | 0:90226220dccf | 41 | float buffer [10]; |
rhodes42 | 0:90226220dccf | 42 | |
rhodes42 | 0:90226220dccf | 43 | while(1) |
rhodes42 | 0:90226220dccf | 44 | { |
rhodes42 | 0:90226220dccf | 45 | for (int i = 0; i < 10; i++) |
rhodes42 | 0:90226220dccf | 46 | { |
rhodes42 | 0:90226220dccf | 47 | float val; |
rhodes42 | 0:90226220dccf | 48 | val = 21/ir; |
rhodes42 | 0:90226220dccf | 49 | buffer[i] = val; |
rhodes42 | 0:90226220dccf | 50 | float sum = 0; |
rhodes42 | 0:90226220dccf | 51 | for (int j = 0; j < 10; j++) |
rhodes42 | 0:90226220dccf | 52 | { |
rhodes42 | 0:90226220dccf | 53 | sum += buffer[j]; |
rhodes42 | 0:90226220dccf | 54 | } |
rhodes42 | 0:90226220dccf | 55 | float avg = sum/10; |
rhodes42 | 0:90226220dccf | 56 | pc.printf("%f\r\n", avg); |
rhodes42 | 0:90226220dccf | 57 | |
rhodes42 | 0:90226220dccf | 58 | if (avg < 70) |
rhodes42 | 0:90226220dccf | 59 | { |
rhodes42 | 0:90226220dccf | 60 | blinkOn(); |
rhodes42 | 0:90226220dccf | 61 | wait_ms(100); |
rhodes42 | 0:90226220dccf | 62 | blinkOff(); |
rhodes42 | 0:90226220dccf | 63 | wait_ms(100); |
rhodes42 | 0:90226220dccf | 64 | blinkOn(); |
rhodes42 | 0:90226220dccf | 65 | wait_ms(100); |
rhodes42 | 0:90226220dccf | 66 | blinkOff(); |
rhodes42 | 0:90226220dccf | 67 | wait_ms(100); |
rhodes42 | 0:90226220dccf | 68 | blinkOn(); |
rhodes42 | 0:90226220dccf | 69 | wait_ms(100); |
rhodes42 | 0:90226220dccf | 70 | blinkOff(); |
rhodes42 | 0:90226220dccf | 71 | wait_ms(100); |
rhodes42 | 0:90226220dccf | 72 | |
rhodes42 | 0:90226220dccf | 73 | } |
rhodes42 | 0:90226220dccf | 74 | } |
rhodes42 | 0:90226220dccf | 75 | } |
rhodes42 | 0:90226220dccf | 76 | |
rhodes42 | 0:90226220dccf | 77 | } |