i need some guide here for a better understanding on using several function.
i want to build system that measure the value of encoder. with that value, i want to decide the motor will run again or stop right there and wait for other signal (in my case i'm using push button).
it's works perfectly for my first practice using code from intrutables.
but when i want use that code in other declare function (not in void loop), it act weirdly and don't want count straight forward (when i turn clock wise, the counter not always adding sometimes it decrease and then adding again).
#define outputA 4
#define outputB 7
int aState;
int aLastState;
int counter ;
void setup() {
pinMode (outputA,INPUT);
pinMode (outputB,INPUT);
Serial.begin (9600);
// Reads the initial state of the outputA
aLastState = digitalRead(outputA);
}
int encoder(){
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 --;
}
}
aLastState = aState; // Updates the previous state of the outputA with the current state
return counter;
}
void loop() {
int encodervalue = encoder();
Serial.print("Position: ");
Serial.println(encodervalue);
}
You may want to think about using a pin change interrupt to count encoder ticks. They can be quite fast and your polling code spends a lot of time not reading the encoder while doing time-consuming serial operations.
i think i cannot use interupt mode sir because i need to move motor until some value on encoder reached.
my project is automatic storage system.
and i think if i use interrupt mode, it will make my motor run and stop continuously because that interrupt (or maybe i'm wrong??).
but i have some question with your code sir
what this code mean sir?
void EncPHAInterrupt( void ) ?
and this
bFlags |= TICK_OCCURED;
and also this
if( bFlags & TICK_OCCURED )
i need some guidance for this language
thanks for your kindness