-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStepperControl.ino
More file actions
54 lines (46 loc) · 1.18 KB
/
StepperControl.ino
File metadata and controls
54 lines (46 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// StepperControl.ino
// Autore: bocaletto-luca
// Controllo base per motore stepper con A4988
const int stepPin = 2;
const int dirPin = 3;
const int enPin = 4;
const int btnFwd = 5; // Pulsante avanti
const int btnRev = 6; // Pulsante indietro
const int btnRst = 7; // Pulsante reset
long currentStep = 0;
void setup() {
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(enPin, OUTPUT);
digitalWrite(enPin, LOW); // Attiva motore
pinMode(btnFwd, INPUT_PULLUP);
pinMode(btnRev, INPUT_PULLUP);
pinMode(btnRst, INPUT_PULLUP);
Serial.begin(9600);
Serial.println("Stepper pronto.");
}
void loop() {
if (digitalRead(btnFwd) == LOW) {
moveStepper(1);
delay(200);
}
if (digitalRead(btnRev) == LOW) {
moveStepper(-1);
delay(200);
}
if (digitalRead(btnRst) == LOW) {
currentStep = 0;
Serial.println("Posizione azzerata.");
delay(500);
}
}
void moveStepper(int direction) {
digitalWrite(dirPin, direction > 0 ? HIGH : LOW);
digitalWrite(stepPin, HIGH);
delayMicroseconds(800);
digitalWrite(stepPin, LOW);
delayMicroseconds(800);
currentStep += direction;
Serial.print("Posizione: ");
Serial.println(currentStep);
}