// Pushbike turn/blinker controler #include "FastLED.h" // LED string and type #define NUM_LEDS 4 #define LEDTYPE WS2812B // Where everything is connected #define BACK_PIN 7 #define FRONT_PIN 6 #define LEFTBUT_PIN 5 #define RIGHTBUT_PIN 4 //#define WHEEL_PIN NaN //#define HLIGHT_PIN NaN #define DEBUGLED 13 // Onboard LED // Colours - Tune to your prefrences. #define RGB_RED 255,0,0 #define RGB_YELLOW 255,150,0 //#define RGB_YELLOW 255,0,0 // Uncomment if your american. :) //#define RGB_WHITE 50,255,50 #define RGB_WHITE 63,85,63 // Pretty much whiteish. #define RGB_BWHITE 255,255,255 // 'Bright WHite'. As bright as possable. #define RGB_BLACK 0,0,0 // Globals CRGB frontleds[NUM_LEDS]; // Led CRGB backleds[NUM_LEDS]; // Arrays char mode=1; // 0 = day(indicators only), 1= night(All flashing), 2 = headlight(rear flashing, front solid), 3=All solid. char turn=0; // 0 = off, 1= left 2=right ( Allways override 'mode') char alt=0; // Patern cycle counter char butcount=0; // Counter for changing modes. /* 'alt' cycle Short cycle: 0 or 1 = blink on, 2 or 3 = blink off (0 2 = alt 1, 1 3 = alt 2) New patern: alt value 0 1 2 3 4 5 6 7 flasher 1 0 1 0 1 0 1 0 turnccc 1 1 1 1 0 0 0 0 */ void setup() { FastLED.addLeds(backleds, NUM_LEDS); FastLED.addLeds(frontleds, NUM_LEDS); pinMode(LEFTBUT_PIN, INPUT_PULLUP); pinMode(RIGHTBUT_PIN, INPUT_PULLUP); pinMode(DEBUGLED, OUTPUT); digitalWrite(DEBUGLED, HIGH); // Serial.begin(9600); } // Round and round and round we go. void loop() { checkbut(); // check buttons updateled(); // cycle in new patern FastLED.show(); // update LEDs delay(125); // Four 125 = 1 second // Cycle though all eight 'states' alt++; if(alt==8){alt=0;} } // Update LED arrays with new patterns based on turn and alt // Only gets called every 1/4 of a second void updateled() { if(turn){ // If turning all inner colours solid backleds[0] = CRGB(RGB_RED); // red backleds[2] = CRGB(RGB_RED); // red frontleds[0] = CRGB(RGB_WHITE); // white frontleds[2] = CRGB(RGB_WHITE); // white if(alt<4){ // First half of the cycle, required LEDs are yellow digitalWrite(DEBUGLED, LOW); if(turn==1) { // left backleds[1] = CRGB(RGB_YELLOW); frontleds[1] = CRGB(RGB_YELLOW); }else if(turn==2){ // right backleds[3] = CRGB(RGB_YELLOW); frontleds[3] = CRGB(RGB_YELLOW); } // No turning, should not be here } else { // if(alt>2) { // Second half, outer LEDs allways off, regardless of direction, digitalWrite(DEBUGLED, HIGH); backleds[1] = CRGB(RGB_BLACK); backleds[3] = CRGB(RGB_BLACK); frontleds[1] = CRGB(RGB_BLACK); frontleds[3] = CRGB(RGB_BLACK); } } else { // Not turning. This needs room for 'mode' later. digitalWrite(DEBUGLED, HIGH); if(mode==3) { // All solid lights, except while turning. frontleds[0] = CRGB(RGB_BWHITE); frontleds[1] = CRGB(RGB_BWHITE); frontleds[2] = CRGB(RGB_BWHITE); frontleds[3] = CRGB(RGB_BWHITE); backleds[0] = CRGB(RGB_RED); backleds[1] = CRGB(RGB_RED); backleds[2] = CRGB(RGB_RED); backleds[3] = CRGB(RGB_RED); return; // We don't care about any other modes. Quick way out. } if(alt==0 || alt==2 || alt==4 || alt==6) { // alt=0 or 2. First pattern backleds[0] = CRGB(RGB_RED); backleds[1] = CRGB(RGB_BLACK); backleds[2] = CRGB(RGB_RED); backleds[3] = CRGB(RGB_BLACK); // backleds[5] = CRGB(0,0,0); // 0 = day(indicators only), , if(mode==1) { // 1= night(All flashing) frontleds[0] = CRGB(RGB_WHITE); frontleds[1] = CRGB(RGB_BLACK); frontleds[2] = CRGB(RGB_WHITE); frontleds[3] = CRGB(RGB_BLACK); } else { // 2 = headlight(front solid, rear flashing) frontleds[0] = CRGB(RGB_BWHITE); frontleds[1] = CRGB(RGB_BWHITE); frontleds[2] = CRGB(RGB_BWHITE); frontleds[3] = CRGB(RGB_BWHITE); } } else { // alt=1 or 3. Alt pattern backleds[0] = CRGB(RGB_BLACK); backleds[1] = CRGB(RGB_RED); backleds[2] = CRGB(RGB_BLACK); backleds[3] = CRGB(RGB_RED); if(mode==1) { frontleds[0] = CRGB(RGB_BLACK); frontleds[1] = CRGB(RGB_WHITE); frontleds[2] = CRGB(RGB_BLACK); frontleds[3] = CRGB(RGB_WHITE); } else { frontleds[0] = CRGB(RGB_BWHITE); frontleds[1] = CRGB(RGB_BWHITE); frontleds[2] = CRGB(RGB_BWHITE); frontleds[3] = CRGB(RGB_BWHITE); } } } } // end updateled() // Check button status and update modes. void checkbut() { // mode: 0 = day(indicators only), 1= night(All flashing), 2 = headlight(rear flashing, front solid) // turn: 0 = off, 1= left 2=right ( Allways override 'mode') char rightstate=digitalRead(RIGHTBUT_PIN); char leftstate=digitalRead(LEFTBUT_PIN); // Everything is inverted. Serial.print("Mode:"); Serial.print(mode,DEC); Serial.print(" Buttons L:R:"); Serial.print(rightstate,BIN); Serial.println(leftstate,BIN); if(leftstate) { if(rightstate) { // none - No switches, don't turn, reset button counter turn=0; butcount=0; return; } turn=1; // left -- We are going left! }else if(rightstate) { turn=2; //right -- Or right? } else { // Both buttons are down, this is our 'mode' switch. if(butcount==1){ // Button held down for short amount of time. if(mode==2){mode=1;} else {mode=2;}; // Change mode. This needs to be 3 state, not two. } if(butcount==10) { // Button held down almost as long as 'off'. Make all lights solid. mode=3; } if(butcount>13 ){ // Button held down for much longer butcount=0; turnoff(); // Shut it all down! } butcount++; // turn=0; //none } } // end checkbut() void turnoff() { // Turn off LEDs. A bit at a time to make it pretty. backleds[1] = CRGB(RGB_BLACK); backleds[3] = CRGB(RGB_BLACK); FastLED.show(); delay(125); frontleds[1] = CRGB(RGB_BLACK); frontleds[3] = CRGB(RGB_BLACK); FastLED.show(); delay(125); backleds[0] = CRGB(RGB_BLACK); backleds[2] = CRGB(RGB_BLACK); FastLED.show(); delay(125); frontleds[0] = CRGB(RGB_BLACK); frontleds[2] = CRGB(RGB_BLACK); FastLED.show(); // while(1){}; // lock up digitalWrite(DEBUGLED, LOW); // Wait a bit to give time for buttons to be let go. And wait for button to be pressed again to wake. delay(2000); while(digitalRead(RIGHTBUT_PIN) || digitalRead(LEFTBUT_PIN) ) { // Nothing! We wait. Reading buttons Until ready! // Not power efficent. But enough to let the backpack shut down. } mode=2; // When woken up, human will hold button down, // so put in the next mode 'down' from what we want to end up in. }