Greetings,
I just dove into the world of Arduino a few days ago. I ordered a couple of kits that included an Uno and Mega along with a sack full of accessories.
I am not a programmer so I can't speak the language right now.
I am a ham radio operator and enjoy building stuff for my hobby. I saw a video using a stepper and an Arduino and came up with an idea of a remotely controlled antenna tuner where I would turn the rotary encoder which would turn the stepper and ultimately turn 3 separate knobs on an antenna tuner that I would locate at the base of my antenna tower. I had looked at playing with Ardurinos some time ago but never got around to it until now.
I am using my Mega for this project along with 3 NEMA 17 steppers, 3 rotary encoders and 3 easy driver boards.
I have successfully connected 1 stepper setup thanks to the guys at Brainy bits following this tutorial:
My next step is to connect a 2nd stepper. This is where I am needing help.
In the process of trying to figure it all out, I learned about the attachInterrupt assignment. So, I assigned Pin 20 for the 2nd encoder which you can see in the following code.
I have also defined the easy driver connections for the 2nd driver board.
Am I going in the right direction with this? If so, other than pin definitions for the driver board and rotary controller and the attachInterrupt for the 2nd encoder, I do not know where to go from here. Everything I try does not work. About the best I get is the 2nd stepper only moves one direction.
Thanks for the help.
Kevin
// EasyDriver connections
#define step_pin 9 // Pin 9 connected to Steps pin on EasyDriver
#define dir_pin 8 // Pin 8 connected to Direction pin
#define MS1 10 // Pin 10 connected to MS1 pin
#define MS2 11 // Pin 11 connected to MS2 pin
#define SLEEP 12 // Pin 12 connected to SLEEP pin
// EasyDriver2 connections
#define step_pin2 43 // Pin 43 connected to Steps pin on EasyDriver2
#define dir_pin2 45 // Pin 45 connected to Direction pin
#define MS1_2 41 // Pin 41 connected to MS1 pin
#define MS2_2 39 // Pin 39 connected to MS2 pin
#define SLEEP2 37 // Pin 37 connected to SLEEP pin
volatile boolean TurnDetected; // need volatile for Interrupts
volatile boolean rotationdirection; // CW or CCW rotation
// Rotary Encoder Module connections
const int PinCLK=21; // Generating interrupts using CLK signal
const int PinDT=4; // Reading DT signal
const int PinSW=5; // Reading Push Button switch
// Rotary Encoder2 Module connections
const int PinCLK2=20; // Generating interrupts using CLK signal
const int PinDT2=33; // Reading DT signal
const int PinSW2=31; // Reading Push Button switch
int StepperPosition=0; // To store Stepper Motor Position
int StepsToTake=4; // Controls the speed of the Stepper per Rotary click
int direction; // Variable to set Rotation (CW-CCW) of stepper
// Interrupt routine runs if CLK goes from HIGH to LOW
void rotarydetect () {
delay(4); // delay for Debouncing
if (digitalRead(PinCLK))
rotationdirection= digitalRead(PinDT);
else
rotationdirection= !digitalRead(PinDT);
TurnDetected = true;
}
void setup () {
pinMode(MS1, OUTPUT);
pinMode(MS2, OUTPUT);
pinMode(dir_pin, OUTPUT);
pinMode(step_pin, OUTPUT);
pinMode(SLEEP, OUTPUT);
digitalWrite(SLEEP, HIGH); // Wake up EasyDriver
delay(5); // Wait for EasyDriver wake up
/* Configure type of Steps on EasyDriver:
// MS1 MS2
//
// LOW LOW = Full Step //
// HIGH LOW = Half Step //
// LOW HIGH = A quarter of Step //
// HIGH HIGH = An eighth of Step //
*/
digitalWrite(MS1, LOW); // Configures to Full Steps
digitalWrite(MS2, LOW); // Configures to Full Steps
pinMode(PinCLK,INPUT); // Set Pin to Input
pinMode(PinDT,INPUT);
pinMode(PinSW,INPUT);
digitalWrite(PinSW, HIGH); // Pull-Up resistor for switch
attachInterrupt (2,rotarydetect,FALLING); // interrupt 2 always connected to pin 21 on Arduino UNO
attachInterrupt (3,rotarydetect,FALLING); // interrupt 3 always connected to pin 20 on Arduino UNO
}
void loop () {
if (!(digitalRead(PinSW))) { // check if button is pressed
if (StepperPosition == 0) { // check if button was already pressed
} else {
if (StepperPosition > 0) { // Stepper was moved CW
while (StepperPosition != 0){ // Do until Motor position is back to ZERO
digitalWrite(dir_pin, HIGH); // (HIGH = anti-clockwise / LOW = clockwise)
for (int x = 1; x < StepsToTake; x++) {
digitalWrite(step_pin, HIGH);
delay(1);
digitalWrite(step_pin, LOW);
delay(1);
}
StepperPosition=StepperPosition-StepsToTake;
}
}
else {
while (StepperPosition != 0){
digitalWrite(dir_pin, LOW); // (HIGH = anti-clockwise / LOW = clockwise)
for (int x = 1; x < StepsToTake; x++) {
digitalWrite(step_pin, HIGH);
delay(1);
digitalWrite(step_pin, LOW);
delay(1);
}
StepperPosition=StepperPosition+StepsToTake;
}
}
StepperPosition=0; // Reset position to ZERO after moving motor back
}
}
// Runs if rotation was detected
if (TurnDetected) {
TurnDetected = false; // do NOT repeat IF loop until new rotation detected
// Which direction to move Stepper motor
if (rotationdirection) { // Move motor CCW
digitalWrite(dir_pin, HIGH); // (HIGH = anti-clockwise / LOW = clockwise)
for (int x = 1; x < StepsToTake; x++) {
digitalWrite(step_pin, HIGH);
delay(1);
digitalWrite(step_pin, LOW);
delay(1);
}
StepperPosition=StepperPosition-StepsToTake;
}
if (!rotationdirection) { // Move motor CW
digitalWrite(dir_pin, LOW); // (HIGH = anti-clockwise / LOW = clockwise)
for (int x = 1; x < StepsToTake; x++) {
digitalWrite(step_pin, HIGH);
delay(1);
digitalWrite(step_pin, LOW);
delay(1);
}
StepperPosition=StepperPosition+StepsToTake;
}
}
}