Hello, I am working on a "pan & tilt" project controlled by a joystick.
The pan motor is a 28BYJ-48 w/ ULN2003 driver
The tilt is a servo GS90
I am not able to get the stepper to work with the servo and I m using IDE 1.8.12
There appear to be a conflict on the timing between these to, independently they work perfectly but when I combine them they both move together.
I have read online about timer conflicts and tried ServoTimer2 but that would compile for the UNO with this IDE so I need some help.
It would be fine if it would pan then tilt but simultaneously is best.
My Code is below and Thanks in advance, Steve
A special thanks to the moderator for the coaching on posting code!
/* Control Stepper Motor with ULN2003 and Analog Joy Stick for Pan
GS90 Servo Control for the Tilt Axis
This code is in the public domain...
*/
#include <Servo.h>
Servo servoTilt; // Assign the name of the Tilt servo
#include<MultiStepper.h>
#include <AccelStepper.h> // AccelStepper library by Mike McCauley : https://www.arduinolibraries.info/libraries/accel-stepper
#define StepperDriverType 4 // 4 Wires Stepper Motor interface
// Arduino Pins 8,10,9,11 connected to OUT 1,2,3,4 on ULN2003 BE CAREFUL HERE!
AccelStepper stepper = AccelStepper(StepperDriverType, 8,10,9, 11);
#define Pan_Stick_Pin A0 // Arduino A0 Pin connected to the analog out of the Stick to Pan
#define Tilt_Stick_Pin A1 // Arduino A1 Pin connected to the analog out of the Stick to Tilt
int Tilt_Stick_Value; //Create an integer variable for the joy stick reading in the Tilt_Stick
int Pan_Stick_Value; // Used to save the current Analog Pan_Stick Value
int Stepper_Speed; // Used to set the travel speed of the stepper motor
int Stepper_Direction; // Used to choose the direction of travel
int motorSpeed = 1000; // High speeds (800 and above) may cause erratic behavior in 28BYJ-48
int motorAccel = 500; // As above: better avoiding extreme accelerations
void setup()
{
Serial.begin(9600); // Only used for debugging
servoTilt.attach(5); //Assign pin 5 as the output for the Tilt Servo
stepper.setMaxSpeed(motorSpeed);
stepper.setSpeed(motorSpeed);
stepper.setAcceleration(motorAccel);
// the following two lines reset "step zero" to the current position
stepper.setCurrentPosition(stepper.currentPosition());
stepper.runToPosition();
}
void loop()
{
Pan_Stick_Value=analogRead(Pan_Stick_Pin); // Read the value of the analog Pan_Stick
// Use the next line to determine the Pan_Sticks center position in this case 526
//Serial.print("Pan ");
//Serial.println(Pan_Stick_Value);
if (Pan_Stick_Value > 550) // If Pan_Stick is moved to the Right
{
// Map the Right value of the Pan_Stick to a Speed value for the stepper
Stepper_Speed=map(Pan_Stick_Value,550,1023,1,200);
Stepper_Direction--; // Decrease the position to reach by one which will be used to move the stepper later
}
else if (Pan_Stick_Value < 500) // If Pan_Stick is moved to the Left
{
// Map the Left value of the Pan_Stick to a Speed value for the stepper
Stepper_Speed=map(Pan_Stick_Value,500,0,1,200);
Stepper_Direction++; // Increase the position to reach by one which will be used to move the stepper later
}
else
{
Stepper_Speed=0; // If Pan_Stick is around the middle then don't move the stepper by setting the speed to zero
stepper.disableOutputs(); // Turn OFF ULN2003
}
// Move the stepper to new position
stepper.moveTo(Stepper_Direction);
// Serial.print("Stepper ");
// Serial.println(Stepper_Direction);
stepper.setSpeed(Stepper_Speed);
stepper.setMaxSpeed(800);
// Do this until the stepper as reached the new destination
while (stepper.distanceToGo() != 0) // if stepper hasn't reached new position
{
stepper.enableOutputs(); // Turn ON ULN2003
stepper.runSpeedToPosition(); // move the stepper until new position reached
}
//*************************************************************************************
// Read the value of analog input A1 and store it it the valJoyTilt variable
Tilt_Stick_Value = analogRead(Tilt_Stick_Pin);
//scale the Tilt_Stick_Value varible from 0-1023 counts to 0-180 deg.
Tilt_Stick_Value = map(Tilt_Stick_Value, 0, 1023, 0, 180);
//Serial.print("Tilt ");
//Serial.println(Tilt_Stick_Value);
servoTilt.write(Tilt_Stick_Value); // Write the newly mapped Tilt_Stick_Value value to the servo output
delay(1); // Delay 1 milliseconds to allow communication
}