Hello everyone who reads this.
I've got a fairly precise industrial encoder with high resolution. After a little dinking around I got the Arduino to read it wonderfully, now I'm trying to make use of it but have hit a brick wall. I need to send out the encoder position (possibly to another Arduino, maybe my computer or a display), and the only way I can figure is with SerialPrint() - however when I invoke SerialPrint() it seems all other functions stop during this period (which is rather lengthly in the microprocessor world) - which means it's not checking for pulses in that time and thus loosing many positions.
Attached is code that works okay, but it spits out position every second, rather than every pulse (like I tried at first).
Right now I'm thinking maybe try to control a digital potentometer and have my secondary microcontroller read the digital pot? I dunno.
If anyone has any ideas on how to send a number out without using SerialPrint(), I'd greatly appreciate them.
Thanks!
/*Designed for AutomationDirect's TRD-S/SH1000 VD Encoder (1000 pulses per revolution)
0V (blue wire) goes to GND
5V (brown wire) goes to 5V
A+ (black wire) goes to Arduino Digital 2
B+ (white wire) goes to Arduino Digital 4
everything in loop() is just for verification.
This setup should be accurate to roughly 0.3 degrees.
*/
volatile double c = 0;
volatile double pos = 0;
#define A 2
#define B 4
void setup(){
pinMode(A, INPUT);
digitalWrite(A,HIGH); // internal pull up resistor
pinMode(B, INPUT);
digitalWrite(B,HIGH); // internal pull up resistor
attachInterrupt(0,position,CHANGE);
Serial.begin(9600);
}
void loop(){
delay(1000);
Serial.println(pos);
}
void position(){
if (digitalRead(A)==LOW && digitalRead(B)==HIGH){
c=c+1;
}
else if (digitalRead(A)==LOW && digitalRead(B)==LOW){
c=c-1;
}
else if (digitalRead(A)==HIGH && digitalRead(B)==HIGH){
c=c-1;
}
else {
c=c+1;
}
pos=((c/2000)*360); // divide count, multiply by 360 to get degrees.
}