Bike light

Hi everyone,
I am trying to create a automatic bike light with the help of Arduino.

I want the bike light to turn on when I sit on the seat because of a force sensitive resistor embedded in the seat. However, if I'm riding in the daylight I want the light not to turn on - i think a photo resistor on the bike could achieve this. This means, the light will only turn on when I'm is seated and it is night time. Also, when I press the brake (i.e. a button) I want this to not only make the bike brake but also make the light flash, to let everyone know I am braking.

I have tried my code out on the Arduino simulator on 123D circuit, but it doesn't seem to work. I was working if anyone could have a look at my code and provide some feedback on what I might be doing wrong, as I think it is my code that is wrong and not my circuit. Thanks in advance to anyone who replies!

Here is my code and I have also attached an image of my circuit-

int ledpin = 11;
int sensePin1 = A0;
int sensePin2 = A1;
int sensePin3 = 7;

int senseReading;
int lightReading;
int pressureReading;
boolean buttonReading;

void setup()
{
  pinMode(ledpin, OUTPUT);
  pinMode(sensePin1, INPUT);
  pinMode(sensePin2, INPUT);
  pinMode(sensePin3, INPUT_PULLUP);
  Serial.begin(9600);

}

void loop()
{
  lightReading = analogRead(sensePin1);
  pressureReading = analogRead(sensePin2);
  buttonReading = digitalRead(sensePin3);

  if (lightReading < 300 && pressureReading > 500) {
    //it's dark and person on seat so turn light on
    digitalWrite(ledpin, HIGH);

  } else {
    //it's light and no one is on bike so turn light off
    digitalWrite(ledpin, LOW);
    Serial.println(senseReading);
  }
  
  if (buttonReading == HIGH) {
    //the brake button has been pushed and the light flashes/blinks
    digitalWrite(ledpin, LOW);  // turn LED OFF
    delay (500);
    digitalWrite(ledpin, HIGH);  // turn LED ON
    delay (500);

  } else {
    //it's light and no one is on bike so turn light off
    digitalWrite(ledpin, LOW);
    Serial.println(senseReading);
  }
}

What do you plan to do about the case where you are going up hill and need to stand up to pedal?

You don't seem to do anything with buttonReading, which I think is the one for the brake. (Hint: use better names for the pins and their associated readings :slight_smile: )

I'm thinking you need an "if" on that one, and then only do the blink without delay stuff inside that if when the the button is pressed. I'd see that all as independent of the day/night are you on the bike or not logic, whereas now it's inside that.

Also, makes everyone's life easier if you would go and edit your opening post and put the code in code tags using the </> icon at top left.

Hmm I didn't think about that gpsmikey

thanks for the reply JimboZA - I have since edited it with ur comments in mind

But your brake blink if is still inside the daylight/on saddle if, and moreover is now a blink with delay

ur right JimboZA, ill change it

Wait - do you have two lights or one?

in any case,

loop {
  if Im braking
    digitalWrite(light, (millis() & 256)!=0) // flash duration 1/4 second
  else if i'm sitting and it's not daylight
    digitalWrite(light, true);
  else 
    digitalWrite(light, false);