Automatic bicycle light

Hello fellow Arduino users.

My name is Joey and I’m currently registered under the name DrStickle, this is my player name on a lot of online games for those who want to know!

Currently i have thought of something that is a little to complicated as i have encountered along the way.

My project is making a bicycle head light that does almost everything automatic. This is for my mother who has a super nice electric bicycle, but no head light ??? i know right.

The idea is that i use a hall effect sensor to sense if the bicycle is moving (lets say above 2 rpm), after it senses that the Arduino will check an LDR (light sensor) whom it asks if the light level is above or under 512 (i have to fiddle around with this number in real life situations later, this works best on my desk).

If so, the rpm is above 2 and the LDR says the light level is below 512 it will turn on a led light (3x1w in my case) wich it will use PWM on to turn them on 50% brightness. This is also to provide a little power saving and uses less power then powering 2x1w at 90% !. Also the case i have bought has 3 spots for leds.

Then in turn it will turn on a counter, so that if you cycle under a streetlight your headlight won’t turn off immediately. And it will give you a little light when storing your bicycle (i set it on 60 seconds). When the counter turns to 0 the light will power off and can only be triggered by the rpm increase and LDR value being low enough. So it won’t turn on suddenly in your garage or shed.

Below i have my code where i have a few little problems.

I tested all the sensors standalone, and then tried to implement it in one script. Also i switched to a few other codes when i found a better or simpler code online! so there could be a line unnecessary somewhere.

My skills aren’t that great yet, although i learn everyday that i fiddle around with the Arduino and as mentioned before i managed to get everything running standalone! wich i’m very proud of.

As of now it prints the LDR value, and the battery warning (oh yes i implemented a little safe guard for the lipo battery), but no rpm! I used the position of the original script but that didn’t help.

Also the light doesn’t turn on, so i guess there is a kink in the cable as we call it in the Netherlands.

Is there anyone who can point me in the right direction to fix this?

int led1 = 9;
int cled1 = 12;
int counter;
int LDR = A1;
int ldrValue;
float vPow = 4.7;
float r1 = 100000;
float r2 = 10000;
int value = 0;
int brightness =0;
//boolean isAan = false;
int half_revolutions = 0;
int rpm = 0;

//Halleffect sensor pin 2, interrupt 0 = pin 2
unsigned long lastmillis = 0;

void setup()
 analogWrite(led1, brightness);
 pinMode(led1, OUTPUT);
 pinMode(cled1, OUTPUT);
 counter = 0; 
 attachInterrupt(0, rpm_fan, FALLING);

void loop(){

if (millis() - lastmillis == 1000){ //Uptade every one second, this will be equal to reading frecuency (Hz).

detachInterrupt(0);//Disable interrupt when calculating

rpm = half_revolutions * 60; // Convert frecuency to RPM, note: this works for one interruption per full rotation. For two interrups per full rotation use half_revolutions * 30.
half_revolutions = 0; // Restart the RPM counter
lastmillis = millis(); // Uptade lasmillis
attachInterrupt(0, rpm_fan, FALLING); //enable interrupt

 float v = (analogRead(0) * vPow) / 1024.0;
  float v2 = v / (r2 / (r1 + r2));
 if (v2 < 3.1)
   digitalWrite(cled1, HIGH);
 digitalWrite(cled1, LOW);

  ldrValue = analogRead(LDR);
  if (ldrValue <= 512 && rpm > 2)
    counter = 65;
    //isAan = true;
    brightness = 128;
    analogWrite(led1, brightness);
  else if (ldrValue <=512 && rpm > 2)
    counter = 60;
  else if (ldrValue > 512 && rpm < 2)
    counter --;
  if (counter == 0)
    brightness = 0;
    analogWrite(led1, brightness);
   Serial.print(rpm); // print the rpm value

void rpm_fan(){

I am using a Arduino Uno r3 to test, and would like this to run on a attiny85 later on. But for now i will use a promini wich has all the connection on the same pins so i won’t get confused!

Hopefully someone sees my problem and can help me fix it so it atleast prints the rpm for now. Thank you for reading my post.

What do your debug prints tell you?

half_revolutions should really be qualified "volatile"

Please remember to use code tags when posting code.

Hello AWOl,

Thank you for reading and replying so quickly, i changed the code tag part (didn't know/didn't search if it was necessary).

Like i mentioned, if i startup the serial monitor it prints the Voltage of the voltage meter part, and it will print the LDR value. It won't print a 0 for the rpm, wich it does in the source code if there is no input.

Or am i reading your question wrong?