Hello all,
This is my first post so please forgive me if I do not get the proper etiquette right for this forum.
I am working on a goto system for my dobsonian telescope. I have two encoders, one for Azimuth and one for Elevation. My code is far from complete but I am currently experimenting with getting my stepper motors to be closed looped. I currently only have the Azimuth encoder and Stepper motor wired up so some of the code is missing for the Elevation axis.
For my current setup I have everything on my workbench not mechanically connected, just wired up.
For my test I have an initial Azimuth position demand set for 90 degrees in the setup as:
long AziPositiondemand = 90;
I then have the Error calculated from the position demand and the encoder position:
Error = (Azi.read()*0.15)-AziPositiondemand;
Next I have the stepper motor move the amount of steps needed to correct the error. When I power the Arduino on the Stepper will move the calculated steps then stop. If I had it setup correctly it should continue to move until the error is zero but it doesn’t. It only moves the initial calculated amount. If I turn the encoder it will recalculate and move that amount. I want the stepper to keeping turning until the encoder is at the demand position/Error = 0.
(For clarity the encoder and stepper are not connected so its impossible for it to reach the demand position. They are just sitting next to each on the bench)
Thank you for any help,
Zak
#include <AccelStepper.h>
#include <MultiStepper.h>
#include <LiquidCrystal.h> // includes the LiquidCrystal Library
#include <Encoder.h>
// Change these pin numbers to the pins connected to your encoder.
// Best Performance: both pins have interrupt capability
// Good Performance: only the first pin has interrupt capability
// Low Performance: neither pin has interrupt capability
// Two phase 4 frequency doubling to 2400 pulses
// 2400 * 0.15 = 360
const char degree = char (223);
Encoder Azi(18, 19);
Encoder Elev(20, 21);
// avoid using pins with LEDs attached
// Define pin connections
const int dirPin = 22;
const int stepPin = 24;
// Define motor interface type
#define motorInterfaceType 1
// Creates an instance
AccelStepper myStepper(motorInterfaceType, stepPin, dirPin);
LiquidCrystal lcd(1, 2, 4, 5, 6, 7); // Creates an LC object. Parameters: (rs, enable, d4, d5, d6, d7)
const short setZero = 10;
void setup() {
pinMode(setZero, INPUT_PULLUP);
digitalWrite(setZero, LOW);
lcd.begin(16,2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display }
// Declare pins as Outputs
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
// set the maximum speed, acceleration factor,
// initial speed and the target position
myStepper.setMaxSpeed(10000);
myStepper.setAcceleration(500);
myStepper.setSpeed(2000);
}
void loop() {
long AziPositiondemand = 90;
long Error = 0;
Error = (Azi.read()*0.15)-AziPositiondemand;
if (Azi.read() >= 2400.0) { //If you rotate past +360 Degs it will reset to zero
Azi.write(0);
}
if (Azi.read() <= -2400.0) { //If you rotate past -360 Degs it will reset to zero
Azi.write(0);
}
if (Elev.read() >= 2400.0) { //If you rotate past +360 Degs it will reset to zero
Elev.write(0);
}
if (Elev.read() <= -2400.0) { //If you rotate past -360 Degs it will reset to zero
Elev.write(0);
}
{
lcd.setCursor(0,0); // Sets the location at which subsequent text written to the LCD will be displayed
lcd.print(“Azimuth=”); // Prints “Azimuth” on the LCD
lcd.setCursor(9,0);
lcd.print(Azi.read()0.15); //0.15 conversion factor of encoder pulses to degrees
lcd.setCursor(15,0);
lcd.print(degree);
lcd.setCursor(0,1);
lcd.print(“Elevation=”);
lcd.print(Elev.read()0.15); //0.15 conversion factor of encoder pulses to degrees
lcd.setCursor(15,1);
lcd.print(degree);
}
if (digitalRead(setZero) == HIGH){ //Pushbutton that Zeros Azimuth
Azi.write(0);
}
if (Error == 0) { //This section reads the Error between the encoder and the Demand and moves stepper to eliminate error
myStepper.stop();}
if (Error > 0) {
myStepper.moveTo(-Error1.11); // 1.11 is conversion factor for degrees to steps
myStepper.run();
}
if (Error < 0) {
myStepper.moveTo(Error1.11); //1.11 is conversion factor for degrees to steps
myStepper.run();
}
}
Digital_Setting_Circle_Code_with_LCD_and_Stepper_Azimuth_test.i.ino (3.1 KB)