Hello! I am a beginner when it comes to arduino. I'm trying to have two joysticks move two stepper motors (left/right, and up/down). However, When testing it out, the stepper motor just buzzes when it's on.
Here is my frizting photo (I am new, sorry it is sloppy). FYI: for A11, it was suppose to be for VRY, not SW.
#include <Stepper.h>
const int stepsPerRevolution = 2048; // Number of steps per revolution for your stepper motor
const int rolePerMinute = 15;
const int X_pin = 8; // Analog pin connected to X output
const int Y_pin = 11; // Analog pin connected to Y output
// Declare two separate stepper motor objects
Stepper X_stepper(stepsPerRevolution, 22, 24, 26, 28); // X-axis motor
Stepper Y_stepper(stepsPerRevolution, 31, 33, 35, 37); // Y-axis motor (change pins as needed)
void setup() {
X_stepper.setSpeed(rolePerMinute); // Set speed for X-axis motor
Y_stepper.setSpeed(rolePerMinute); // Set speed for Y-axis motor
Serial.begin(9600);
}
void loop() {
// X-axis control
Serial.print("X-axis: ");
int a = analogRead(X_pin);
Serial.println(a);
if (a > 400 && a < 520) { // Neutral zone
// Stop the X motor
for (int i = 2; i < 6; i++) {
digitalWrite(i, LOW);
}
} else if (a < 400) { // Move backward (left)
X_stepper.setSpeed(rolePerMinute);
X_stepper.step(-30);
} else if (a > 530) { // Move forward (right)
X_stepper.setSpeed(rolePerMinute);
X_stepper.step(30);
}
// Y-axis control
Serial.print("Y-axis: ");
int b = analogRead(Y_pin);
Serial.println(b);
if (b > 400 && b < 520) { // Neutral zone
// Stop the Y motor
for (int i = 6; i < 10; i++) {
digitalWrite(i, LOW);
}
} else if (b < 400) { // Move down
Y_stepper.setSpeed(rolePerMinute);
Y_stepper.step(-30);
} else if (b > 530) { // Move up
Y_stepper.setSpeed(rolePerMinute);
Y_stepper.step(30);
}
}
More important to debugging your project, what are the values you are seeing on the serial monitor for your two joysticks? Do they change as you move the joysticks? Do they fit the ranges you have coded in your program?
Yes they fit into the ranges I programmed with. For example, right now the x joystick is at 504. When I slide it to the left, it decrease and goes down to 0 if I push it all the way. When I push it to the right, the max is 1015.
Well, we don;t know what stepper motors you are using, so how are you powering them? A single tiny stepper could be powered by the 5 volts from an Arduino, but two certainly could not be.
Can I suggest you leave your code for a while and run one of the example codes in the stepper library.
Just a simple code to make sure your hardware is good.
Can you post some images of your project?
So we can see your component layout.
Hey, it worked but, it is like stopping and going (I dont know if that makes sense). I did test it with this single code and it worked smoothly. Here is the basic code:
#include <Stepper.h>
const int stepsPerRevolution = 2048; // change this to fit the number of steps per revolution
const int rolePerMinute = 10;
const int X_pin = 8; // analog pin connected to X output
Stepper myStepper(stepsPerRevolution, 22, 26, 24, 28);
void setup() {
myStepper.setSpeed(rolePerMinute);
//Serial.begin(9600);
}
void loop() {
// Serial.print("X-axis: ");
// Serial.println(analogRead(X_pin));
int a = analogRead(X_pin);
if (a > 400 && a < 520)
{
for (int i = 2; i < 6; i++)
{
digitalWrite(i, LOW);
}
}
else if (a < 400)
{
myStepper.setSpeed(rolePerMinute);
myStepper.step(-30);
}
else if (a > 530)
{
myStepper.setSpeed(rolePerMinute);
myStepper.step(30);
}
}
Hello! Here is the basic code I ran for 1 stepper motor and it seems to be running smoothy.
However, now that I applied user JCA34F's suggestion to the code I originally had, it moves but it stops and goes. So it's overall slower. Also I will check if I have a DMM.
I will admit, I have other things attach to this for a bigger project I'm doing along with the joysticks, but it is attached to the other breadboard (not shown)
#include <Stepper.h>
const int stepsPerRevolution = 2048; // change this to fit the number of steps per revolution
const int rolePerMinute = 10;
const int X_pin = 8; // analog pin connected to X output
Stepper myStepper(stepsPerRevolution, 22, 26, 24, 28);
void setup() {
myStepper.setSpeed(rolePerMinute);
//Serial.begin(9600);
}
void loop() {
// Serial.print("X-axis: ");
// Serial.println(analogRead(X_pin));
int a = analogRead(X_pin);
if (a > 400 && a < 520)
{
for (int i = 2; i < 6; i++)
{
digitalWrite(i, LOW);
}
}
else if (a < 400)
{
myStepper.setSpeed(rolePerMinute);
myStepper.step(-30);
}
else if (a > 530)
{
myStepper.setSpeed(rolePerMinute);
myStepper.step(30);
}
}