#include <digitalWriteFast.h> // gamit ani para mas paspas ang pag toggle
#define encoderA 2
#define encoderB 3
#define uplus 8
#define uminus 9
#define vplus 10
#define vminus 11
#define wplus 12
#define wminus 13
#define PINP A0
int phase = 1;
int sped = 0;
int pulses = 0;
int deg = 0;
int pulsesChanged = 0;
unsigned long previousMillis = 0;
unsigned long previous_print_Millis = 0;
void ccs1() {
digitalWriteFast(uminus, HIGH);
digitalWriteFast(uplus, LOW);
digitalWriteFast(vplus, HIGH);
digitalWriteFast(vminus, LOW);
digitalWriteFast(wminus, HIGH);
digitalWriteFast(wplus, LOW);
}
void ccs2() {
digitalWriteFast(wplus, HIGH);
digitalWriteFast(wminus, LOW);
}
void ccs3() {
digitalWriteFast(vminus, HIGH);
digitalWriteFast(vplus, LOW);
}
void ccs4() {
digitalWriteFast(uplus, HIGH);
digitalWriteFast(uminus, LOW);
}
void ccs5() {
digitalWriteFast(wminus, HIGH);
digitalWriteFast(wplus, LOW);
}
void ccs6() {
digitalWriteFast(vplus, HIGH);
digitalWriteFast(vminus, LOW);
}
void cs1() {
digitalWriteFast(uplus, HIGH);
digitalWriteFast(uminus, LOW);
digitalWriteFast(vplus, HIGH);
digitalWriteFast(vminus, LOW);
digitalWriteFast(wminus, HIGH);
digitalWriteFast(wplus, LOW);
}
void cs2() {
digitalWriteFast(vminus, HIGH);
digitalWriteFast(vplus, LOW);
}
void cs3() {
digitalWriteFast(wplus, HIGH);
digitalWriteFast(wminus, LOW);
}
void cs4() {
digitalWriteFast(uminus, HIGH);
digitalWriteFast(uplus, LOW);
}
void cs5() {
digitalWriteFast(vplus, HIGH);
digitalWriteFast(vminus, LOW);
}
void cs6() {
digitalWriteFast(wminus, HIGH);
digitalWriteFast(wplus, LOW);
}
void stahp() {
digitalWriteFast(uminus, HIGH);
digitalWriteFast(wplus, HIGH);
digitalWriteFast(vplus, HIGH);
digitalWriteFast(vminus, HIGH);
digitalWriteFast(uplus, HIGH);
digitalWriteFast(wminus, HIGH);
}
void A_CHANGE() {
if ( digitalReadFast(encoderB) == 0 ) {
if ( digitalReadFast(encoderA) == 0 ) {
// A fell, B is low
pulses--; // moving reverse
} else {
// A rose, B is low
pulses++; // moving forward
}
} else {
if ( digitalReadFast(encoderA) == 0 ) {
// A fell, B is high
pulses++; // moving forward
} else {
// A rose, B is high
pulses--; // moving reverse
}
}
// Make sure the pulses are between 0 and 359
if (pulses > 359) {
pulses = pulses - 360;
} else if (pulses < 0) {
pulses = pulses + 360;
}
// tell the loop that the pulses have changed
pulsesChanged = 1;
}
void B_CHANGE() {
if ( digitalReadFast(encoderA) == 0 ) {
if ( digitalReadFast(encoderB) == 0 ) {
// B fell, A is low
pulses++; // moving forward
} else {
// B rose, A is low
pulses--; // moving reverse
}
} else {
if ( digitalReadFast(encoderB) == 0 ) {
// B fell, A is high
pulses--; // moving reverse
} else {
// B rose, A is high
pulses++; // moving forward
}
}
// Make sure the pulses are between 0 and 359
if (pulses > 359) {
pulses = pulses - 360;
} else if (pulses < 0) {
pulses = pulses + 360;
}
// tell the loop that the pulses have changed
pulsesChanged = 1;
}
void setup() {
Serial.begin(115200);
pinModeFast(encoderA, INPUT);
pinModeFast(encoderB, INPUT);
pinModeFast(uplus, OUTPUT);
pinModeFast(uminus, OUTPUT);
pinModeFast(wplus, OUTPUT);
pinModeFast(wminus, OUTPUT);
pinModeFast(vplus, OUTPUT);
pinModeFast(vminus, OUTPUT);
pinModeFast(PINP, INPUT);
attachInterrupt(0, A_CHANGE, CHANGE);
attachInterrupt(1, B_CHANGE, CHANGE);
previousMillis = millis();
}
void loop() {
unsigned long currentMillis = millis();
//Serial.println(sped);
if (currentMillis - previousMillis >= sped) {
previousMillis += sped;
if (pulsesChanged != 0) {
pulsesChanged = 0;
Serial.println(pulses);
}
switch (phase) {
case 1:
cs1();
break;
case 2:
cs2();
break;
case 3:
cs3();
break;
case 4:
cs4();
break;
case 5:
cs5();
break;
case 6:
cs6();
break;
}
int z = constrain(analogRead(PINP), 0, 1023);
sped = map(z, 0, 1023, 5, 50);
if (phase < 6) {
phase = phase + 1;;
}
else {
phase = 1;
}
}
}
Is there something i can do such that i can read quadrature encoder vales with my arduino uno without skipping huge values?
Do i need much fastor arduino such that it can catch encoder values without skipping? or i need to use port manipulation?
Please help me.
Result of code above:
202
196
396
5
7
100