/* MotoLights Independently pulses a bank of 6 sets of LEDs. This code is in the public domain. Paul Spooner */ boolean debug = false; byte iterdelay = 12; // the top level iteration delay byte turndelay = 15; // the turn signal iteration delay byte ledpin[7] = {13,3,11,5,10,6,9}; // The output pin for the LED byte rightleds[] = {1,3,5}; byte leftleds[] = {2,4,6}; byte ledlevel[7] = {64,64,64,64,64,64,64,}; // the current brightness of the LED byte ledfastramp[7] = {4,4,4,4,4,4,4}; // the fast ramp speed for changing the LED brighness byte ledtarget[7] = {30,30,30,30,30,30,30}; // the current target brightness byte ledleap[7] = {60,60,60,60,60,60,60}; // how large the leap up in brightness is byte ledboost[7] = {0,3,3,2,2,1,1}; // boost for adjusting for led position, 1 for tips, 2 for mid, 3 for root byte leftinpin = 8; byte rightinpin = 4; byte auxinpin = 7; unsigned long time; unsigned long nextpulse = 0; unsigned int pulseperiod = 2000; //boolean highBeamsOn = true; void randomizeparams(byte i) { // randomize the fast ramp speed int adjust = ledfastramp[i] + random(-1,2); byte lowerlimit = 7-ledboost[i]*2; byte upperlimit = 14-ledboost[i]*3; ledfastramp[i] = constrain(adjust, lowerlimit, upperlimit); // randomize the leap size int inclination = -8-ledboost[i]; adjust = ledleap[i] + random(inclination,12); lowerlimit = 72-ledboost[i]*12; upperlimit = 127-ledboost[i]*12; ledleap[i] = constrain(adjust, lowerlimit, upperlimit); // randomize the target level adjust = ledtarget[i] + random(-7,6); lowerlimit = 1+ledboost[i]*24; upperlimit = 240+ledboost[i]*5-ledleap[i]; ledtarget[i] = constrain(adjust, lowerlimit, upperlimit); } void outputLEDvalue(byte i) { byte invert = 0; byte commandlevel = ledlevel[i]; if (commandlevel < 10){ commandlevel = 0; } //if (!highBeamsOn) commandlevel /= 2; invert = 255-commandlevel; // P-type inverts the signal analogWrite(ledpin[i], invert); } void lightloop() { byte fastcutoff; for (byte i = 0; i<7; i++) { fastcutoff = ledtarget[i] + ledleap[i]/(5-ledboost[i]); if (ledlevel[i] > fastcutoff) ledlevel[i] -= ledfastramp[i]; // fast decay else if (ledlevel[i] >= ledtarget[i]) ledlevel[i]--; else if (ledlevel[i] < ledtarget[i]) { // level has fallen below the target // boost the level ledlevel[i] += ledleap[i]; randomizeparams(i); } outputLEDvalue(i); } } void turnloop() { // a consistent decay, to keep the lights synched for (byte i = 0; i<7; i++) { if (ledlevel[i] > 253) { ledlevel[i] = 1; } else if (ledlevel[i] > 230){ ledlevel[i] += 1; } else if ((230 > ledlevel[i]) && (ledlevel[i] > 55)) { ledlevel[i] = 232; } if (ledlevel[i] == 0) { ledlevel[i] = 11; outputLEDvalue(i); ledlevel[i] = 0; } else { ledlevel[i] += 1; outputLEDvalue(i); } } } void turnright() { boolean keepturning = true; ledlevel[rightleds[2]] = 1; ledlevel[rightleds[1]] = 6; ledlevel[rightleds[0]] = 11; ledlevel[leftleds[0]] = 0; ledlevel[leftleds[1]] = 0; ledlevel[leftleds[2]] = 0; while (keepturning) { keepturning = false; for (byte i = 0; i<24; i++) { turnloop(); delay(turndelay); if (digitalRead(rightinpin)) keepturning = true; } } } void turnleft() { boolean keepturning = true; ledlevel[rightleds[2]] = 0; ledlevel[rightleds[1]] = 0; ledlevel[rightleds[0]] = 0; ledlevel[leftleds[0]] = 11; ledlevel[leftleds[1]] = 6; ledlevel[leftleds[2]] = 1; while (keepturning) { keepturning = false; for (byte i = 0; i<24; i++) { turnloop(); delay(turndelay); if (digitalRead(leftinpin)) keepturning = true; } } } // runs once when reset void setup() { randomSeed(analogRead(0)); // initialize the random number generator for (byte i = 0; i<7; i++) pinMode(ledpin[i], OUTPUT); // initialize the pin as an output. pinMode(leftinpin, INPUT); pinMode(rightinpin, INPUT); pinMode(auxinpin, INPUT); if (debug) { Serial.begin(9600); Serial.print("initialized\n"); } } void loop() { // iterate all of the light values if (digitalRead(leftinpin)) turnleft(); if (digitalRead(rightinpin)) turnright(); lightloop(); delay(iterdelay); time = millis(); if (time > nextpulse){ // schedule the next pulse nextpulse += pulseperiod; // send a synchronized pulse across all of the leds for (byte i = 0; i<7; i++) { ledlevel[i] = 255; // Maximize all of the LED levels outputLEDvalue(i); if (ledfastramp[i] < 3) randomizeparams(i); // randomization pass for low decay values. } delay(iterdelay); // dwell at the max light level } if (debug) { Serial.print(digitalRead(leftinpin)); Serial.print(" left\n"); Serial.print(digitalRead(rightinpin)); Serial.print(" right\n"); Serial.print(digitalRead(auxinpin)); Serial.print(" aux\n"); } }