Code here:
/*
Code by Kealan O'Carroll, April 2019
Encoder.h library by PaulStoffregen
http://www.pjrc.com/teensy/td_libs_Encoder.html
Code intended for an arduino nano with the following wiring:
Pins 2, 4 : Scale 1 inputs
Pins 3, 5 : Scale 2 inputs
Arduino Pins 7, 8 : Outputs to DRO A and B pins. (Pins 6 and 8 on a DSUB9 type plug)
*/
#define ENCODER_OPTIMIZE_INTERRUPTS
#include
Encoder scale1(4,2); //Wire first scale A & B quadrature lines to arduino Pins 2 and 4
Encoder scale2(5,3); //Wire second scale A & B quadrature lines to arduino Pins 3 and 5
//If either of the scales reads in the wrong direction, reverse the pin numbers above (e.g. (4,2) changes to (2,4)
long pos2 = 0;
long pos1 = 0;
long delt = 0;
long oldpos1 = 0;
long oldpos2 = 0;
long delt1 = 0;
long delt2 = 0;
bool outA = LOW;
bool outB = LOW;
void setup() {
DDRD = DDRD | B11000000;
DDRB = DDRB | B000001;
PORTD = B00000000; // sets digital pins all Low
PORTB = B00000000; // sets digital pins all Low
}
void loop() {
pos1 = scale1.read();
pos2 = scale2.read();
delt2 = pos2 – oldpos2;
delt1 = pos1 – oldpos1;
delt = delt2 – delt1;
while (delt > 0) {
if (outA == LOW && outB == LOW) { //if 8 is low and 7 is low
PORTD = B10000000; //push 7 high
PORTB = B000000; //leave 8 low
outB = HIGH;
delt–;
}
else if (outA == LOW && outB == HIGH) { //if 8 is low and 7 is high
PORTD = B10000000; //leave 7 high
PORTB = B000001; //Push 8 high
outA = HIGH;
delt–;
}
else if (outA == HIGH && outB == HIGH) { //if 8 is high and 7 is high
PORTD = B00000000; //push 7 low
PORTB = B000001; //Leave 8 high
outB = LOW;
delt–;
}
else if (outA == HIGH && outB == LOW) { //if 8 is HIGH and 7 is LOW;
PORTD = B00000000; //leave 7 LOW;
PORTB = B000000; //push 8 low
outA = LOW;
delt–;
}
}
while (delt < 0) {
if (outA == LOW && outB == LOW) { //if both are low,
PORTD = B00000000; //leave 7 low;
PORTB = B000001; //Push 8 high
outA = HIGH;
delt++;
}
else if (outA == LOW && outB == HIGH) { //if 8 is low and 7 is high;
PORTD = B00000000; //push 7 low
PORTB = B000000; //Leave 8 LOW
outB = LOW;
delt++;
}
else if (outA == HIGH && outB == LOW) { //if 8 is high and 7 is low;
PORTD = B10000000; //push 7 high
PORTB = B000001; //Leave 8 HIGH
outB = HIGH;
delt++;
}
else if (outA == HIGH && outB == HIGH) { //if 8 is high and 7 is high;
PORTD = B10000000; //leave 7 high;
PORTB = B000000; //Push 8 LOW
outA = LOW;
delt++;
}
}
oldpos1 = pos1;
oldpos2 = pos2;
}
Edited By Kealan O’Carroll on 26/04/2019 17:44:17