#include <AccelStepper.h>
// Define the number of steps per revolution for your stepper motor
#define STEPS_PER_REVOLUTION 200
// Define the pins connected to the stepper motor driver
#define STEP_PIN 2
#define DIR_PIN 3
// Create an instance of the AccelStepper library
AccelStepper stepper(AccelStepper::FULL4WIRE, STEP_PIN, DIR_PIN);
// Constants for the sine wave parameters
const float amplitude = 100; // Amplitude of the sine wave (adjust as needed)
const float frequency = 1.1; // Frequency of the sine wave (adjust as needed)
const float phaseShift = 0.0; // Phase shift of the sine wave (adjust as needed)
void setup()
{
// Set the maximum speed and acceleration of the stepper motor
stepper.setMaxSpeed(10000); // Adjust as needed
stepper.setAcceleration(500); // Adjust as needed
// Set the initial position of the stepper motor
stepper.setCurrentPosition(0);
}
void loop()
{
// Calculate the target position based on the sine wave
float time = millis() * 0.001; // Convert milliseconds to seconds
float targetPosition = amplitude * sin(2 * PI * frequency * time + phaseShift);
// Move the stepper motor to the target position
stepper.moveTo(targetPosition);
// Run the stepper motor until it reaches the target position
stepper.run();
// You can adjust the delay to control the speed of the sine wave
delay(1); // Adjust as needed
}