// Program for integrating brake lights and turn signals into bike handlebars. int ledB1 = 5, ledG1 = 6, ledR1 = 3, ledB2 = 10, ledG2 = 11, ledR2 = 9; int lButton = 2, rButton = 4, lButtonState = 0, rButtonState = 0; int brakeTime = 4000; int turnTime = 400, turnCount = 8; int checkCount = 0, limit = 2; int wait = 20, longWait = 40, shortWait = 20, lightMax = 255; int off = 255, on = 0; int upperBound = 140, lowerBound = 60; void setup() { pinMode(ledB1, OUTPUT); pinMode(ledG1, OUTPUT); pinMode(ledR1, OUTPUT); pinMode(ledB2, OUTPUT); pinMode(ledG2, OUTPUT); pinMode(ledR2, OUTPUT); pinMode(lButton, INPUT); pinMode(rButton, INPUT); } void loop() { randColor(); } void check() { lButtonState = digitalRead(lButton); rButtonState = digitalRead(rButton); if(lButtonState == HIGH || rButtonState == HIGH) { if(lButtonState == HIGH && rButtonState == HIGH) { brake(); } else if(lButtonState == HIGH) { lTurn(); } else { rTurn(); } } } void updateCheck() { checkCount++; if(checkCount > limit) { checkCount = 0; check(); } } void randColor() { // red, green fade in for(int i = 190; i < lightMax; i++) { updateCheck(); if(i > lowerBound && i < 255) { wait = longWait; } else { wait = shortWait; } analogWrite(ledB2, off); analogWrite(ledG2, off - i); analogWrite(ledR2, on); analogWrite(ledB1, off); analogWrite(ledG1, off - i); analogWrite(ledR1, on); delay(wait); } // red fade out, green for(int i = 0; i < lightMax; i++) { updateCheck(); if(i > 0 && i < upperBound) { wait = longWait; } else { wait = shortWait; } analogWrite(ledB2, off); analogWrite(ledG2, on); analogWrite(ledR2, on + i); analogWrite(ledB1, off); analogWrite(ledG1, on); analogWrite(ledR1, on + i); delay(wait); } // green, blue fade in for(int i = 0; i < lightMax; i++) { updateCheck(); if(i > lowerBound && i < upperBound) { wait = longWait; } else { wait = shortWait; } analogWrite(ledB2, off - i); analogWrite(ledG2, on); analogWrite(ledR2, off); analogWrite(ledB1, off - i); analogWrite(ledG1, on); analogWrite(ledR1, off); delay(wait); } // green fade out, blue for(int i = 0; i < lightMax; i++) { updateCheck(); if(i > lowerBound && i < upperBound) { wait = longWait; } else { wait = shortWait; } analogWrite(ledB2, on); analogWrite(ledG2, on + i); analogWrite(ledR2, off); analogWrite(ledB1, on); analogWrite(ledG1, on + i); analogWrite(ledR1, off); delay(wait); } // blue, red fade in for(int i = 0; i < lightMax; i++) { updateCheck(); if(i > lowerBound && i < upperBound) { wait = longWait; } else { wait = shortWait; } analogWrite(ledB2, on); analogWrite(ledG2, off); analogWrite(ledR2, off - i); analogWrite(ledB1, on); analogWrite(ledG1, off); analogWrite(ledR1, off - i); delay(wait); } // blue fade out, red for(int i = 0; i < 120; i++) { updateCheck(); if(i > lowerBound && i < upperBound) { wait = longWait; } else { wait = shortWait; } analogWrite(ledB2, on + i); analogWrite(ledG2, off); analogWrite(ledR2, on); analogWrite(ledB1, on + i); analogWrite(ledG1, off); analogWrite(ledR1, on); delay(wait); } } void brake() { red(); delay(brakeTime); } void lTurn() { for(int i = 0; i < turnCount; i++) { lRed(); delay(turnTime); allOff(); delay(turnTime); } } void rTurn() { for(int i = 0; i < turnCount; i++) { rRed(); delay(turnTime); allOff(); delay(turnTime); } } void lRed() { allOff(); digitalWrite(ledG1, HIGH); digitalWrite(ledR1, LOW); digitalWrite(ledB1, HIGH); } void rRed() { allOff(); digitalWrite(ledG2, HIGH); digitalWrite(ledR2, LOW); digitalWrite(ledB2, HIGH); } void red() { allOff(); digitalWrite(ledG2, HIGH); digitalWrite(ledR2, LOW); digitalWrite(ledB2, HIGH); digitalWrite(ledG1, HIGH); digitalWrite(ledR1, LOW); digitalWrite(ledB1, HIGH); } void allOff() { digitalWrite(ledG1, HIGH); digitalWrite(ledR1, HIGH); digitalWrite(ledB1, HIGH); digitalWrite(ledG2, HIGH); digitalWrite(ledR2, HIGH); digitalWrite(ledB2, HIGH); }