I am currently doing a project with a stepper and an Arduino. I need an R4 for the 14 bit Analog resolution. I started with a R3 before my R4 Wifi came in and plugged everything in the same way. Now my encoder does not send signals on the Digital Pin and will not spin the stepper. If I plug the R3 back in it works perfectly. My full code is 2500 lines long and alot of it has nothing to do with the encoder. So, I have attached a bit that I used for initial testing of what is not non-functioning.
#define STEPS 400
volatile boolean TurnDetected;
volatile boolean rotationdirection;
const int PinCLK = 2;
const int PinDT = 6;
const int PinSW = 13;
const int stepPin = 8;
const int dirPin = 7;
const int enPin = 9;
int RotaryPosition = 0;
int PrevPosition;
int StepsToTake;
void isr() {
delay(4);
if (digitalRead(PinCLK)) {
rotationdirection = digitalRead(PinDT);
} else {
rotationdirection = !digitalRead(PinDT);
}
TurnDetected = true;
}
void setup() {
pinMode(PinCLK, INPUT);
pinMode(PinDT, INPUT);
pinMode(PinSW, INPUT);
digitalWrite(PinSW, HIGH);
attachInterrupt(0, isr, FALLING);
}
void loop() {
if (!(digitalRead(PinSW))) {
if (RotaryPosition == 0) {
} else {
StepsToTake = RotaryPosition;
digitalWrite(dirPin, HIGH);
for (int x = 0; x < StepsToTake; x++) {
digitalWrite(stepPin, HIGH);
delay(1);
digitalWrite(stepPin, LOW);
delay(1);
}
RotaryPosition = 0;
}
}
if (TurnDetected) {
PrevPosition = RotaryPosition;
if (rotationdirection) {
RotaryPosition = RotaryPosition - 1;
} else {
RotaryPosition = RotaryPosition + 1;
}
}
TurnDetected = false;
if ((PrevPosition + 1) == RotaryPosition) {
StepsToTake = 40;
digitalWrite(dirPin, HIGH);
for (int x = 0; x < StepsToTake; x++) {
digitalWrite(stepPin, HIGH);
delay(1);
digitalWrite(stepPin, LOW);
delay(1);
}
}
if ((RotaryPosition + 1) == PrevPosition) {
StepsToTake = 40;
digitalWrite(dirPin, LOW);
for (int x = 0; x < StepsToTake; x++) {
digitalWrite(stepPin, HIGH);
delay(1);
digitalWrite(stepPin, LOW);
delay(1);
}
}
}