hi
i have written this code to measure the position and the speed of encoder but the values of speed is so large
#define outputA 6
#define outputB 7
unsigned long timeO = 0;
unsigned long timeF = 0;
volatile long pulses = 0;
float encoderspeed = 0;
int counter = 0;
int aState;
int aLastState;
void setup() {
pinMode (outputA, INPUT);
pinMode (outputB, INPUT);
Serial.begin(9600);
aLastState = digitalRead(outputA);
attachInterrupt(digitalPinToInterrupt(outputA), detectPulses, RISING);
timeO = micros();
}
void loop() {
timeF = micros();
aState = digitalRead(outputA);
if (aState != aLastState) {
if (digitalRead(outputB) != aState) {
counter ++;
} else {
counter --;
}
encoderspeed = ((float)pulses/12.0)/((float)(timeF - timeO)/(60000000.0) );
Serial.print("Position: ");
Serial.println(counter);
Serial.print("Speed:");
Serial.println(encoderspeed);
}
aLastState = aState;
pulses = 0;
timeO = micros();
}
void detectPulses() {
if (digitalRead(outputB) == LOW) {
pulses++;
}
else {
pulses--;
}
}
koronus:
hi
i have written this code to measure the position and the speed of encoder but the values of speed is so large
#define outputA 6
#define outputB 7
unsigned long timeO = 0;
unsigned long timeF = 0;
volatile long pulses = 0;
float encoderspeed = 0;
int counter = 0;
int aState;
int aLastState;
void setup() {
pinMode (outputA, INPUT);
pinMode (outputB, INPUT);
Serial.begin(9600);
aLastState = digitalRead(outputA);
attachInterrupt(digitalPinToInterrupt(outputA), detectPulses, RISING);
timeO = micros();
}
void loop() {
timeF = micros();
aState = digitalRead(outputA);
if (aState != aLastState) {
if (digitalRead(outputB) != aState) {
counter ++;
} else {
counter --;
}
encoderspeed = ((float)pulses/12.0)/((float)(timeF - timeO)/(60000000.0) );
Serial.print("Position: ");
Serial.println(counter);
Serial.print("Speed:");
Serial.println(encoderspeed);
}
aLastState = aState;
pulses = 0;
timeO = micros();
}
void detectPulses() {
if (digitalRead(outputB) == LOW) {
pulses++;
}
else {
pulses--;
}
}
the value of the speed is some numbers like 1250000.0 or 874500.0 etc. but I want to show the value of the speed of the encoder something like 250.0 in the maximum value. could you help me please what is wrong in my programming?
If it's just a matter of presentation, divide the result by a given number while printing. That number could be e.g the result of 1250000 / 250.
sterretje:
If it's just a matter of presentation, divide the result by a given number while printing. That number could be e.g the result of 1250000 / 250.
thanks for your answer, but I want to know why this result would be shown? the encoderspeed formula in my code is changing the pulses/microsecond to rev/min and the huge number shows that the pulses which read in one revolute are so many than the resolution of the encoder but why?