#include <AccelStepper.h> // for commands for max vel, max acc, and others
//#include <StepperDriver.h> // for A4988 specific stepper?
// nothing from these libraries used yet
#define dirPin 3
#define stepPin 4
#define MS1Pin 7
#define MS2Pin 6
#define MS3Pin 5
#define handwheelPin 3
int val = 0; // variable to store the value read
bool automatic = true; //
void setup() { //runs once
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(MS1Pin,OUTPUT);
pinMode(MS2Pin,OUTPUT);
pinMode(MS3Pin,OUTPUT);
Serial.begin(9600); // setup serial
}
// MS1 MS2 MS3
// low low low full step fast 200 steps for full rotation, 1,8° per step
// high low low half step 400 steps, 0,9°
// low high low 1/4 step 800, 0,45°
// high high low 1/8 step 1600, 0,225°
// high high high 1/16 step precise 3200 steps for full rotation, 0,1125°
void set_full_step (){
digitalWrite(MS1Pin,LOW); // ensuring full step resolution
digitalWrite(MS2Pin,LOW);
digitalWrite(MS3Pin,LOW);
}
void set_half_step (){
digitalWrite(MS1Pin,HIGH);
digitalWrite(MS2Pin,LOW);
digitalWrite(MS3Pin,LOW);
}
void set_quarter_step (){
digitalWrite(MS1Pin,LOW);
digitalWrite(MS2Pin,HIGH);
digitalWrite(MS3Pin,LOW);
}
void set_eigth_step (){
digitalWrite(MS1Pin,HIGH);
digitalWrite(MS2Pin,HIGH);
digitalWrite(MS3Pin,LOW);
}
void set_sixteenth_step (){
digitalWrite(MS1Pin,HIGH);
digitalWrite(MS2Pin,HIGH);
digitalWrite(MS3Pin,HIGH);
}
void loop() {
if (automatic == true) {
digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction (here clockwise)
set_quarter_step(); // ensuring full step resolution
for(int x = 0; x < 200; x++) { // Makes 200 pulses for making one full cycle rotation
digitalWrite(stepPin,HIGH);
delayMicroseconds(500); // increase delay between steps to decrease rotation speed
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
delay(3000); // tree second delay
digitalWrite(dirPin,LOW); // Changes the rotation direction (here to counter clockwise)
for(int x = 0; x < 400; x++) { // Makes 400 pulses to do two full cycle rotation
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
delay(1000);
}
else { //manual mode
val = analogRead(handwheelPin); // read the input pin
float voltage = val * (5.0 / 1023.0);
Serial.println(voltage); // debug value
}
}