Speed sensor error condition on startup
Posted: Tue Jan 12, 2021 7:31 am
Hi, I am using an interupt to catch motion events through a slotted infrared type sensor (spped / tacho sensor). The code works reliably and fine catching each event apart from this one error case. on startup, there is garbage on the serial monitor. On hitting the reset button and activating the slotted sensor at the same time, the sketch runs normally with events being displayed on the serial monitoring screen
Any advice on how to solve or sidestep this startup issue greatly appreciated, thank you.
Sensor connected to D3, Like the one here
Code:
void ICACHE_RAM_ATTR isr()
{
unsigned long now = micros();
float pulselength;
pulseNow = now;
int averagepulse;
int totalsample = 0;
if ((pulseNow - pulseThen) > resolution)
{
Serial.print("numPulses ");
Serial.print(numPulses+1);
Serial.print(" ");
pulselength = (float)(pulseNow - pulseThen) /1000000;
Serial.print(pulselength);
Serial.print("\n");
if( count < sample )
{
keeppulse[count] = pulselength;
count++;
}
else
{
count = 0;
Serial.print("Average in a sample of ");
Serial.print(sample+1);
Serial.print(" is ");
for( int x=0; (x<sample); x++ ) {
totalsample += (int)keeppulse[x];
}
averagepulse = totalsample/sample;
Serial.print(averagepulse);
Serial.print("\n\n");
}
pulseThen = pulseNow;
++numPulses;
}
}
void setup()
{
Serial.begin(19200);
pinMode(3, OUTPUT); //probe output on pin 3
attachInterrupt(0, isr, RISING);
numPulses = 0; // prime the system to start a new reading
}
Any advice on how to solve or sidestep this startup issue greatly appreciated, thank you.
Sensor connected to D3, Like the one here
Code:
void ICACHE_RAM_ATTR isr()
{
unsigned long now = micros();
float pulselength;
pulseNow = now;
int averagepulse;
int totalsample = 0;
if ((pulseNow - pulseThen) > resolution)
{
Serial.print("numPulses ");
Serial.print(numPulses+1);
Serial.print(" ");
pulselength = (float)(pulseNow - pulseThen) /1000000;
Serial.print(pulselength);
Serial.print("\n");
if( count < sample )
{
keeppulse[count] = pulselength;
count++;
}
else
{
count = 0;
Serial.print("Average in a sample of ");
Serial.print(sample+1);
Serial.print(" is ");
for( int x=0; (x<sample); x++ ) {
totalsample += (int)keeppulse[x];
}
averagepulse = totalsample/sample;
Serial.print(averagepulse);
Serial.print("\n\n");
}
pulseThen = pulseNow;
++numPulses;
}
}
void setup()
{
Serial.begin(19200);
pinMode(3, OUTPUT); //probe output on pin 3
attachInterrupt(0, isr, RISING);
numPulses = 0; // prime the system to start a new reading
}