3dprinter:
Well, not exactly for what you ask, but this is the times4 multiplier code.
const int InputPin = 2 ;
const int OutputPin = 7 ;
void setup() {
pinMode(OutputPin,OUTPUT) ;
pinMode(InputPin,INPUT) ;
attachInterrupt(digitalPinToInterrupt(InputPin), LevelDetector, RISING) ;
}
volatile unsigned long InputInterval ;
unsigned long LastEdge ;
void LevelDetector() {
InputInterval = micros() - LastEdge ;
LastEdge = micros() ;
}
unsigned long Output2Interval ;
unsigned long OutputTimer ;
unsigned long AverageTimer ;
void loop() {
if ( micros() - OutputTimer > Output2Interval ) {
digitalWrite(OutputPin,digitalRead(OutputPin)==HIGH?LOW:HIGH) ;
OutputTimer = micros() ;
}
if ( millis() - AverageTimer > 1000UL ) {
// Running average missing
noInterrupts() ; Output2Interval = InputInterval / 8 ; interrupts() ;
AverageTimer = millis() ;
}
}
Tested. Attached are two waveforms from the logic analyzer - top is input bottom is output. Note that the output changes a bit after the input - due to the once-a-second update you asked for.
A "simple, yet powerfull" :roll_eyes: averaging code, that does not need a circular buffer is
Average = Average0.9+Input0.1 ;
This gives an exponential curve following the input. Adjust the 0.9 and 0.1 to, f.ex. 0.8 0.2 for a faster average, and so on.
Thank you! Does this work also if the pulse high period is much shorter then the low period?
I did a lot of testing over the weekend and a lot of "finished" code for RPM is just giving wrong values if the RPM is getting higher or do not fit at all.
I decided now to split this out of the main code and use a separate teensy device to do the RPM stuff and transfer this than through wire to the CAN NMEA2K bus connected teensy. This will solve some problems.
Still, I have the problem to get a functional code to get the RPM based on the pulse frequency period, special when the pulse high time is much shorter than the low time. I created different using interrupts, all of them created wrong values at low or high RPM.