You shouldn’t have to call motor.monitor();. If you’re not calling Serial.begin(115200); at startup, do it. But I don’t think the monitor functions would work without it either.
If it’s able to keep up with the data flood, that is actually better to be able to see exactly how often you’re getting bad readings. But if it seems to be struggling, try this:
static int32_t lastPrintTime = 0;
int32_t t = millis();
if (t - lastPrintTime >= 50) {
lastPrintTime = t;
// Print stuff here
}
From the little data you’ve got, it looks like you’re getting big spikes rather than steady noise. See if you can find anything that changes the pattern. Fiddle with the wiring, pick up the motor and move closer or farther from potential EMI sources, try a different power source if it’s not too much trouble, different USB port and/or cable, or anything else you can think of.
You could try adding a simple spike filter:
void ReadLinearHalls(int hallA, int hallB, int *a, int *b)
{
int threshold = 50;
static int olderA = *a, olderB = *b, oldA = *a, oldB = *b;
int newA = analogRead(hallA), newB = analogRead(hallB);
*a = abs(newA-oldA) > threshold && abs(newA-olderA) > threshold ? oldA : newA;
*b = abs(newB-oldB) > threshold && abs(newB-olderB) > threshold ? oldB : newB;
olderA = oldA, oldA = newA, olderB = oldB, oldB = newB;
}
Technically those static variables should go in the sensor class, but as long as there’s only one instance of it this will work.
Basically it says if the new reading is more than 50 units different from the previous two, it’s probably a spike so reuse the previous reading. It won’t work if spikes are so frequent that you get two consecutively (likely, if 3 of 11 readings are bad), or if you get spike,good,spike and both spikes are within 50 units of eachother. But since the B972 and B819 spikes in your printout are more than 50 different from eachother, it would still catch both of them.
That's an argument to the constructor which defaults to false. You can either pass in true when creating it or set it before calling init, whichever you prefer.
– dekutree64