Lights up shoes and sends steps to a person's phone over bluetooth
Fork of NeoPixels by
main.cpp@1:d235193680ab, 2016-04-14 (annotated)
- Committer:
- hippi345
- Date:
- Thu Apr 14 16:08:35 2016 +0000
- Revision:
- 1:d235193680ab
- Parent:
- 0:f38492690f0e
- Child:
- 2:1a511cc48d0d
LED Shoes ECE 4180
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aswild | 0:f38492690f0e | 1 | #include "mbed.h" |
aswild | 0:f38492690f0e | 2 | #include "NeoStrip.h" |
aswild | 0:f38492690f0e | 3 | #include "gt.h" |
aswild | 0:f38492690f0e | 4 | #define N 64 |
aswild | 0:f38492690f0e | 5 | #define PATTERNS 3 |
aswild | 0:f38492690f0e | 6 | |
aswild | 0:f38492690f0e | 7 | int hueToRGB(float h); |
hippi345 | 1:d235193680ab | 8 | |
hippi345 | 1:d235193680ab | 9 | |
aswild | 0:f38492690f0e | 10 | |
hippi345 | 1:d235193680ab | 11 | AnalogIn photocell(p16); |
hippi345 | 1:d235193680ab | 12 | RawSerial dev(p28,p27); |
aswild | 0:f38492690f0e | 13 | NeoStrip strip(p18, N); |
hippi345 | 1:d235193680ab | 14 | DigitalOut myled(LED1); |
hippi345 | 1:d235193680ab | 15 | DigitalIn vibration(p15); |
hippi345 | 1:d235193680ab | 16 | Serial pc(USBTX,USBRX); |
hippi345 | 1:d235193680ab | 17 | int count = 0; |
hippi345 | 1:d235193680ab | 18 | int senseValCur; |
hippi345 | 1:d235193680ab | 19 | int senseValPre = 1; |
aswild | 0:f38492690f0e | 20 | |
aswild | 0:f38492690f0e | 21 | // timer used for debugging |
aswild | 0:f38492690f0e | 22 | Timer timer; |
aswild | 0:f38492690f0e | 23 | |
aswild | 0:f38492690f0e | 24 | int hueToRGB(float h) |
aswild | 0:f38492690f0e | 25 | { |
aswild | 0:f38492690f0e | 26 | // lots of floating point magic from the internet and scratching my head |
aswild | 0:f38492690f0e | 27 | float r, g, b; |
aswild | 0:f38492690f0e | 28 | if (h > 360) |
aswild | 0:f38492690f0e | 29 | h -= 360; |
aswild | 0:f38492690f0e | 30 | if (h < 0) |
aswild | 0:f38492690f0e | 31 | h += 360; |
aswild | 0:f38492690f0e | 32 | int i = (int)(h / 60.0); |
aswild | 0:f38492690f0e | 33 | float f = (h / 60.0) - i; |
aswild | 0:f38492690f0e | 34 | float q = 1 - f; |
aswild | 0:f38492690f0e | 35 | |
aswild | 0:f38492690f0e | 36 | switch (i % 6) |
aswild | 0:f38492690f0e | 37 | { |
aswild | 0:f38492690f0e | 38 | case 0: r = 1; g = f; b = 0; break; |
aswild | 0:f38492690f0e | 39 | case 1: r = q; g = 1; b = 0; break; |
aswild | 0:f38492690f0e | 40 | case 2: r = 0; g = 1; b = f; break; |
aswild | 0:f38492690f0e | 41 | case 3: r = 0; g = q; b = 1; break; |
aswild | 0:f38492690f0e | 42 | case 4: r = f; g = 0; b = 1; break; |
aswild | 0:f38492690f0e | 43 | case 5: r = 1; g = 0; b = q; break; |
aswild | 0:f38492690f0e | 44 | default: r = 0; g = 0; b = 0; break; |
aswild | 0:f38492690f0e | 45 | } |
aswild | 0:f38492690f0e | 46 | // scale to integers and return the packed value |
aswild | 0:f38492690f0e | 47 | uint8_t R = (uint8_t)(r * 255); |
aswild | 0:f38492690f0e | 48 | uint8_t G = (uint8_t)(g * 255); |
aswild | 0:f38492690f0e | 49 | uint8_t B = (uint8_t)(b * 255); |
aswild | 0:f38492690f0e | 50 | |
aswild | 0:f38492690f0e | 51 | return (R << 16) | (G << 8) | B; |
aswild | 0:f38492690f0e | 52 | } |
aswild | 0:f38492690f0e | 53 | |
hippi345 | 1:d235193680ab | 54 | |
hippi345 | 1:d235193680ab | 55 | int main() |
hippi345 | 1:d235193680ab | 56 | { |
hippi345 | 1:d235193680ab | 57 | vibration.mode(PullUp); |
hippi345 | 1:d235193680ab | 58 | |
hippi345 | 1:d235193680ab | 59 | |
hippi345 | 1:d235193680ab | 60 | float bright = 0.2; // 20% is plenty for indoor use |
hippi345 | 1:d235193680ab | 61 | |
hippi345 | 1:d235193680ab | 62 | |
hippi345 | 1:d235193680ab | 63 | strip.setBrightness(bright); // set default brightness |
hippi345 | 1:d235193680ab | 64 | |
hippi345 | 1:d235193680ab | 65 | while (true) |
hippi345 | 1:d235193680ab | 66 | { |
hippi345 | 1:d235193680ab | 67 | strip.setBrightness(photocell); |
hippi345 | 1:d235193680ab | 68 | myled = vibration.read(); |
hippi345 | 1:d235193680ab | 69 | senseValCur = vibration.read(); |
hippi345 | 1:d235193680ab | 70 | timer.reset(); // use a timer to measure loop execution time for debugging purposes |
hippi345 | 1:d235193680ab | 71 | timer.start(); // for this application, the main loop takes approximately 3ms to run |
hippi345 | 1:d235193680ab | 72 | |
hippi345 | 1:d235193680ab | 73 | |
hippi345 | 1:d235193680ab | 74 | if(senseValCur == 0) |
hippi345 | 1:d235193680ab | 75 | { |
hippi345 | 1:d235193680ab | 76 | strip.setBrightness(photocell/4); |
hippi345 | 1:d235193680ab | 77 | static float dh = 360.0 / N; |
hippi345 | 1:d235193680ab | 78 | static float x = 0; |
hippi345 | 1:d235193680ab | 79 | |
hippi345 | 1:d235193680ab | 80 | for (int i = 0; i < N; i++) |
hippi345 | 1:d235193680ab | 81 | {strip.setPixel(i, hueToRGB((dh * i) - x)); |
hippi345 | 1:d235193680ab | 82 | |
hippi345 | 1:d235193680ab | 83 | |
hippi345 | 1:d235193680ab | 84 | x += 1; |
hippi345 | 1:d235193680ab | 85 | if (x > 360) |
hippi345 | 1:d235193680ab | 86 | x = 0; |
hippi345 | 1:d235193680ab | 87 | strip.write(); |
hippi345 | 1:d235193680ab | 88 | wait(0.01);} |
hippi345 | 1:d235193680ab | 89 | |
hippi345 | 1:d235193680ab | 90 | |
hippi345 | 1:d235193680ab | 91 | strip.setPixels(0, N, gt_img2); |
hippi345 | 1:d235193680ab | 92 | strip.write(); |
hippi345 | 1:d235193680ab | 93 | wait(.005); |
hippi345 | 1:d235193680ab | 94 | count++; |
hippi345 | 1:d235193680ab | 95 | pc.printf("%i\n\r",count); |
hippi345 | 1:d235193680ab | 96 | dev.printf("%i\n\r",count); |
hippi345 | 1:d235193680ab | 97 | } |
hippi345 | 1:d235193680ab | 98 | senseValPre = senseValCur; |
hippi345 | 1:d235193680ab | 99 | } |
hippi345 | 1:d235193680ab | 100 | } |
hippi345 | 1:d235193680ab | 101 | |
hippi345 | 1:d235193680ab | 102 | |
hippi345 | 1:d235193680ab | 103 |