SHOW:
|
|
- or go back to the newest paste.
1 | #include <Wire.h> | |
2 | #include <LiquidCrystal_I2C.h> | |
3 | #include <AccelStepper.h> | |
4 | ||
5 | #define ENA_PIN Q0_2 | |
6 | #define DIR_PIN Q0_1 | |
7 | #define PUL_PIN Q0_0 | |
8 | ||
9 | #define ArretRotation I0_6 //Sortie sécurité en cas d'arrêt de rotation arbre | |
10 | #define Signal I0_5 //Entrée signal depart | |
11 | #define CtrlRotation I0_4 //Entrée contrôle de rotation d'arbre | |
12 | #define VitMoins I0_3 //Entrée diminution de la vitesse de rotation | |
13 | #define VitPlus I0_2 //Entrée augmentation de la vitesse de rotation | |
14 | ||
15 | bool bButtonPressed = false; | |
16 | ||
17 | AccelStepper stepper(AccelStepper::DRIVER, PUL_PIN, DIR_PIN); | |
18 | LiquidCrystal_I2C lcd(0x27, 20, 4); //Set the LCD Adress to 0x27 for a 16 chars and 2 line display | |
19 | ||
20 | int Vitesse = 0; | |
21 | ||
22 | ||
23 | void IRAM_ATTR onButton0Event() { | |
24 | Vitesse = Vitesse + 5; // quelle est la vitesse max ? | |
25 | bButtonPressed = true; | |
26 | } | |
27 | ||
28 | void IRAM_ATTR onButton1Event() { | |
29 | Vitesse = Vitesse - 5; // quelle est le minimum ? | |
30 | bButtonPressed = true; | |
31 | } | |
32 | ||
33 | ||
34 | void setup() { | |
35 | Wire.begin(); | |
36 | Serial.begin(9600); | |
37 | ||
38 | lcd.begin(20, 4); | |
39 | lcd.backlight(); | |
40 | ||
41 | - | if(digitalRead(VitPlus) == HIGH){ |
41 | + | |
42 | - | Vitesse = Vitesse + 5; |
42 | + | |
43 | - | delay(15); |
43 | + | |
44 | pinMode(VitMoins, INPUT); | |
45 | pinMode(CtrlRotation, INPUT); | |
46 | - | if(digitalRead(VitMoins) == HIGH){ |
46 | + | |
47 | - | Vitesse = Vitesse - 5; |
47 | + | |
48 | - | delay(15); |
48 | + | attachInterrupt(digitalPinToInterrupt(VitPlus), onButton0Event, RISING); // sur Arduino, interruptions seulement sur 2 et 3 |
49 | attachInterrupt(digitalPinToInterrupt(VitMoins), onButton1Event, RISING); // sur ESP32, beaucoup plus de possibilités | |
50 | ||
51 | - | stepper.setMaxSpeed(Vitesse); |
51 | + | |
52 | ||
53 | - | Serial.println(Vitesse); |
53 | + | |
54 | } | |
55 | - | displayValues(Vitesse); |
55 | + | |
56 | void loop() { | |
57 | ||
58 | if (bButtonPressed) { | |
59 | bButtonPressed = false; | |
60 | stepper.setMaxSpeed(Vitesse); | |
61 | Serial.println(Vitesse); | |
62 | displayValues(Vitesse); | |
63 | } | |
64 | ||
65 | if(digitalRead(Signal) == HIGH){ | |
66 | Moteur(); | |
67 | } | |
68 | } | |
69 | ||
70 | void Moteur(){ | |
71 | ||
72 | stepper.move(5000); | |
73 | stepper.run(); | |
74 | ||
75 | if(stepper.distanceToGo() == 0){ | |
76 | digitalWrite(ENA_PIN, HIGH); | |
77 | } | |
78 | else { | |
79 | digitalWrite(ENA_PIN, LOW); | |
80 | } | |
81 | } | |
82 | ||
83 | void displayValues(int Vitesse) { | |
84 | lcd.clear(); | |
85 | ||
86 | lcd.setCursor(7, 0); | |
87 | lcd.print("TEST"); | |
88 | - | delay(50); |
88 | + | |
89 | lcd.setCursor(1, 1); | |
90 | lcd.print("Reglage vitesse"); | |
91 | ||
92 | lcd.setCursor(3, 3); | |
93 | lcd.print("Vitesse : "); | |
94 | lcd.print(Vitesse); | |
95 | ||
96 | // delay(50); // devenu inutile, n'est appelé que si changement | |
97 | } | |
98 |