Bike Turn Signal - Part 2
Using 8x8 WS2812B RGB LED Matrix, Arduino Nano & PS2 Joystick Module
Components and supplies
1
Jumper wires (generic)
1
Analog joystick (Generic)
1
RGB LED Pixel Matrix, NeoPixel NeoMatrix
1
Arduino Nano R3
Tools and machines
1
Soldering iron (generic)
Apps and platforms
1
Arduino IDE
Project description
Code
Test_Code_2.ino
arduino
Blink using Delays
1#include "FastLED.h" // Include FastLED Library 2#define Vx A0 // Define / Equate "Vx" with A0, the pin where Vx is connected 3#define Vy A1 // Define / Equate "Vy" with A1, the pin where Vy is connected 4#define Button A2 // Define / Equate Button with A2, the pin where the button is conne 5#define NUM_LEDS 64 // The number of LEDS on your matrix 6#define DATA_PIN 2 // Data-In of your matrix 7#define BRIGHTNESS 64 8CRGB leds[NUM_LEDS]; 9 10 11 12void setup() { 13 14 15 pinMode(Vx, INPUT); // Configure Vx (A0) as an Input 16 pinMode(Vy, INPUT); // Configure Vy (A1) as an Input 17 pinMode(Button, INPUT_PULLUP); // Configure Button (A2) as an Input, internally "pulled-up" to 5v 18 FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS); 19 Serial.begin(9600); 20 21} 22 23void loop() { 24 25 if (analogRead(Vx) <= 50) { // X-Axis Left 26 LeftArrowBlink(); 27 } 28 if (analogRead(Vx) >= 900) { // X-Axis Right 29 RightArrowBlink(); 30 } 31 if (analogRead(Vy) <= 50) { // Y-Axis Up 32 BlinkGreen(); 33 } 34 if (analogRead(Vy) >= 900) { // Y-Axis Down 35 BlinkRed(); 36 } 37 if (digitalRead(Button) == 0) { // Clear / Reset 38 Clear(); 39 } 40} 41 42 43void LeftArrowBlink() { // Function that is called when Vx is <= 80 and shows a Left Blinking Arrow on the matrix 44 45 46 FastLED.clear(); 47 leds[3] = CRGB(255, 18, 0); 48 leds[4] = CRGB(255, 18, 0); 49 leds[11] = CRGB(255, 18, 0); 50 leds[12] = CRGB(255, 18, 0); 51 leds[19] = CRGB(255, 18, 0); 52 leds[20] = CRGB(255, 18, 0); 53 leds[24] = CRGB(255, 18, 0); 54 leds[27] = CRGB(255, 18, 0); 55 leds[28] = CRGB(255, 18, 0); 56 leds[31] = CRGB(255, 18, 0); 57 leds[32] = CRGB(255, 18, 0); 58 leds[33] = CRGB(255, 18, 0); 59 leds[35] = CRGB(255, 18, 0); 60 leds[36] = CRGB(255, 18, 0); 61 leds[38] = CRGB(255, 18, 0); 62 leds[39] = CRGB(255, 18, 0); 63 leds[41] = CRGB(255, 18, 0); 64 leds[42] = CRGB(255, 18, 0); 65 leds[43] = CRGB(255, 18, 0); 66 leds[44] = CRGB(255, 18, 0); 67 leds[45] = CRGB(255, 18, 0); 68 leds[46] = CRGB(255, 18, 0); 69 leds[50] = CRGB(255, 18, 0); 70 leds[51] = CRGB(255, 18, 0); 71 leds[52] = CRGB(255, 18, 0); 72 leds[53] = CRGB(255, 18, 0); 73 leds[59] = CRGB(255, 18, 0); 74 leds[60] = CRGB(255, 18, 0); 75 FastLED.setBrightness(BRIGHTNESS); 76 FastLED.show(); 77 delay(500); 78 FastLED.clear(); 79 FastLED.show(); 80 delay(500); 81} 82 83 84void RightArrowBlink() {// Function that is called when Vx is >= 1000 and shows a Right Blinking Arrow on the matrix 85 86 FastLED.clear(); 87 leds[3] = CRGB(255, 18, 0); 88 leds[4] = CRGB(255, 18, 0); 89 leds[10] = CRGB(255, 18, 0); 90 leds[11] = CRGB(255, 18, 0); 91 leds[12] = CRGB(255, 18, 0); 92 leds[13] = CRGB(255, 18, 0); 93 leds[17] = CRGB(255, 18, 0); 94 leds[18] = CRGB(255, 18, 0); 95 leds[19] = CRGB(255, 18, 0); 96 leds[20] = CRGB(255, 18, 0); 97 leds[21] = CRGB(255, 18, 0); 98 leds[22] = CRGB(255, 18, 0); 99 leds[24] = CRGB(255, 18, 0); 100 leds[25] = CRGB(255, 18, 0); 101 leds[27] = CRGB(255, 18, 0); 102 leds[28] = CRGB(255, 18, 0); 103 leds[30] = CRGB(255, 18, 0); 104 leds[31] = CRGB(255, 18, 0); 105 leds[32] = CRGB(255, 18, 0); 106 leds[35] = CRGB(255, 18, 0); 107 leds[36] = CRGB(255, 18, 0); 108 leds[39] = CRGB(255, 18, 0); 109 leds[43] = CRGB(255, 18, 0); 110 leds[44] = CRGB(255, 18, 0); 111 leds[51] = CRGB(255, 18, 0); 112 leds[52] = CRGB(255, 18, 0); 113 leds[59] = CRGB(255, 18, 0); 114 leds[60] = CRGB(255, 18, 0); 115 FastLED.setBrightness(BRIGHTNESS); 116 FastLED.show(); 117 delay(500); 118 FastLED.clear(); 119 FastLED.show(); 120 delay(500); 121 122} 123 124 125void BlinkGreen() {// Function that is called when Vy is <= 10 and shows a blinking solid_fill of Green LEDs on the matrix 126 FastLED.clear(); 127 fill_solid (leds, NUM_LEDS, CRGB::Green); 128 FastLED.setBrightness(BRIGHTNESS); 129 FastLED.show(); 130 delay(500); 131 FastLED.clear(); 132 FastLED.show(); 133 delay(500); 134} 135 136 137void BlinkRed() {// Function that is called when Vy is >= 1000 and shows a blinking solid_fill of Red LEDs on the matrix 138 FastLED.clear(); 139 fill_solid (leds, NUM_LEDS, CRGB::Red); 140 FastLED.setBrightness(BRIGHTNESS); 141 FastLED.show(); 142 delay(500); 143 FastLED.clear(); 144 FastLED.show(); 145 delay(500); 146} 147 148 149void Clear() { 150 151 FastLED.clear(); 152 FastLED.show(); 153} 154
ArduinoBikeSignal MILLIS.ino
arduino
Blink using FastLED Millis
1#include "FastLED.h" // Include FastLED Library 2#define Vx A0 // Define / Equate "Vx" with A0, the pin where Vx is connected 3#define Vy A1 // Define / Equate "Vy" with A1, the pin where Vy is connected 4#define Button A2 // Define / Equate Button with A2, the pin where the button is conne 5#define NUM_LEDS 64 // The number of LEDS on your matrix 6#define DATA_PIN 2 // Data-In of your matrix 7#define BRIGHTNESS 20 // Set Brightness Level 8 9CRGB leds[NUM_LEDS]; 10 11//Setup booleans and variables here: 12boolean XL, XR, YU, YD, BT0, BT1; 13int xAxisLeft = 80; 14int xAxisRight = 700; 15int yAxisUp = 50; 16int yAxisDown = 700; 17 18void setup() { 19 20 pinMode(Vx, INPUT); // Configure Vx (A0) as an Input 21 pinMode(Vy, INPUT); // Configure Vy (A1) as an Input 22 pinMode(Button, INPUT_PULLUP); // Configure Button (A2) as an Input, internally "pulled-up" to 5V 23 // Note, we're configuring an Analog input as digital input 24 // which is perfectly fine. I did this to make the wiring easier 25 // and keep all of the wires on the same side of the board 26 FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS); 27 FastLED.setBrightness(BRIGHTNESS); 28} 29 30void loop() { 31 32 // Unblock this part to see the values on serial monitor, then setup your values for the Joystick 33 /* Serial.begin(9600); 34 Serial.print(x); // Print the X value to the serial port 35 Serial.print("\ "); // Print a Tab character 36 Serial.print(y); // Print the Y value 37 Serial.print("\ "); // Print a Tab 38 Serial.println(btn); // Print the value of the Btn (0=Pushed, 1 = Not Pushed) 39 delay(250); // Delay 250ms so the results don't print too quickly */ 40 41 //X-AXIS 42 if (analogRead(Vx) <= xAxisLeft) { // Read the value of Vx and if it's *less than or equal to* xAxisLeft which is 80. 43 BT0 = false; 44 BT1 = true; // BT1 is set to true, because when the button is open (unpushed), the input will read High (+5V). When the button is closed (pressed), the input pin is connected to ground and will read Low (0V). 45 XL = true; // XL is set to true, because we will call it together with the function LeftArrowBlink(); 46 XR = false; 47 YU = false; 48 YD = false; 49 } 50 51 else if (analogRead(Vx) > xAxisRight) { // Read the value of Vx and if it's *greater than* xAxisRight which is 1000. 52 BT0 = false; 53 BT1 = true; // BT1 is set to true, because when the button is open (unpushed), the input will read High (+5V). When the button is closed (pressed), the input pin is connected to ground and will read Low (0V). 54 XR = true; // XR is set to true, because we will call it together with the function RightArrowBlink(); 55 XL = false; 56 YU = false; 57 YD = false; 58 } 59 60 //Y-AXIS 61 if (analogRead(Vy) <= yAxisUp) { // Read the value of Vy and if it's *less than or equal to* yAxisUp which is 50. 62 BT0 = false; 63 BT1 = true; // BT1 is set to true, because when the button is open (unpushed), the input will read High (+5V). When the button is closed (pressed), the input pin is connected to ground and will read Low (0V). 64 YU = true; // YU is set to true, because we will call it together with the function BlinkGreen(); 65 YD = false; 66 XL = false; 67 XR = false; 68 } 69 else if (analogRead(Vy) > yAxisDown) { // Read the value of Vy and if it's *greater than* yAxisDown which is 700. 70 BT0 = false; 71 BT1 = true; // BT1 is set to true, because when the button is open (unpushed), the input will read High (+5V). When the button is closed (pressed), the input pin is connected to ground and will read Low (0V). 72 YD = true; // YD is set to true, because we will call it together with the function BlinkRed(); 73 YU = false; 74 XL = false; 75 XR = false; 76 } 77 if (digitalRead(Button) == BT0) { // Read the value of Button and if it *is equal to* BT0 which is 0. 78 BT0 = true; // BT0 is set to true, because when the button is closed (pressed), the input pin is connected to ground and will read Low (0V). 79 BT1 = false;// BT1 is set to false, because BT0 is active. 80 YD = false; 81 YU = false; 82 XL = false; 83 XR = false; 84 } 85 // booleans + Functions. 86 if (XL) LeftArrowBlink(); 87 if (XR) RightArrowBlink(); 88 if (YU) BlinkGreen(); 89 if (YD) BlinkRed(); 90 if (BT0) Clear(); 91} 92void LeftArrowBlink() { // Being called when XL is set to true (active) 93 EVERY_N_MILLISECONDS(500) { // I set EVERY_N_MILLISECONDS to different values, because something weird is happening when they are the same and IDK why. 94 FastLED.clear(); // To clear any active LEDs before activating LeftArrowBlink(); 95 leds[3] = CRGB(255, 18, 0); 96 leds[4] = CRGB(255, 18, 0); 97 leds[11] = CRGB(255, 18, 0); 98 leds[12] = CRGB(255, 18, 0); 99 leds[19] = CRGB(255, 18, 0); 100 leds[20] = CRGB(255, 18, 0); 101 leds[24] = CRGB(255, 18, 0); 102 leds[27] = CRGB(255, 18, 0); 103 leds[28] = CRGB(255, 18, 0); 104 leds[31] = CRGB(255, 18, 0); 105 leds[32] = CRGB(255, 18, 0); 106 leds[33] = CRGB(255, 18, 0); 107 leds[35] = CRGB(255, 18, 0); 108 leds[36] = CRGB(255, 18, 0); 109 leds[38] = CRGB(255, 18, 0); 110 leds[39] = CRGB(255, 18, 0); 111 leds[41] = CRGB(255, 18, 0); 112 leds[42] = CRGB(255, 18, 0); 113 leds[43] = CRGB(255, 18, 0); 114 leds[44] = CRGB(255, 18, 0); 115 leds[45] = CRGB(255, 18, 0); 116 leds[46] = CRGB(255, 18, 0); 117 leds[50] = CRGB(255, 18, 0); 118 leds[51] = CRGB(255, 18, 0); 119 leds[52] = CRGB(255, 18, 0); 120 leds[53] = CRGB(255, 18, 0); 121 leds[59] = CRGB(255, 18, 0); 122 leds[60] = CRGB(255, 18, 0); 123 FastLED.setBrightness(BRIGHTNESS); // Defined brightness setting 124 FastLED.show(); 125 } 126 EVERY_N_MILLISECONDS(1000) { // 2nd FastLED *delay* that clears LeftArrowBlink(); then activates it again to imitate blinking. 127 FastLED.clear(); 128 FastLED.show(); 129 } 130} 131void RightArrowBlink() { // Being called when XR is set to true (active) 132 EVERY_N_MILLISECONDS(500) { 133 FastLED.clear(); // To clear any active LEDs before activating RightArrowBlink(); 134 leds[3] = CRGB(255, 18, 0); 135 leds[4] = CRGB(255, 18, 0); 136 leds[10] = CRGB(255, 18, 0); 137 leds[11] = CRGB(255, 18, 0); 138 leds[12] = CRGB(255, 18, 0); 139 leds[13] = CRGB(255, 18, 0); 140 leds[17] = CRGB(255, 18, 0); 141 leds[18] = CRGB(255, 18, 0); 142 leds[19] = CRGB(255, 18, 0); 143 leds[20] = CRGB(255, 18, 0); 144 leds[21] = CRGB(255, 18, 0); 145 leds[22] = CRGB(255, 18, 0); 146 leds[24] = CRGB(255, 18, 0); 147 leds[25] = CRGB(255, 18, 0); 148 leds[27] = CRGB(255, 18, 0); 149 leds[28] = CRGB(255, 18, 0); 150 leds[30] = CRGB(255, 18, 0); 151 leds[31] = CRGB(255, 18, 0); 152 leds[32] = CRGB(255, 18, 0); 153 leds[35] = CRGB(255, 18, 0); 154 leds[36] = CRGB(255, 18, 0); 155 leds[39] = CRGB(255, 18, 0); 156 leds[43] = CRGB(255, 18, 0); 157 leds[44] = CRGB(255, 18, 0); 158 leds[51] = CRGB(255, 18, 0); 159 leds[52] = CRGB(255, 18, 0); 160 leds[59] = CRGB(255, 18, 0); 161 leds[60] = CRGB(255, 18, 0); 162 FastLED.setBrightness(BRIGHTNESS); 163 FastLED.show(); 164 } 165 EVERY_N_MILLISECONDS(1000) { // 2nd FastLED *delay* that clears RightArrowBlink(); then activates it again to imitate blinking. 166 FastLED.clear(); 167 FastLED.show(); 168 } 169} 170 171void BlinkGreen() { // Being called when YU is set to true (active) 172 EVERY_N_MILLISECONDS(500) { 173 FastLED.clear(); // To clear any active LEDs before activating BlinkGreen(); 174 fill_solid (leds, NUM_LEDS, CRGB::Green); 175 FastLED.setBrightness(BRIGHTNESS); 176 FastLED.show(); 177 } 178 EVERY_N_MILLISECONDS(1000) { // 2nd FastLED *delay* that clears RightArrowBlink(); then activates it again to imitate blinking. 179 FastLED.clear(); 180 FastLED.show(); 181 } 182} 183void BlinkRed() { // Being called when YD is set to true (active) 184 EVERY_N_MILLISECONDS(500) { 185 FastLED.clear(); // To clear any active LEDs before activating BlinkRed(); 186 fill_solid (leds, NUM_LEDS, CRGB::Red); 187 FastLED.setBrightness(BRIGHTNESS); 188 FastLED.show(); 189 } 190 EVERY_N_MILLISECONDS(1000) { // 2nd FastLED *delay* that clears RightArrowBlink(); then activates it again to imitate blinking. 191 FastLED.clear(); 192 FastLED.show(); 193 } 194} 195 196void Clear() { // Being called when BT0 is set to true (active) 197 198 FastLED.clear(); // To clear any active LEDs 199 FastLED.show(); 200} 201
Test_Code_2.ino
arduino
Blink using Delays
1#include "FastLED.h" // Include FastLED Library 2#define Vx A0 // Define / Equate "Vx" with A0, the pin where Vx is connected 3#define Vy A1 // Define / Equate "Vy" with A1, the pin where Vy is connected 4#define Button A2 // Define / Equate Button with A2, the pin where the button is conne 5#define NUM_LEDS 64 // The number of LEDS on your matrix 6#define DATA_PIN 2 // Data-In of your matrix 7#define BRIGHTNESS 64 8CRGB leds[NUM_LEDS]; 9 10 11 12void setup() { 13 14 15 pinMode(Vx, INPUT); // Configure Vx (A0) as an Input 16 pinMode(Vy, INPUT); // Configure Vy (A1) as an Input 17 pinMode(Button, INPUT_PULLUP); // Configure Button (A2) as an Input, internally "pulled-up" to 5v 18 FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS); 19 Serial.begin(9600); 20 21} 22 23void loop() { 24 25 if (analogRead(Vx) <= 50) { // X-Axis Left 26 LeftArrowBlink(); 27 } 28 if (analogRead(Vx) >= 900) { // X-Axis Right 29 RightArrowBlink(); 30 } 31 if (analogRead(Vy) <= 50) { // Y-Axis Up 32 BlinkGreen(); 33 } 34 if (analogRead(Vy) >= 900) { // Y-Axis Down 35 BlinkRed(); 36 } 37 if (digitalRead(Button) == 0) { // Clear / Reset 38 Clear(); 39 } 40} 41 42 43void LeftArrowBlink() { // Function that is called when Vx is <= 80 and shows a Left Blinking Arrow on the matrix 44 45 46 FastLED.clear(); 47 leds[3] = CRGB(255, 18, 0); 48 leds[4] = CRGB(255, 18, 0); 49 leds[11] = CRGB(255, 18, 0); 50 leds[12] = CRGB(255, 18, 0); 51 leds[19] = CRGB(255, 18, 0); 52 leds[20] = CRGB(255, 18, 0); 53 leds[24] = CRGB(255, 18, 0); 54 leds[27] = CRGB(255, 18, 0); 55 leds[28] = CRGB(255, 18, 0); 56 leds[31] = CRGB(255, 18, 0); 57 leds[32] = CRGB(255, 18, 0); 58 leds[33] = CRGB(255, 18, 0); 59 leds[35] = CRGB(255, 18, 0); 60 leds[36] = CRGB(255, 18, 0); 61 leds[38] = CRGB(255, 18, 0); 62 leds[39] = CRGB(255, 18, 0); 63 leds[41] = CRGB(255, 18, 0); 64 leds[42] = CRGB(255, 18, 0); 65 leds[43] = CRGB(255, 18, 0); 66 leds[44] = CRGB(255, 18, 0); 67 leds[45] = CRGB(255, 18, 0); 68 leds[46] = CRGB(255, 18, 0); 69 leds[50] = CRGB(255, 18, 0); 70 leds[51] = CRGB(255, 18, 0); 71 leds[52] = CRGB(255, 18, 0); 72 leds[53] = CRGB(255, 18, 0); 73 leds[59] = CRGB(255, 18, 0); 74 leds[60] = CRGB(255, 18, 0); 75 FastLED.setBrightness(BRIGHTNESS); 76 FastLED.show(); 77 delay(500); 78 FastLED.clear(); 79 FastLED.show(); 80 delay(500); 81} 82 83 84void RightArrowBlink() {// Function that is called when Vx is >= 1000 and shows a Right Blinking Arrow on the matrix 85 86 FastLED.clear(); 87 leds[3] = CRGB(255, 18, 0); 88 leds[4] = CRGB(255, 18, 0); 89 leds[10] = CRGB(255, 18, 0); 90 leds[11] = CRGB(255, 18, 0); 91 leds[12] = CRGB(255, 18, 0); 92 leds[13] = CRGB(255, 18, 0); 93 leds[17] = CRGB(255, 18, 0); 94 leds[18] = CRGB(255, 18, 0); 95 leds[19] = CRGB(255, 18, 0); 96 leds[20] = CRGB(255, 18, 0); 97 leds[21] = CRGB(255, 18, 0); 98 leds[22] = CRGB(255, 18, 0); 99 leds[24] = CRGB(255, 18, 0); 100 leds[25] = CRGB(255, 18, 0); 101 leds[27] = CRGB(255, 18, 0); 102 leds[28] = CRGB(255, 18, 0); 103 leds[30] = CRGB(255, 18, 0); 104 leds[31] = CRGB(255, 18, 0); 105 leds[32] = CRGB(255, 18, 0); 106 leds[35] = CRGB(255, 18, 0); 107 leds[36] = CRGB(255, 18, 0); 108 leds[39] = CRGB(255, 18, 0); 109 leds[43] = CRGB(255, 18, 0); 110 leds[44] = CRGB(255, 18, 0); 111 leds[51] = CRGB(255, 18, 0); 112 leds[52] = CRGB(255, 18, 0); 113 leds[59] = CRGB(255, 18, 0); 114 leds[60] = CRGB(255, 18, 0); 115 FastLED.setBrightness(BRIGHTNESS); 116 FastLED.show(); 117 delay(500); 118 FastLED.clear(); 119 FastLED.show(); 120 delay(500); 121 122} 123 124 125void BlinkGreen() {// Function that is called when Vy is <= 10 and shows a blinking solid_fill of Green LEDs on the matrix 126 FastLED.clear(); 127 fill_solid (leds, NUM_LEDS, CRGB::Green); 128 FastLED.setBrightness(BRIGHTNESS); 129 FastLED.show(); 130 delay(500); 131 FastLED.clear(); 132 FastLED.show(); 133 delay(500); 134} 135 136 137void BlinkRed() {// Function that is called when Vy is >= 1000 and shows a blinking solid_fill of Red LEDs on the matrix 138 FastLED.clear(); 139 fill_solid (leds, NUM_LEDS, CRGB::Red); 140 FastLED.setBrightness(BRIGHTNESS); 141 FastLED.show(); 142 delay(500); 143 FastLED.clear(); 144 FastLED.show(); 145 delay(500); 146} 147 148 149void Clear() { 150 151 FastLED.clear(); 152 FastLED.show(); 153} 154
Downloadable files
Same Schematic From Part 1
Same Schematic From Part 1

FastLED Library
https://github.com/FastLED/FastLED
FastLED Library
https://github.com/FastLED/FastLED
Same Schematic From Part 1
Same Schematic From Part 1

Comments
Only logged in users can leave comments