// Step motor position control
// https://forum.arduino.cc/t/step-motor-position-control/1300890
// definizione dei PIN
const int dirPin = 5;
const int stepPin = 6;
const int motMS1Pin = 9;
const int motMS2Pin = 10;
const int motMS3Pin = 11;
const int stepPerRevolution = 200 * 16;
// list of microstep config pin outputs in binary notation B[MS1][MS2][MS3]
byte microSteps[5] = {
0b000, // full step
0b100, // 1/2 step
0b010, // 1/4 step
0b110, // 1/8 step
0b111, // 1/16 step
};
void setup() {
Serial.begin(115200);
// configure initial motor settings
digitalWrite(stepPin, LOW);
digitalWrite(dirPin, HIGH);
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(motMS1Pin, OUTPUT);
pinMode(motMS2Pin, OUTPUT);
pinMode(motMS3Pin, OUTPUT);
digitalWrite(motMS1Pin, bitRead(microSteps[4], 2));
digitalWrite(motMS2Pin, bitRead(microSteps[4], 1));
digitalWrite(motMS3Pin, bitRead(microSteps[4], 0));
Serial.print("sizeof(int) = ");
Serial.println(sizeof(int));
for (int8_t x = 0; x < sizeof(microSteps) / sizeof(microSteps[0]); x++) {
Serial.print("microSteps[");
Serial.print(x);
Serial.print("] = ");
Serial.println(microSteps[x]);
}
}
int motSpeed = 62; // TODO: desired speed in rpm 62 is for 6500bph
void loop() {
//Motore in direzione oraria
digitalWrite(dirPin, LOW);
Serial.println("Starting");
// 45 giri del motore, cioè 5 della Filler
// for (int32_t x = 0; x < 45 * stepPerRevolution; x++) { // Doesn't work with 8bit Nano
for (int32_t x = 0; x < 45UL * stepPerRevolution; x++) { // 45UL mandatory with 8bit as noticed by MicroBahner post #17
digitalWrite(stepPin, HIGH);
delayMicroseconds(motSpeed);
digitalWrite(stepPin, LOW);
delayMicroseconds(motSpeed);
}
Serial.println("Done");
delay(10000); // pausa di 10 secondo
}