Enkodér
Napsal: 15 dub 2021, 10:23
Snád sa to považuje za senzor:D Mám trocha špecifický enkodér a potreboval by som pomôcť s napísaním kódu. Našiel som si tento kód
Ale je písaný na iný typ enkodéru Na tejto stránke je tenkód aj priebehy enkodéru ako by to malo vyzerať https://howtomechatronics.com/tutorials ... e-arduino/
Lenže môj enkodér má takéto priebehy Vedeli by ste poradiť ako ten kód upraviť aby spolupracoval s mojim enkodérom? Pretože teraz to občas opakuje čísla napr takýto výpis z toho dostanem
Position: 53
Position: 54
Position: 55
Position: 56
Position: 57
Position: 56
Position: 57
Position: 56
Position: 57
Position: 58
Position: 57
Position: 58
Position: 57
Position: 58
Kód: Vybrat vše
#define outputA 6
#define outputB 7
int counter = 0;
int aState;
int aLastState;
void setup() {
pinMode (outputA,INPUT);
pinMode (outputB,INPUT);
Serial.begin (9600);
// Reads the initial state of the outputA
aLastState = digitalRead(outputA);
}
void loop() {
aState = digitalRead(outputA); // Reads the "current" state of the outputA
// If the previous and the current state of the outputA are different, that means a Pulse has occured
if (aState != aLastState){
// If the outputB state is different to the outputA state, that means the encoder is rotating clockwise
if (digitalRead(outputB) != aState) {
counter ++;
} else {
counter --;
}
Serial.print("Position: ");
Serial.println(counter);
}
aLastState = aState; // Updates the previous state of the outputA with the current state
}
Lenže môj enkodér má takéto priebehy Vedeli by ste poradiť ako ten kód upraviť aby spolupracoval s mojim enkodérom? Pretože teraz to občas opakuje čísla napr takýto výpis z toho dostanem
Position: 53
Position: 54
Position: 55
Position: 56
Position: 57
Position: 56
Position: 57
Position: 56
Position: 57
Position: 58
Position: 57
Position: 58
Position: 57
Position: 58