-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStepper.ino
114 lines (103 loc) · 3.08 KB
/
Stepper.ino
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <Arduino.h>
#include <AccelStepper.h>
// 1000pas=3,5cm in real life
// vitesse lien serie USB
#define SERIAL_RATE 9600
// taille max tampon donnees lien serie
#define STRING_SIZE 255
// All the wires needed for full functionality
#define STEP 9
#define DIR 8
#define DISBL 7
#define FIN_COURSE1 1
#define FIN_COURSE2 2
#define BOUTON_STOP 3
// vitesse rotation moteur cible;
#define MAX_SPEED 2400
#define ACCELERATION 1000
// garder le moteur alimente pour tenir la position
AccelStepper stepper (1, STEP, DIR);
// Serial vars
unsigned int inputCharCount = 0;
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
// pas a faire tourner
double pas = 0;
void setup() {
Serial.begin(SERIAL_RATE);
inputString.reserve(STRING_SIZE);
while (!Serial) {;}
Serial.println("Parametrez le moniteur serie pour l'envoi du caractere 'Nouvelle Ligne'\n La distance que vous voulez faire doit etre en mm");
stringComplete = false;
pinMode(DISBL, OUTPUT); // disable coil pin
pinMode(FIN_COURSE1, INPUT_PULLUP);
pinMode(FIN_COURSE2, INPUT_PULLUP);
pinMode(BOUTON_STOP, INPUT_PULLUP);
digitalWrite(DISBL, LOW); // disable coils
attachInterrupt(digitalPinToInterrupt(BOUTON_STOP), arret_urgence, FALLING);
attachInterrupt(digitalPinToInterrupt(FIN_COURSE1), arret_course2, FALLING);
attachInterrupt(digitalPinToInterrupt(FIN_COURSE2), arret_course1, FALLING);
stepper.setMaxSpeed(MAX_SPEED); // Set target motor RPM.
}
void loop() {
serialEvent();
if (stringComplete) {
Serial.print("serial message received:");
Serial.println(inputString);
if (inputString.startsWith("-")) {
pas = 0 - (inputString.substring(1).toInt()/0.035);
} else {
pas = (inputString.toInt()/0.035);
}
inputCharCount = 0;
inputString = "";
stringComplete = false;
}
else if (pas != 0) {
Serial.print("moving steps:");
Serial.println(pas);
stepper.setAcceleration (ACCELERATION);
stepper.move(pas); // move avec un nombre en step
pas = 0;
digitalWrite(DISBL, HIGH); // active coils
}
stepper.run();
if (stepper.speed() == 0) {
digitalWrite(DISBL, LOW); // disable coils
}
}
void arret_urgence() {
Serial.println("arret urgence \n");
stepper.setSpeed(0);
stepper.moveTo(stepper.currentPosition());
// stepper.stop(); // slow stop, deccelerate before stop ...
pas = 0;
}
void arret_course1() {
Serial.println("arret course1 \n");
stepper.setSpeed(0);
stepper.moveTo(stepper.currentPosition());
// stepper.stop(); // slow stop, deccelerate before stop ...
pas = 0;
}
void arret_course2() {
Serial.println("arret course2 \n");
stepper.setSpeed(0);
stepper.moveTo(stepper.currentPosition());
// stepper.stop(); // slow stop, deccelerate before stop ...
pas = 0;
}
void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read();
inputCharCount ++;
if (inputCharCount < STRING_SIZE) {
inputString += inChar;
if (inChar == '\n') {
stringComplete = true;
}
} else {
stringComplete = true;
}
}
}