2

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?

  • 2
    You need to tell us what "not working" means. If it doesn't do what you want, What does it do? – sachleen Apr 18 '14 at 17:24
  • There are so many potential issues here: oscillating across encoder ticks even when the motor is "not moving" (most designs of that resolution interpret a quadrature encoder to give a reversible count and ignore stationary vibration), integer overflow in a relatively small number of revolutions, lack of allowance for acceleration and deceleration. What type of motor/reduction? Is the encoder directly on the motor or after the reduction? – Chris Stratton Apr 18 '14 at 18:37
  • Thanks for the response, i'm actually simulating this on Proteus, and since it is an ideal model i don't think it has these issues, i'm really skeptical about the code itself, i think there is something wrong about how i'm reading the encoder. – Apastrix Apr 18 '14 at 19:17
  • How do you drive the motor? Directly attached to Arduino or using a motor driver (shield)? – jippie Apr 19 '14 at 8:44
  • I'm simulating this on ISIS, i'm using a mosfet to drive the motor. – Apastrix Apr 19 '14 at 9:42
2

You never tell the motor to stop.

void loop(){
   while(counter< 360*5){
     analogWrite(motor,255);
   }
   analogWrite(motor, 0); //add something like this or whatever could stop it
   //you may want to disable the counter now too
}

However, even with that change you are likely a long way from having a working, or at least reliable system - see other issues in my comment on the question.

  • But i'm telling it to run only if the condition is right, nevertheless i tried it and it isn't working. – Apastrix Apr 18 '14 at 19:29
  • The output statement you are using continues to generate the requested output until countermanded. So if you want to stop doing that, you must tell it to do something else instead. But there are doubtless other problems as well. – Chris Stratton Apr 18 '14 at 19:42
0

Since the Encoder resolution is high, the Arduino Uno misses some pulses hence you could not get the accurate reading from it. You could use the hardware quadrature decoder in Arduine Due board which can easily read the encoder values.

Here is a code which can only be executed in arduino Due to Read one quadrature encoder. How to configure 2nd Quadrature Decoder IO pins in Arduino IDE

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.