Toggle switch avec un moteur pas à pas
Bonjour à tous,
Je souhaiterai faire fonctionner un moteur pas à pas à l'aide d'un switch (push button)
L'amélioration consisterait à appuyer une seule fois pour le démarrer (+X rotations) et vice versa.
J'ai essayé le script ci dessous sans succès. La Led interne s'allume et reste fixe lorsque j'appuie une seule fois mais le moteur ne tourne pas.
Savez-vous me dire pourquoi celui-ci ne tourne pas?
Merci pour l'aide ;)
Voici le schéma de montage de base.
https://i.postimg.cc/w19Kb6LJ/moteur.png
et le script sur lequel se base mon essai:
Code:
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
| /*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
*
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-toggle-led
*/
// constants won't change
const int BUTTON_PIN = 12; // Arduino pin connected to button's pin
const int LED_PIN = 13; // Arduino pin connected to LED's pin
int smDirectionPin = 3; //Direction pin
int smStepPin = 2; //Stepper pin
// variables will change:
int ledState = LOW; // the current state of LED
int lastButtonState; // the previous state of button
int currentButtonState; // the current state of button
void setup() {
Serial.begin(9600); // initialize serial
pinMode(smDirectionPin, OUTPUT); // set arduino pin to output mode
pinMode(smStepPin, OUTPUT); // set arduino pin to output mode
pinMode(BUTTON_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode
pinMode(LED_PIN, OUTPUT); // set arduino pin to output mode
currentButtonState = digitalRead(BUTTON_PIN);
}
//Motor Forward function
void SlideForward() {
// turn motor foward:
digitalWrite(smDirectionPin, HIGH); //Writes the direction to the EasyDriver DIR pin. (HIGH is clockwise).
/*turns the motor 1 step*/
for (int i = 0; i < 400; i++){
digitalWrite(smStepPin, ledState);
delayMicroseconds(70);
digitalWrite(smStepPin, LOW);
delayMicroseconds(70);
}
}
void loop() {
lastButtonState = currentButtonState; // save the last state
currentButtonState = digitalRead(BUTTON_PIN); // read new state
if(lastButtonState == HIGH && currentButtonState == LOW) {
Serial.println("The button is pressed");
// toggle state of LED
ledState = !ledState;
// control LED arccoding to the toggled state
digitalWrite(LED_PIN, ledState);
SlideForward();
}
} |