Hi Folks,
I'm working on something that must do the following:
A prox sensor picks up pulse from a rotating device.
A variable (NbLED) is increased by 1
Every 250 mSec, that same variable is decreased by one
A strip of 30 WS2812B LED is lit according to NbLED (red for the first 15 LEDs, followed by yellow and green)
The idea, is that pulses must come in on a regular basis. If the pace at which they come in decrease, the number of LED lit will decrease.
The prox used is an inductive sensor, NPN (from Ebay: Prox Sw used). I have made a circuit that debounce the inputs.
So far, so good...
My problem is that when the attached code is running, a pulse from the prox will increase by more than 1 count. Strangely, the actual behaviour is that when detecting metal, the NbLED variable increase by 1, but when the metal is leaving, the count increase by 7-8 counts....
I have disabled the WS2812B portion, to replace it by a Serial.println that tells me the actual count: the count is increasing by only one, just as expected !
What the hell am I doing wrong ????
I have also attached the schematic of my skid....
#include <TimerOne.h>
#include <Adafruit_NeoPixel.h>
int Pin = 4;
int NbLED = 1;
int NumPixels = 30;
Adafruit_NeoPixel Pixels = Adafruit_NeoPixel(NumPixels, Pin, NEO_GRB + NEO_KHZ800);
void setup()
{
Timer1.initialize(250000); // set a timer of length 250000 microseconds (or 0.25 sec)
Timer1.attachInterrupt( timerIsr );
Pixels.begin();
attachInterrupt(digitalPinToInterrupt(3),Pulse, FALLING);
}
void loop()
{
if(NbLED==0)
{
NbLED = 1;
}
if(NbLED >15)
{
if(NbLED >25)
{
for(int i=0;i<15;i++)
{
Pixels.setPixelColor(i, 255,0,0); //Red
Pixels.show();
}
for(int i=15;i<25;i++)
{
Pixels.setPixelColor(i,255,150,0);//Yellow
Pixels.show();
}
for(int i=25;i<NbLED;i++)
{
Pixels.setPixelColor(i,100,255,0); //Green
Pixels.show();
}
for(int i=NbLED;i<NumPixels;i++)
{
Pixels.setPixelColor(i,0,0,0); // Blank
Pixels.show();
}
}
else
{
for(int i=0;i<15;i++)
{
Pixels.setPixelColor(i, 255,0,0); //Red
Pixels.show();
}
for(int i=15;i<NbLED;i++)
{
Pixels.setPixelColor(i,255,150,0); //Yellow
Pixels.show();
}
for(int i=NbLED;i<NumPixels;i++)
{
Pixels.setPixelColor(i,0,0,0); //Blank
Pixels.show();
}
}
}
else
{
for(int i=0;i<NbLED;i++)
{
Pixels.setPixelColor(i, 255,0,0);
Pixels.show();
}
for(int i=NbLED;i<NumPixels;i++)
{
Pixels.setPixelColor(i,0,0,0);
Pixels.show();
}
}
}
void Pulse()
{
NbLED = NbLED+1;
}
void timerIsr()
{
NbLED = NbLED - 1;
}
FYI: I use 2 libraries:
NeoPixel from Adafruit, to drive the WS2812B
Timer1: which allow time interrupt easily
