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.
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?
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.
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.
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...
}
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.