I managed to fix this by moving the computation to a different part of the code.
New code here, but it doesn't resemble the old code all that much. I am getting some practice in with coding. Still lots to do, including adding the average back when I have time.
EDIT: That code is wrong...
I saved the wrong sketch, oops. Guess I have to re write it...
EDIT:
Here is the code
/*
Test for reading zero cross and Timing
*/
#define REVS A3
#define PHASE A4
#define LPIN 13
// Combined class for RPM and bemf
class Pulse {
public:
uint8_t sPin;
uint8_t pPin;
volatile bool rpmNewIsrFlag;
volatile bool phaseNewIsrFlag;
uint32_t rpmMicros;
uint32_t prevRpmMicros;
volatile uint32_t rpmIsrMicros;
volatile uint32_t phaseIsrMicros;
uint32_t phaseMicros;
float freq;
float degree;
uint32_t rpm;
float timing;
Pulse(uint8_t dSPin, uint8_t dPPin, bool rpmNewIsrFlag, bool phaseNewIsrFlag);
void begin();
void rpmData();
};
Pulse::Pulse(uint8_t dRPin, uint8_t dPPin, bool dRpmNewIsrFlag, bool dPhaseNewIsrFlag) {
sPin = dRPin;
pPin = dPPin;
rpmNewIsrFlag = dRpmNewIsrFlag;
phaseNewIsrFlag = dPhaseNewIsrFlag;
}
void Pulse::begin() {
pinMode(sPin, INPUT);
pinMode(pPin, INPUT);
}
void Pulse::rpmData() {
if (rpmNewIsrFlag == true) { // ISR flag is true, let's do stuff
prevRpmMicros = rpmMicros; // Save previous rpmMicros
noInterrupts(); // Turn off interrupts
rpmMicros = rpmIsrMicros; // set rpmMicros to the ISR interrupt time
rpmNewIsrFlag = false; // ISR flag is false, wait until next time
interrupts(); // Enable interrupts
freq = (1000000 / (rpmMicros - prevRpmMicros)); // Account for micro seconds
rpm = (freq * 60); // Converts from ticks per second to ticks per minute
degree = (((rpmMicros - prevRpmMicros) * 1000) / 360); // Converts frequency into 360 equal degrees
}
if (phaseNewIsrFlag == true) { //ISR flag us true, let's do stuff
noInterrupts(); //Disable interrupts
phaseMicros = phaseIsrMicros; //Set phaseMicros to the ISR event time
phaseNewIsrFlag = false; //Change our ISR flag, guess we wait until its true again
interrupts(); //Enable interrupts
timing = ((((phaseMicros - rpmMicros) * 1000) / degree) - 30);
}
}
Pulse pulseA(A3, A4, false, false);
//Pulse pulseB(3, 4, false, false);
//Pulse pulseC(5, 6, false, false);
uint32_t displayMillis = 0;
uint32_t prevDisplayMillis = 0;
uint8_t led = LOW;
const uint16_t refresh = 1000;
void setup() {
Serial.begin(115200);
pulseA.begin();
pinMode(LPIN, OUTPUT);
attachInterrupt(digitalPinToInterrupt(REVS), rpmAISR, FALLING);
attachInterrupt(digitalPinToInterrupt(PHASE), phaseAISR, FALLING);
prevDisplayMillis = millis();
}
void loop() {
pulseA.rpmData();
displayMillis = millis();
if (displayMillis >= prevDisplayMillis + refresh) {
prevDisplayMillis = millis();
if (led == LOW) {
led = HIGH;
} else {
led = LOW;
}
digitalWrite(LPIN, led);
showData();
}
}
void rpmAISR() {
pulseA.rpmIsrMicros = micros();
pulseA.rpmNewIsrFlag = true;
}
void phaseAISR() {
pulseA.phaseIsrMicros = micros();
pulseA.phaseNewIsrFlag = true;
}
void showData() {
Serial.println();
Serial.println("===============");
Serial.print(" FREQ ");
Serial.print(pulseA.freq);
Serial.print(" Hz");
Serial.print(" RPM ");
Serial.print(pulseA.rpm);
Serial.print(" Timing ");
Serial.print(pulseA.timing);
Serial.print("*");
Serial.println();
}