I'm trying to calculate the coming RPS from rotary encoder. I am trying to get it by checking the status with digitalRead
. However, I can not get more than 6 RPS. Any solution? I will be obliged for your help.
Following is the code.
float rps;
void setup() {
Serial.begin(9600);
DDRC &= ~_BV (1); // pinMode (A1, INPUT);
}
void loop() {
static int lastReedState;
static unsigned long lastTransition;
int reedState = (PINC & _BV (1)) == 0; // digitalRead (A1);
// On a rising transition of the reed switch:
if (reedState == LOW && lastReedState == HIGH) {
// HIGH LOW measuring the low period
// Compute time since last valid transition.
unsigned long now = micros();
unsigned long revolutionTime = now - lastTransition;
// measuring the High period
// Compute the RPS
rps = 1.00 / (12.00*revolutionTime) *1000000.00;
// 12 is rotary encoders resolution
// Remember this transition.
lastTransition = now;
}
// Remember last state.
lastReedState = reedState;
Serial.println(rps);
}
Serial.println(rps);
. You should at least embed this statement inside something likeif (millis() - lastTimePrinted >= PRINT_PERIOD) { ... }
and not try to print faster than your baud rate (960 bytes/s). – Edgar Bonet Dec 8 '16 at 8:54