Hi!
My problem: I've written the code for using an WS2812b strip with 7 LEDs as a rpm led bar. Input is hall effect sensor. It works pretty fine, but unfortunately, the led bar doesn't restore when theres no further input and I don't get it done. So the LEDS stay on until a new Input occurs.
I'm pretty new on this subject, so pleas excuse my lack of knowledge!
Thanks
#include <Adafruit_NeoPixel.h>
const int LED_PIN = 5;
const int TACH_PIN = 2;
const int BRIGHTNESS = 100;
const int LED_COUNT = 7;
const int REDLINE = 1180;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
bool blinkState = true;
unsigned long lastBlinkUpdate = 0;
volatile unsigned long lastIgnitionTime = 0;
void recordIgnition() {
detachInterrupt(digitalPinToInterrupt(TACH_PIN));
lastIgnitionTime = millis();
attachInterrupt(digitalPinToInterrupt(TACH_PIN), calculateRpm, RISING);
}
void calculateRpm() {
detachInterrupt(digitalPinToInterrupt(TACH_PIN));
unsigned long ignitionTimeDelta = millis() - lastIgnitionTime;
int rpm = 1000.0 / ignitionTimeDelta * 60;
Serial.print("RPM: ");
Serial.println(rpm);
setRpm(rpm * 10);
attachInterrupt(digitalPinToInterrupt(TACH_PIN), recordIgnition, RISING);
}
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
pinMode(TACH_PIN, INPUT);
pinMode(5, OUTPUT);
strip.begin();
//strip.setBrightness(BRIGHTNESS);
strip.show(); // Initialize all pixels to 'off'
attachInterrupt(digitalPinToInterrupt(TACH_PIN), recordIgnition, RISING);
}
void loop() {
}
void setRpm(int rpm) {
int rpmPerLed = REDLINE / LED_COUNT;
for(int led = 0; led < LED_COUNT; led++) {
if((led+1)*rpmPerLed <= rpm)
strip.setPixelColor(led, getColor((led+1)*rpmPerLed, 1));
else if(led*rpmPerLed < rpm && (led+1)*rpmPerLed > rpm) {
strip.setPixelColor(led, getColor(rpm, (double)(rpm-(led)*rpmPerLed)/rpmPerLed));
}
else
strip.setPixelColor(led, 0, 0, 0);
}
strip.show();
}
uint32_t getColor(int rpm, double intensity) {
int red = rpm/(double)REDLINE * 255 * intensity;
int green = (1-rpm/(double)REDLINE) * 255 * intensity;
int blue = 0;
return strip.Color(red, green, blue);
}
void restore () {
}