It is currently Fri Apr 18, 2014 4:50 pm


Post a new topicPost a reply Page 1 of 1   [ 1 post ]
Author Message
 Post subject: Arduino Brake Light Modulator
PostPosted: Thu Nov 18, 2010 4:06 am 

Joined: Thu May 27, 2010 11:05 am
Posts: 13
Location: /dev/null
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
Screen shot 2010-11-18 at 3.05.47 AM.jpg [ 106.36 KiB | Viewed 1384 times ]


Top
 Profile  
 
Display posts from previous:  Sort by  
Post a new topicPost a reply Page 1 of 1   [ 1 post ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron


Powered by phpBB