Hello Arduinoers,
I am attempting to create a bicycle light from a neopixel strip where the lightpattern is controlled by the bike speed. When the bike is not moving, a new light pattern should take over.
The sensor I'm using is a hall effect.
Speed reading works just fine, but when the bike stops, there are no more triggers and the speed stays as the last recorded.
I am struggling to get a "reading" of 0 k/ph, but of course the interrupt only triggers when there is movement.
Here is a bare bones version of the code. I had to clean up a lot of mess, so without the attemps of a 0-reading:
/*
NeoPixel strip PIN6.
Hall effect sensor attached to PIN2
*/
//#include <LiquidCrystal.h>
#include <Adafruit_NeoPixel.h>
#define CHANGE 1
#define FALLING 2
#define RISING 3
#define PIN 6 //Attach NeoPixel strip
#define NUM_PIXELS 24
//const int hallpin = 2;
//int hallState = 0;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_PIXELS, PIN, NEO_GRB + NEO_KHZ800);
unsigned long start, finished;
unsigned long elapsed, time;
float circ = 2.00; // wheel circumference (meters)
float speedkph;
float mps;
float LEDspeed;
float gap;
const int debounce = 100; // debounce milliseconds
void setup() {
attachInterrupt(0, speedCalc, RISING); // interrupt called when rising edge is detected on digital pin 3
start = millis();
Serial.begin(9600);
strip.begin();
strip.show();// Initialize all pixels to 'off'
delay(2000);
}
void speedCalc() {
unsigned long now = millis();
if (digitalRead(0) == HIGH) // check if the pin is really high
{
if (now - start > debounce)
{
elapsed = now - start;
start = now;
speedkph = (3600 * circ) / elapsed; // kph
mps = (speedkph * 0.28); //calculates meters per second from kph
LEDspeed = (33.33 / mps); //calculates milliseconds between LEDs
gap = (1000 / mps); //spacing between LED chases
}
}
Serial.print("speedkph: ");
Serial.println(speedkph);
}
void loop() {
if (speedkph > 0) {
for (int led = 24; led >= 0; led--) {
strip.setPixelColor(led, 0, 0, 180);
strip.show();
delay(LEDspeed);
}
delay(gap);
for (int led = 24; led >= 0; led--) {
strip.setPixelColor(led, 0, 0, 0);
strip.show();
delay(LEDspeed);
}
}
if (speedkph == 0) {
//new light pattern
}
}
Is there any way for the interrupt to generate 0 k/ph? I have tried with:
if (elapsed > 2000) {
speedkph = 0;
...both within the interrupt and in the loop, but to no avail.
I assume, it is best to keep this outside of the main loop, for it to run smoothly.
So to keep this short, I humbly ask for ideas or suggestions (or the need to clarify).
Thanks for reading and for any help!