Arduino LED Race Game using WS2812B LEDs Strip
Table of Contents
Arduino LED Race Game:
Arduino LED Race Game using WS2812b LEDs Strip- Hi Readers, in this article, I will show you how to make LED racing game using Arduino and the WS2812B LEDs Strip in very simple way, if you are a complete beginner no need to worry as I have given all the details that you can follow very easily, so let’s begin to build this project.
What is LED RACE Game?
You have heard of many races before, car race, horse race, and a running race. Here the concept is same but we are not using any human interference here in the game, In fact I can see it’s a race between Light emitting diodes!
Yes you heard that correct, here we are racing against the LEDs, and the length of race depends on the length of the LED strip. I have made one of the amazing LED arcade game in my previous post you can check that HERE.
So as you are clear about the project that we are going to make, we will now see what are the materials that are necessary to build this project
Materials Required
- Arduino Nano/ Uno
- WS2812B ARGB LEDs strip
- LED or Buzzer
- Jumper wires male type
- Push buttons x 2
- Breadboard
- Soldering iron
- Arduino IDE and Programming cable
- Double side tape/ hot glue
Amazon Links:
Arduino Nano USB-C Type (Recommended)
*Disclosure: These are affiliate links. As an Amazon Associate I earn from qualifying purchases.
Why ARGB LED strip?
ARGB or known as addressable Red green Blue LED are type of bulbs in which we can choose time and intensity along with color of each LED as per our requirements.
Above image shows the WS2812B LEDs strip which I will be using in this project, Now you may think how much LED so I need in a strip for my project?
The answer for this question is, it is left to you, I would like to say more the number of LED’s the better the game will be. It’s like longer race track, the lengthy the game more interesting the game will be. In my case I used 240 LED and length of the strip was around 300cm.
One thing to be taken care of here is some LED strips come with very close placing, you can check placement of mine in the below image.
ARGB strips are controlled by Arduino or raspberry pi and they are completely programmable, Meaning you can generate any colors on single LED in a strip of your choice by sending commands with the help of codes.
So these features make it most suitable for us to make this project.
If you see the image above closely you can see the letter Di, Di means signal input and you should look for this arrow before soldering wires to the strip.
To make the circuit easier I soldered male type jumper wires to push buttons and LED strip, so that these can be connected and removed easily whenever needed.
Circuit Diagram for Arduino LED RACE GAME
This is very simple circuit diagram, to be more clear here is the explanation.
Push buttons has 4 terminals out of which 2 are common, you can check this by checking the back sides. Rails are connected with each other are 1 side, and one side here will be connected to D7 in the Nano board, where as the other side will be connected to Gnd. Repeat same step for push button 2, but here we are connecting one Pin to D6 and other to Gnd. This completes our controller setup! One switch is for player 1 and other is for player 2.
Now as said before the LED strip comes with 3 terminals, one for +5v supply, one for gnd and other for Signal input. Here we will connect signal pin to A0 of Nano board.
The game indicating option can be done via LED or by piezoelectric buzzer, I used LED here and it is connected to D3.
To make the circuit simple I used a mini breadboard and connected all this common terminals to Gnd and power supply.
One more thing here should be noted here is if you think to use this for a very long time I would suggest using an external power supply. With internal supply the board will have some negative effect as the longer LED strip consumes more current.
The complete circuit looks like the above image, and now we can add some codes that will make this project work.
Arduino Code for LED Race Game
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
#include <Adafruit_NeoPixel.h> //IF YOU DONT HAVE THIS LIBRARY YOU CAN DOWNLOAD FROM GITHUB, //JUST SEARCH FOR Adafruit NEOPIXEL LIBRARY// #define MAXLED 240 // MAX LEDs actives on strip //Pins Arduino Day 19 version #define PIN_LED A0 // R 500 ohms to DI pin for WS2812 and WS2813, //for WS2813 BI pin of first LED to GND , //CAP 1000 uF to VCC 5v/GND,power supplie 5V 2A #define PIN_P1 7 // switch player 1 to PIN and GND #define PIN_P2 6 // switch player 2 to PIN and GND #define PIN_AUDIO 3 // through CAP 2uf to speaker 8 ohms //Pins Maker Faire Roma 19 version //#define PIN_LED 2 // R 500 ohms to DI pin for WS2812 and WS2813, // for WS2813 BI pin of first LED to GND , // CAP 1000 uF to VCC 5v/GND,power supplie 5V 2A //#define PIN_P1 A0 // switch player 1 to PIN and GND //#define PIN_P2 A2 // switch player 2 to PIN and GND //#define PIN_AUDIO 3 // through CAP 2uf to speaker 8 ohms int NPIXELS=MAXLED; // leds on track #define COLOR1 track.Color(255,0,0) #define COLOR2 track.Color(0,255,0) int win_music[] = { 2637, 2637, 0, 2637, 0, 2093, 2637, 0, 3136 }; byte gravity_map[MAXLED]; int TBEEP=3; float speed1=0; float speed2=0; float dist1=0; float dist2=0; byte loop1=0; byte loop2=0; byte leader=0; byte loop_max=5; //total laps race float ACEL=0.2; float kf=0.015; //friction constant float kg=0.003; //gravity constant byte flag_sw1=0; byte flag_sw2=0; byte draworder=0; unsigned long timestamp=0; Adafruit_NeoPixel track = Adafruit_NeoPixel(MAXLED, PIN_LED, NEO_GRB + NEO_KHZ800); int tdelay = 5; void set_ramp(byte H,byte a,byte b,byte c) {for(int i=0;i<(b-a);i++){gravity_map[a+i]=127-i*((float)H/(b-a));}; gravity_map[b]=127; for(int i=0;i<(c-b);i++){gravity_map[b+i+1]=127+H-i*((float)H/(c-b));}; } void set_loop(byte H,byte a,byte b,byte c) {for(int i=0;i<(b-a);i++){gravity_map[a+i]=127-i*((float)H/(b-a));}; gravity_map[b]=255; for(int i=0;i<(c-b);i++){gravity_map[b+i+1]=127+H-i*((float)H/(c-b));}; } void setup() { for(int i=0;i<NPIXELS;i++){gravity_map[i]=127;}; track.begin(); pinMode(PIN_P1,INPUT_PULLUP); pinMode(PIN_P2,INPUT_PULLUP); if ((digitalRead(PIN_P1)==0)) //push switch 1 on reset for activate physic { set_ramp(12,90,100,110); // ramp centred in LED 100 with 10 led fordward and 10 backguard for(int i=0;i<NPIXELS;i++) { track.setPixelColor(i, track.Color(0,0,(127-gravity_map[i])/8) );}; track.show(); }; start_race(); } void start_race(){ for(int i=0;i<NPIXELS;i++) { track.setPixelColor(i, track.Color(0,0,0)); }; track.show(); delay(2000); track.setPixelColor(12, track.Color(0,255,0)); track.setPixelColor(11, track.Color(0,255,0)); track.show(); tone(PIN_AUDIO,400); delay(2000); noTone(PIN_AUDIO); track.setPixelColor(12, track.Color(0,0,0)); track.setPixelColor(11, track.Color(0,0,0)); track.setPixelColor(10, track.Color(255,255,0)); track.setPixelColor(9, track.Color(255,255,0)); track.show(); tone(PIN_AUDIO,600); delay(2000); noTone(PIN_AUDIO); track.setPixelColor(9, track.Color(0,0,0)); track.setPixelColor(10, track.Color(0,0,0)); track.setPixelColor(8, track.Color(255,0,0)); track.setPixelColor(7, track.Color(255,0,0)); track.show(); tone(PIN_AUDIO,1200); delay(2000); noTone(PIN_AUDIO); timestamp=0; }; void winner_fx() { int msize = sizeof(win_music) / sizeof(int); for (int note = 0; note < msize; note++) { tone(PIN_AUDIO, win_music[note],200); delay(230); noTone(PIN_AUDIO); } }; void burning1(){ //to do } void burning2(){ //to do } void track_rain_fx(){ //to do } void track_oil_fx(){ //to do } void track_snow_fx(){ //to do } void fuel_empty(){ //to do } void fill_fuel_fx(){ //to do } void in_track_boxs_fx(){ //to do } void pause_track_boxs_fx(){ //to do } void flag_boxs_stop(){ //to do } void flag_boxs_ready(){ //to do } void draw_safety_car(){ //to do } void telemetry_rx(){ //to do } void telemetry_tx(){ //to do } void telemetry_lap_time_car1(){ //to do } void telemetry_lap_time_car2(){ //to do } void telemetry_record_lap(){ //to do } void telemetry_total_time(){ //to do } int read_sensor(byte player){ //to do } int calibration_sensor(byte player){ //to do } int display_lcd_laps(){ //to do } int display_lcd_time(){ //to do } void draw_car1(void) {for(int i=0;i<=loop1;i++) { track.setPixelColor(((word)dist1 % NPIXELS)+i, track.Color(0,255-i*20,0)); }; } void draw_car2(void) {for(int i=0;i<=loop2;i++) {track.setPixelColor(((word)dist2 % NPIXELS)+i, track.Color(255-i*20,0,0)); }; } void loop() { //for(int i=0;i<NPIXELS;i++){track.setPixelColor(i, track.Color(0,0,0));}; for(int i=0;i<NPIXELS;i++) {track.setPixelColor(i, track.Color(0,0,(127-gravity_map[i])/8) ); }; if ( (flag_sw1==1) && (digitalRead(PIN_P1)==0) ) {flag_sw1=0;speed1+=ACEL;}; if ( (flag_sw1==0) && (digitalRead(PIN_P1)==1) ) {flag_sw1=1;}; if ((gravity_map[(word)dist1 % NPIXELS])<127) speed1-=kg*(127-(gravity_map[(word)dist1 % NPIXELS])); if ((gravity_map[(word)dist1 % NPIXELS])>127) speed1+=kg*((gravity_map[(word)dist1 % NPIXELS])-127); speed1-=speed1*kf; if ( (flag_sw2==1) && (digitalRead(PIN_P2)==0) ) {flag_sw2=0;speed2+=ACEL;}; if ( (flag_sw2==0) && (digitalRead(PIN_P2)==1) ) {flag_sw2=1; }; if ((gravity_map[(word)dist2 % NPIXELS])<127) speed2-=kg*(127-(gravity_map[(word)dist2 % NPIXELS])); if ((gravity_map[(word)dist2 % NPIXELS])>127) speed2+=kg*((gravity_map[(word)dist2 % NPIXELS])-127); speed2-=speed2*kf; dist1+=speed1; dist2+=speed2; if (dist1>dist2) {leader=1;} if (dist2>dist1) {leader=2;}; if (dist1>NPIXELS*loop1) {loop1++;tone(PIN_AUDIO,600);TBEEP=2;}; if (dist2>NPIXELS*loop2) {loop2++;tone(PIN_AUDIO,700);TBEEP=2;}; if (loop1>loop_max) {for(int i=0;i<NPIXELS;i++) {track.setPixelColor(i, track.Color(0,255,0));}; track.show(); winner_fx(); loop1=0; loop2=0; dist1=0; dist2=0; speed1=0; speed2=0; timestamp=0; start_race(); } if (loop2>loop_max) {for(int i=0;i<NPIXELS;i++) {track.setPixelColor(i, track.Color(255,0,0)); }; track.show(); winner_fx(); loop1=0; loop2=0; dist1=0; dist2=0; speed1=0; speed2=0; timestamp=0; start_race(); } if ((millis() & 512)==(512*draworder)) { if (draworder==0) {draworder=1; } else {draworder=0; } }; if (draworder==0) {draw_car1();draw_car2();} else {draw_car2();draw_car1();} track.show(); delay(tdelay); if (TBEEP>0) { TBEEP-=1; if (TBEEP==0) {noTone(PIN_AUDIO);}; // lib conflict !!!! interruption off by neopixel }; } |
Before uploading the code to board please check for some important changes that is given below.
In the second line of code #define MAXLED that asks what is the count of LED in the strip you have to enter the number of LED in your strip, if you use the same number which I use and if there is no such count in your strip this project wont work correctly.
Manually count the number of LED and enter correct values, I have marked the changing area to yellow for easier navigation.
If you want colors other than red and green you can change the color codes in the line #define COLOR1 and #define COLOR2, to make the changes as per your color wish know the RGB code of your colorand add the same in track.color(*your values here in R,G,B format*).
If you are not sure of these and don’t want to change you can keep the same default color.
Now you can upload the code to Arduino, after it is done test the project.
As you can see from the above image the length of the LED is too long, to get a better view of LED I installed this setup on a wooden table, so the running LEDs are visible.
How to Play LED RACE GAME?
You can see the setup which I made, You can also use a vertical setup too.
Now we will see how to play this game
Connect power supply to the board, I used usb power supply for demonstration.
There will be 2 players in the game one controls the GREEN LED and the other player controls RED LED, the more frequent you push the buttons the more faster the LED moves.
There are 3 Laps, meaning you should complete total 3 rounds and whoever reaches the last lap first will be the winner of game. And to indicate this at the end entire strip of light will display the color of winner, Suppose red light player is the winner the entire strip glows to red and its same with green also.
This completes our LED RACE game project, if you have any questions you can ask us in the comments we will answer your question as soon as possible.