I'm using an arduino UNO, and i want to turn a motor a set number of revolutions, i'm trying to read the clicks of the encoder which has a resolution of 360 clicks/ revolution, my logic is that i count the number of clicks until i get to the desired number which represents the the number of revolutions * the resolution of the encoder:
1 revolution ==> 360 clicks
5 revolutions ==> 360*5 (desired number)
i'm a beginner with arduino so here is my code:
volatile int counter=0;
int motor=5; // motor is connected to pin 5
void setup(){
pinMode(motor,OUTPUT);
attachInterrupt(0, count,RISING); // attaching encoder on interrupt 0
}
void loop(){
while(counter< 360*5){
analogWrite(motor,255);
}
}
void count(){
counter++;
}
This code is not working at all, how can i fix it?