Pages: [1]   Go Down
Author Topic: Calculating Pulse from encoder to Ard. Duemilanove  (Read 1343 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 17
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

[size=12]May I know how to do simple program for Arduino Duemilanove to count pulse from encoder and display the result in Serial Monitor. The encoder that we are using has 12 pulse for one rotation.

I already tried the example program that has been given by Arduino website http://arduino.cc/en/Tutorial/ButtonStateChange is this suitable program for this case?[/size]
« Last Edit: January 13, 2011, 12:39:42 pm by zolofy » Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 112
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is this encoder a hardware closure encoder? If it is a digital pulse then not really.
Logged

No trees were harmed by the transmission of this message. However a few million electrons were temporarily inconvenienced.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 17
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

this is my encoder http://www.pololu.com/catalog/product/1217
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 112
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Depends on what you need to display, do you want to display the amount of pulses per second for example? Or the speed? Or simply output a string on serial monitor on every pulse? I can think of a few approaches for this depending on your end result. What is it you need to know?
Logged

No trees were harmed by the transmission of this message. However a few million electrons were temporarily inconvenienced.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 17
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I want to know the pulse that has been generated by the encoder thank you
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 599
Posts: 48538
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I want to know the pulse that has been generated by the encoder
You want to know that a pulse was generated, or when the pulse was generated, or how long the pulse was? Or, all of the above?

What do you want to do when a pulse arrives?

One does not calculate a pulse. The encoder generates a pulse. It is necessary to detect that a pulse has occurred, and to do something with that simple fact.

What you want to do with that fact has a lot to do with how you detect that pulse, and with what else your Arduino is doing.

You need to provide a lot more details.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 17
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK what I need here is a simple program for my microcontroller that can read the pulse generated by the encoder. The encoder will generate 12 pulse per rotation, and this is the formula that I need to include in this program 0.43x inches (x=pulse), because the main objective of the robot is to measure the distance.
« Last Edit: January 14, 2011, 03:13:59 pm by zolofy » Logged

Knoxville TN USA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 81
Arduino rocks, teach me
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can he not just use PulseIn()? Seriously don't know, just trying to be helpful.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 599
Posts: 48538
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So, now we know that you want to simply detect that a change has occurred. We don't know what else the robot is doing, or how critical it is to not miss a pulse.

If missing pulses is terrible, and the interrupt pins are available, the using an interrupt handler that is triggered when the pulse occurs is possible. If the interrupt pins are not available, the Arduino is not terribly busy, and missing a pulse isn't critical, then polling the encoder pin is generally good enough.

If the pulse width is high relative to the polling frequency, then interrupts are not required.

Let's assume that they are not. In that case, the encoder is just a switch.

int state = digitalRead(encoderPin);
if(state == HIGH)
{
   // encoder sent a pulse. Do something...
}
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 17
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

is this program suitable for my case? actually this is my encoder http://www.pololu.com/catalog/product/1217

Code:
/* Read Quadrature Encoder
  * Connect Encoder to Pins encoder0PinA, encoder0PinB, and +5V.
  *
  * Sketch by max wolf / www.meso.net
  * v. 0.1 - very basic functions - mw 20061220
  *
  */  


 int val;
 int encoder0PinA = 3;
 int encoder0PinB = 4;
 int encoder0Pos = 0;
 int encoder0PinALast = LOW;
 int n = LOW;

 void setup() {
   pinMode (encoder0PinA,INPUT);
   pinMode (encoder0PinB,INPUT);
   Serial.begin (9600);
 }

 void loop() {
   n = digitalRead(encoder0PinA);
   if ((encoder0PinALast == LOW) && (n == HIGH)) {
     if (digitalRead(encoder0PinB) == LOW) {
       encoder0Pos--;
     } else {
       encoder0Pos++;
     }
     Serial.print (encoder0Pos);
     Serial.print ("/");
   }
   encoder0PinALast = n;
 }
« Last Edit: January 14, 2011, 03:31:14 pm by zolofy » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 599
Posts: 48538
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
is this program suitable for my case?
Possibly. It depends on the RPM of the axle.

In the real application, you won't be outputting to the serial port every time the encoder position changes, so then it will work for fairly high RPM rates.

Increasing the baud rate would cause shorter outage windows while serial data is being transmitted.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 17
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok thank you very much
Logged

Pages: [1]   Go Up
Jump to: