#include <math.h>
#define STEP_PIN 3
#define DIR_PIN 2
const float amplitude = 200; // Amplitude of sine wave (number of steps)
const float frequency = 1; // Frequency of sine wave (Hz)
const float phase_shift = 0; // Phase shift of sine wave (radians)
void setup() {
pinMode(STEP_PIN, OUTPUT);
pinMode(DIR_PIN, OUTPUT);
}
void loop() {
float time = millis() / 1000.0; // Time in seconds
float angle = 2 * PI * frequency * time + phase_shift; // Calculate angle in radians
int steps = amplitude * sin(angle); // Compute number of steps based on sine wave
// Set direction based on sign of steps
if (steps >= 0) {
digitalWrite(DIR_PIN, HIGH); // Set direction (clockwise)
} else {
digitalWrite(DIR_PIN, LOW); // Set direction (counterclockwise)
}
// Generate STEP pulses
for (int i = 0; i < abs(steps); i++) {
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(500); // Adjust delay for desired speed
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(500); // Adjust delay for desired speed
}
}