My first Arduino project!
I'm building a device to add to my motorcycle, which quickly blinks the brake light for a moment when I step on the brake, before illuminating the brake light as usual. Here is the code, as requested by Larry. Let me know if you have any questions or need help getting it working!
Features a selectable number of blinks via a switch on pin 9, and a 3-second blink lockout to prevent excessive blinking during rapid braking cycles.
Code:
// Motorcycle Tail Light Control
const int light = 7; // output for brake light, digital pin
const int sw = 8; // input for brake light switch, digital pin
const int flashpref = 9; // input for flash number preference, digital pin (LOW = 2 flashes, HIGH = 3 flashes)
const int ontime = 75; // flash duration ON time
const int offtime = 50; // flash duration OFF time
const long delaytime = 3000; // delay timeout (ms)
long releasetime;
long interval;
void setup() {
pinMode(light, OUTPUT);
pinMode(sw, INPUT);
pinMode(flashpref, INPUT);
releasetime = -90000000;
flash(); // startup chime
}
void loop() {
interval = millis() - releasetime; // calculate how long the brake has been off
if(digitalRead(sw) == HIGH && interval >= delaytime){ // flash + hold! (if more than 3sec elapsed)
flash();
while(digitalRead(sw) == HIGH){ //holds the brake light on
digitalWrite(light, HIGH);
releasetime = millis();
}
}
else if(digitalRead(sw) == HIGH && interval < delaytime){ // turns the brake light on without flashing (if less than 3 sec)
while(digitalRead(sw) == HIGH){
digitalWrite(light, HIGH);
releasetime = millis();
}
}
else // turns the brake light off
{
digitalWrite(light, LOW);
}
}
void flash(){ // function for flashing
digitalWrite(light, HIGH);
delay(ontime);
digitalWrite(light, LOW);
delay(offtime);
digitalWrite(light, HIGH);
delay(ontime);
digitalWrite(light, LOW);
delay(offtime);
if(digitalRead(flashpref) == HIGH){ // flash once more if preference is set
digitalWrite(light, HIGH);
delay(ontime);
digitalWrite(light, LOW);
delay(offtime);
}
}
Attachment:
Screen shot 2010-11-18 at 3.05.47 AM.jpg [ 106.36 KiB | Viewed 962 times ]