Flora_Brakelight_Backpack.ino
1 // SPDX-FileCopyrightText: 2019 Anne Barela for Adafruit Industries 2 // 3 // SPDX-License-Identifier: MIT 4 5 #include <Wire.h> 6 #include <Adafruit_LSM303.h> 7 #include <SPI.h> 8 #include <Adafruit_WS2801.h> 9 10 Adafruit_LSM303 lsm; 11 12 #define BRAKETHRESHOLD 350 13 #define BRAKETIMETHRESHOLD 200 14 15 int dataPin = 2; // Yellow wire on Adafruit Pixels 16 int clockPin = 3; // Green wire on Adafruit Pixels 17 const int cPin = 11; 18 const int dPin = 6; 19 20 // Set the first variable to the NUMBER of pixels. 32 = 32 pixels in a row 21 // The LED strips are 32 LEDs per meter but you can extend/cut the strip 22 Adafruit_WS2801 strip = Adafruit_WS2801(36,dataPin,clockPin); 23 24 int start = 0; 25 26 int prevX = 0; 27 int currentX = 0; 28 29 int cState = 0; 30 int dState = 0; 31 32 long brakeTime = 0; 33 34 void setup() 35 { 36 Serial.begin(9600); 37 38 // Start up the LED strip 39 strip.begin(); 40 // Update the strip, to start they are all 'off' 41 strip.show(); 42 // Try to initialise and warn if we couldn't detect the chip 43 if (!lsm.begin()) 44 { 45 Serial.println("Oops ... unable to initialize the LSM303. Check your wiring!"); 46 while (1); 47 } 48 pinMode(cPin, INPUT); 49 pinMode(dPin, INPUT); 50 } 51 52 void loop() 53 { 54 check_switches(); // when we check the switches we'll get the current state 55 56 lsm.read(); 57 currentX = abs(lsm.accelData.x); 58 59 if (start == 0){ 60 prevX = currentX; 61 start = 1; 62 } 63 64 int i = currentX - prevX; 65 66 if (abs(i) > BRAKETHRESHOLD) { 67 brakeTime = millis(); 68 int strikes = 0; 69 while ((abs(i) > BRAKETHRESHOLD) && (strikes < 3)) { 70 if (abs(i) < BRAKETHRESHOLD) { 71 strikes = strikes + 1; 72 } 73 lsm.read(); 74 currentX = abs(lsm.accelData.x); 75 i = currentX - prevX; 76 77 if ((millis() - brakeTime) > BRAKETIMETHRESHOLD) { 78 brakeLights(Color(255,0,0),250); 79 while (abs(i) > BRAKETHRESHOLD) { 80 lsm.read(); 81 currentX = abs(lsm.accelData.x); 82 i = currentX - prevX; 83 Serial.println(i); 84 delay(100); 85 } 86 hideAll(); 87 brakeTime = millis(); 88 i = 0; 89 lsm.read(); 90 currentX = abs(lsm.accelData.x); 91 } 92 } 93 } 94 95 prevX = currentX; 96 delay(200); 97 } 98 99 void check_switches() 100 { 101 cState = digitalRead(cPin); 102 dState = digitalRead(dPin); 103 104 if (cState == HIGH) { 105 // left blinker 106 Serial.println("left blink on"); 107 hideAll(); 108 leftTurn(Color(255,63,0),250); 109 delay(300); 110 Serial.println("left blink off"); 111 hideAll(); 112 delay(300); 113 } 114 115 if (dState == HIGH) { 116 // right blinker 117 Serial.println("right blink on"); 118 hideAll(); 119 rightTurn(Color(255,63,0),250); 120 delay(300); 121 Serial.println("right blink off"); 122 hideAll(); 123 delay(300); 124 } 125 } 126 127 void leftTurn(uint32_t c,uint8_t wait){ 128 innerLeftBottom(c); 129 innerLeftTop(c); 130 strip.show(); 131 delay(wait); 132 hideAll(); 133 outerLeftTop(c); 134 outerLeftBottom(c); 135 strip.show(); 136 delay(wait); 137 hideAll(); 138 } 139 140 void rightTurn(uint32_t c,uint8_t wait){ 141 innerRightBottom(c); 142 innerRightTop(c); 143 strip.show(); 144 delay(wait); 145 hideAll(); 146 outerRightTop(c); 147 outerRightBottom(c); 148 strip.show(); 149 delay(wait); 150 hideAll(); 151 } 152 153 void brakeLights(uint32_t c, uint8_t wait){ 154 innerRightBottom(c); 155 innerRightTop(c); 156 innerLeftBottom(c); 157 innerLeftTop(c); 158 strip.show(); 159 delay(wait); 160 hideAll(); 161 outerLeftTop(c); 162 outerLeftBottom(c); 163 outerRightTop(c); 164 outerRightBottom(c); 165 strip.show(); 166 delay(wait); 167 hideAll(); 168 } 169 170 171 /* Helper functions */ 172 173 //Input a value 0 to 384 to get a color value. 174 //The colours are a transition r - g - b - back to r 175 176 void outerRightBottom(uint32_t c){ 177 for (int i=0; i < 5; i++) { 178 strip.setPixelColor(i, c); 179 } 180 } 181 void outerRightTop(uint32_t c){ 182 for (int i=5; i < 10; i++) { 183 strip.setPixelColor(i, c); 184 } 185 } 186 void innerRightTop(uint32_t c){ 187 for (int i=10; i < 14; i++) { 188 strip.setPixelColor(i, c); 189 } 190 } 191 void innerRightBottom(uint32_t c){ 192 for (int i=14; i < 18; i++) { 193 strip.setPixelColor(i, c); 194 } 195 } 196 197 void innerLeftBottom(uint32_t c){ 198 for (int i=18; i < 22; i++) { 199 strip.setPixelColor(i, c); 200 strip.show(); 201 } 202 } 203 204 void innerLeftTop(uint32_t c){ 205 for (int i=22; i < 26; i++) { 206 strip.setPixelColor(i, c); 207 } 208 } 209 210 void outerLeftTop(uint32_t c){ 211 for (int i=26; i < 31; i++) { 212 strip.setPixelColor(i, c); 213 } 214 } 215 void outerLeftBottom(uint32_t c){ 216 for (int i=31; i < 36; i++) { 217 strip.setPixelColor(i, c); 218 } 219 } 220 221 void hideAll(){ 222 for(int i = 0; i > strip.numPixels();i++){ 223 strip.setPixelColor(i,Color(0,0,0)); 224 } 225 strip.show(); 226 } 227 228 // Create a 24 bit color value from R,G,B 229 uint32_t Color(byte r, byte g, byte b) 230 { 231 uint32_t c; 232 c = r; 233 c <<= 8; 234 c |= g; 235 c <<= 8; 236 c |= b; 237 return c; 238 }