Mod edit:
This is a continuation from Stepper not turning
@ardnewbie03, @marwannnnn are apparently room mates working together on the same project.
Hey guys,,,,ive been trying to work the error in my code using chatgpt and some friends and something still seems to be off. ive hooked up a stepper motor to an H bridge and its been working fine,,,so has the encoder theyre both fine on their own,,,when hooked up togther though the stepper seems to be microsteping back and forth aand vibrating in place.
i need to make a code that when the encoder is turned the stepper turns proportionally....
#include <AccelStepper.h>
#include <Encoder.h>
#define IN1 1 // Stepper driver IN1 pin connected to Arduino pin 1
#define IN2 2 // Stepper driver IN2 pin connected to Arduino pin 2
#define encA 8 // Encoder A pin connected to Arduino pin 8
#define encB 9 // Encoder B pin connected to Arduino pin 9
// Define steps per revolution based on your motor's specification
int stepsPerRevolution = 200;
long encoderPos = 0;
long lastEncoderPos = 0;
long stepperPos = 0;
AccelStepper stepper(AccelStepper::DRIVER, IN1, IN2); // Stepper motor setup
Encoder encoder(encA, encB); // Encoder setup
void setup() {
stepper.setMaxSpeed(1000); // Set max speed of stepper motor
stepper.setAcceleration(500); // Set acceleration
Serial.begin(9600); // Start Serial Monitor for debugging
}
void loop() {
encoderPos = encoder.read(); // Get the current position of the encoder
// If the encoder position changes, move the stepper motor accordingly
if (encoderPos != lastEncoderPos) {
long deltaPos = encoderPos - lastEncoderPos; // Calculate how much the encoder moved
stepperPos += deltaPos; // Increment the stepper motor's position
// Move the stepper motor incrementally
stepper.moveTo(stepperPos);
// Move the stepper motor towards the target position
stepper.run();
lastEncoderPos = encoderPos; // Update the last encoder position
}
// Debugging: Print encoder position and stepper position to serial monitor
Serial.print("Encoder Pos: ");
Serial.print(encoderPos);
Serial.print(" | Stepper Pos: ");
Serial.println(stepperPos);
}
here is the chat gpt code
NOTE: the stepper is a nema 17 and the encoder is e38s6g5 600b
using a 12v 30amp power supply,,,,,,,,,,,,,thanks in adavnce guys,

