Dear all,
I know this section for programming. So far I am struck with the problem that I believe is from coding and from the general layout of my solution as well. That is why I post it here and hope that we folks forgive my such inconsiderate act. Thank you sincerely ahead of time here.
I am working on a project that uses Arduino Pro Mini to collect and display the encoder data. The reason to use Pro Mini instead of regular Arduino (Such as Uno, Leonardo) is that we need smaller electronics to fit in an enclosure.
Components I have:
- +5V optical Quadrature encoder (E2-1024, US Digital)(http://www.usdigital.com/products/encoders/incremental/rotary/kit/E2)
- Logic level converter (https://www.sparkfun.com/products/8745)
- Arduino Pro Mini 328 - 3.3V/8MHz (https://www.sparkfun.com/products/11114)
The reason I use the logic level converter is to convert +5V signal to +3.3V level and then to send it to the Pro Mini since it is powered by +3.3V. The logic level converter itself is powered by +5.5V at HV and by +3.3V at LV. The Quadracture signal from Channel A and Channel B of the encoder connects to two TX0 pins at HV end. And the two TX1 pins connect to the pin 2 and pin 3 at the Pro Mini. The Pro Mini connects to my laptop.
I attach the code here. At the first I would like to see if the idea of the whole project works. Second, the code itself only works when I actually supply the Pro Mini with +5V instead of +3.3V. And however when the code is working, the display in serial monitor shows the frozen data updating if I rotate the knob of encoder a little bit faster. I want to see what is wrong with the code. The code source is from a website and demonstrated to be working great with Arduino Uno.
The code outputs the count ( as pulse) and the angle. The encoder counts 4080 per cycle and I only rotate 180 degree so I use 2040 when I calculate angle.
int pulses, A_SIG=0, B_SIG=1;
void setup(){
attachInterrupt(0, A_RISE, RISING);
attachInterrupt(1, B_RISE, RISING);
Serial.begin(115200);
}//setup
void loop(){
}
void A_RISE(){
detachInterrupt(0);
A_SIG=1;
if(B_SIG==0)
pulses++;//moving forward
if(B_SIG==1)
pulses--;//moving reverse
Serial.println(pulses);
attachInterrupt(0, A_FALL, FALLING);
float angle = float (pulses) * (180.0/2040.0);
Serial.println(angle);
Serial.println ("/");
}
void A_FALL(){
detachInterrupt(0);
A_SIG=0;
if(B_SIG==1)
pulses++;//moving forward
if(B_SIG==0)
pulses--;//moving reverse
Serial.println(pulses);
attachInterrupt(0, A_RISE, RISING);
float angle = float (pulses) * (180.0/2040.0);
Serial.println(angle);
Serial.println ("/");
}
void B_RISE(){
detachInterrupt(1);
B_SIG=1;
if(A_SIG==1)
pulses++;//moving forward
if(A_SIG==0)
pulses--;//moving reverse
Serial.println(pulses);
attachInterrupt(1, B_FALL, FALLING);
float angle = float (pulses) * (180.0/2040.0);
Serial.println(angle);
Serial.println ("/");
}
void B_FALL(){
detachInterrupt(1);
B_SIG=0;
if(A_SIG==0)
pulses++;//moving forward
if(A_SIG==1)
pulses--;//moving reverse
Serial.println(pulses);
attachInterrupt(1, B_RISE, RISING);
float angle = float (pulses) * (180.0/2048.0);
Serial.println(angle);
Serial.println ("/");
}