-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncoder.cpp
More file actions
51 lines (40 loc) · 998 Bytes
/
Encoder.cpp
File metadata and controls
51 lines (40 loc) · 998 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "Encoder.h"
Encoder::Encoder(unsigned int pin1, unsigned int pin2):CONTADOR_PIN(pin1),SENTIDO_PIN(pin2){
numPulsos=0;
}
void Encoder::config(){
pinMode(CONTADOR_PIN,INPUT);
pinMode(SENTIDO_PIN,INPUT);
}
void Encoder::lerPulso(){
//Serial.println("teste");
if(pinEdgeHigh()){
if(digitalRead(SENTIDO_PIN))
numPulsos++;
else
numPulsos--;
}
}
bool Encoder::pinEdgeHigh(){
static int valPin=0;
static int valPinLast=0;
valPin = digitalRead(CONTADOR_PIN);
if(valPin != valPinLast){
if(valPin == HIGH){
valPinLast= valPin;
return true;
}
}
valPinLast= valPin;
return false;
}
float Encoder::calculaVelocidade(){
const long pulsosAtuais = getNumPulsos();
const long tempoAtual = millis();
long deltaS = pulsosAtuais - numPulsosAnterior;
long deltaT = tempoAtual - tempoAnterior;
float velocidade = deltaS/((float)deltaT);
tempoAnterior = tempoAtual;
numPulsosAnterior = pulsosAtuais;
return velocidade;
}