/* * ____ _ ______ _____ _____ / __ \ | | | ____| __ \ | __ \ | | | |_ __ ___ _ __ | | | |__ | | | | | |__) |__ _ ___ ___ | | | | '_ \ / _ \ '_ \ | | | __| | | | | | _ // _` |/ __/ _ \ | |__| | |_) | __/ | | | | |____| |____| |__| | | | \ \ (_| | (_| __/ \____/| .__/ \___|_| |_| |______|______|_____/ |_| \_\__,_|\___\___| | | |_| Open LED Race An minimalist cars race for LED strip This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. by gbarbarov@singulardevices.com for Arduino day Seville 2019 Code made dirty and fast, next improvements in: https://github.com/gbarbarov/led-race https://www.hackster.io/gbarbarov/open-led-race-a0331a https://twitter.com/openledrace */ //version Basic for PCB Rome Edition // 2 Player , without Boxes Track #include #define MAXLED 300 // MAX LEDs actives on strip #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 A2 // switch player 1 to PIN and GND #define PIN_P2 A0 // 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 Color(255,0,0) #define COLOR2 Color(0,255,0) int win_music[] = { 2637, 2637, 0, 2637, 0, 2093, 2637, 0, 3136 }; byte gravity_map[MAXLED]; int TBEEP=0; int FBEEP=0; byte SMOTOR=0; 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() { Serial.begin(115200); for(int i=0;i127) 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) {if (leader==2) {FBEEP=440;TBEEP=10;} leader=1;} if (dist2>dist1) {if (leader==1) {FBEEP=440*2;TBEEP=10;} leader=2;}; if (dist1>NPIXELS*loop1) {loop1++;TBEEP=10;FBEEP=440;}; if (dist2>NPIXELS*loop2) {loop2++;TBEEP=10;FBEEP=440*2;}; if (loop1>loop_max) {Serial.println("w1"); for(int i=0;iloop_max) {Serial.println("w2"); for(int i=0;i0) {TBEEP--;} else {FBEEP=0;}; }