void setup() {
attachInterrupt(digitalPinToInterrupt(3),updatePosition,CHANGE);
pinMode(N3, OUTPUT);
pinMode(N4, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(A0, INPUT);
pinMode(pin_A_encodeur, INPUT);
Serial.begin(9600);
}
void loop() {
valeurLu = analogRead(potPin);
valeur_a_ecrire = (255.0/1023.0)*valeurLu;
digitalWrite(N4, HIGH);
digitalWrite(N3, LOW);
analogWrite(ENB, valeur_a_ecrire);
etat_courant_encodeur = digitalRead(pin_A_encodeur);
Serial.print(valeur_a_ecrire);
Serial.print(" ");
Serial.println(etat_courant_encodeur);
}
arduino
2 Answers
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our
Terms of Service.
Since you are only reading pin A
https://stackoverflow.com/questions/45363634/calculate-rpm-with-a-quadrature-rotary-encoder 1/5
06/05/2019 arduino - Calculate RPM with a quadrature rotary encoder - Stack Overflow
//...
#define SAMPLE_DELAY (1000) // thi
// adj
#edfine PULSES_PER_TURN (32) // 32
unsigned int pulseCount;
bool lastState;
unsigned int lastTime;
float rpm; // spee
point.
void setup()
{
// ...
pinMode(pin_A_encodeur, INPUT);
lastState = digitalRead(pin_A_enc
}
void loop()
{
bool curState = digitalRead(pin_A
if (curState != lastState)
{
++pulseCount;
lastState = curState;
}
//...
#define SAMPLE_DELAY (1000) // thi
// adj
#define PULSES_PER_TURN (32) // 32
volatile unsigned int pulseCount; //
unsigned int lastTime;
By using our site, you acknowledge
float rpm; that you have read and understand our Cookie Policy, Privacy Policy, and our
// spee
point.
Terms of Service.
https://stackoverflow.com/questions/45363634/calculate-rpm-with-a-quadrature-rotary-encoder 2/5
06/05/2019 arduino - Calculate RPM with a quadrature rotary encoder - Stack Overflow
void setup() {
// ...
pinMode(pin_A_encodeur, INPUT);
lastState = digitalRead(pin_A_enc
attachInterrupt(digitalPinToInter
}
void loop() {
if ((unsigned int)millis() - last
{
unsigned int pulses;
noInterrupts();
pulses = pulseCount; // the
pulseCount = 0; // to
interrupts(); // re-
// 600
rpm = (pulses * (60000.f / (
PULSES_PER_TURN;
lastTime = (unsigned int)mil
}
//...
}
void onPulse() {
++pulseCount;
}
clear;
clc;
close all;
a = arduino('COM5','Uno')
writeDigitalPin(a, 'D7', 1), writ
writePWMDutyCycle(a,'D9',0.7)
startTime = datetime('now');
try
while(1)
t = datetime('now') - st
function [] = counterVmatlab(signal_A
count = 0;
previousState = 0;
angularPos = 0;
ppr = 32; % encoder resolution, 3
(see encoder's data sheet)
gearRatio = 100;
results = fopen('resultsVmatlab.t
for i = 1:length(signal_A)
presentState = signal_A(i);
By using our site, you acknowledge that you ~=
if presentState have read and understand our Cookie Policy, Privacy Policy, and our
previousSt
Terms of Service. count = count + 1;
previousState = presentSt
https://stackoverflow.com/questions/45363634/calculate-rpm-with-a-quadrature-rotary-encoder 4/5
06/05/2019 arduino - Calculate RPM with a quadrature rotary encoder - Stack Overflow
fprintf(results,'%f\t%f\t
at what time did a change in state oc
angularPos = angularPos +
end
end
fclose(results);
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our
Terms of Service.
https://stackoverflow.com/questions/45363634/calculate-rpm-with-a-quadrature-rotary-encoder 5/5