Hello,
I am having trouble reading a basic rotary encoder and cannot find anything on the Web.
I need to rotate a stepper motor in a 1:1 ratio with some sort of knob and have chosen a 20 detent rotary encoder because I have tried a 10k potentiometer (as in https://www.arduino.cc/en/Tutorial/MotorKnob ) and have failed. The pot was a Bourns 82A1AC28BA0728 (https://www.allelectronics.com/item/ptw-10k/10k-bourns-pot-pre-wired/1.html)
I am using an Uno for testing but plan to ultimately put it on a Pro Mini (5v, 16MHz).
I have tried using it with Paul Stoffregen’s Encoder library and it did nothing, as opposed to rotating the stepper motor.
#include <Stepper.h>
#include <Encoder.h>
#define STEPS 200
int myStep;
int previous = 0;
int myFactor = 10; //# of steppper steps to one encoder step
int encoderSteps = 20; //# of steps in encoder
int lethalDistance = 40; //distance in encoder steps that is "too far"
int overturnCounter = 0; //counter to prevent more than two rotations
//that would rip the coax.
Stepper stepper(STEPS, 8, 9, 10, 11);
Encoder encoder(2, 3);
void setup() {
// put your setup code here, to run once:
stepper.setSpeed(90); //slow down once working right
}
void loop() {
// put your main code here, to run repeatedly:
myStep = encoder.read();
if(myStep =! previous){
stepper.step(myStep - previous);
overturnCounter = myStep;
previous = myStep;
}
if(overturnCounter >= lethalDistance){
stepper.step(0 - STEPS);
previous = previous - STEPS;
}
if(overturnCounter <= (0 - lethalDistance)){
stepper.step(STEPS);
previous = previous + STEPS;
}
}
Since that failed, I looked at the disc that came with the encoder. The encoder I have is one with a push button that came in a RobotLinking 37 sensor kit (https://www.ebay.com/i/222173477724?chn=ps&dispItem=1, I don't know where else to find it) and has 5 pins, namely GND, VCC, CLK, DT, and SW. I have wired it according to the instructions: GND-GND VCC-5V CLK-2 DT-3 SW-4.
const int interruptA = 0; // Interrupt 0
const int interruptB = 1; // Interrupt 1
int CLK = 2; // PIN2
int DAT = 3; // PIN3
int BUTTON = 4; // PIN4
int LED1 = 5; // PIN5
int LED2 = 6; // PIN6
int COUNT = 0;
void setup()
{
attachInterrupt(interruptA, RoteStateChanged, FALLING);
// attachInterrupt(interruptB, buttonState, FALLING);
pinMode(CLK, INPUT);
digitalWrite(2, HIGH); // Pull High Restance
pinMode(DAT, INPUT);
digitalWrite(3, HIGH); // Pull High Restance
pinMode(BUTTON, INPUT);
digitalWrite(4, HIGH); // Pull High Restance
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
Serial.begin(9600);
}
void loop()
{
if (!(digitalRead(BUTTON)))
{
COUNT = 0;
Serial.println("STOP COUNT = 0");
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
delay (2000);
}
Serial.println(COUNT);
}
//-------------------------------------------
void RoteStateChanged() //When CLK FALLING READ DAT
{
if (digitalRead(DAT)) // When DAT = HIGH IS FORWARD
{
COUNT++;
digitalWrite(LED1, HIGH);
digitalWrite(LED2, LOW);
delay(20);
}
else // When DAT = LOW IS BackRote
{
COUNT--;
digitalWrite(LED2, HIGH);
digitalWrite(LED1, LOW);
delay(20);
}
}
It compiles, but returns a solid string of “COUNT = 0” to the Serial Monitor. COUNT was supposed to change with the rotation of the encoder.
Does anybody see what I am doing wrong or a better way to do this?
Or, do I simply need a different rotary encoder?