Étape 3: Code
//Robert Core //Using two buttons to control the direction of the stepper motor int button_1 = 2;//Button 1 is plugged into digital port 2 int button_2 = 3;//Button 2 is plugged into digital port 3 int motorPin1 = 8;//Motor pin 1 is plugged into digital port 8 int motorPin2 = 9;//Motor pin 2 is plugged into digital port 9 int motorPin3 = 10;//Motor pin 3 is plugged into digital port 10 int motorPin4 = 11;//Motor pin 4 is plugged into digital port 11 int motor_Speed = 3;//Tells how fast the stepper motor is int motor_Step; int val1 = 0; int val2 = 0; void setup() {//input and output values of the pins pinMode(button_1, INPUT); pinMode(button_2, INPUT); pinMode(motorPin1, OUTPUT); pinMode(motorPin2, OUTPUT); pinMode(motorPin3, OUTPUT); pinMode(motorPin4, OUTPUT); } void loop() { val1 = digitalRead(button_1);//if button 1 is pressed motor will go clockwise if (val1 == HIGH) { digitalWrite(motorPin1, HIGH);//Motor pin is on digitalWrite(motorPin2, LOW);//Motor pin is off digitalWrite(motorPin3, LOW);//Motor pin is off digitalWrite(motorPin4, LOW);//Motor pin is off delay(motor_Speed); digitalWrite(motorPin1, LOW);//Motor pin is off digitalWrite(motorPin2, HIGH);//Motor pin is on digitalWrite(motorPin3, LOW);//Motor pin is off digitalWrite(motorPin4, LOW);//Motor pin is off delay(motor_Speed); digitalWrite(motorPin1, LOW);//Motor pin is off digitalWrite(motorPin2, LOW);//Motor pin is off digitalWrite(motorPin3, HIGH);//Motor pin is on digitalWrite(motorPin4, LOW);//Motor pin is off delay(motor_Speed); digitalWrite(motorPin1, LOW);//Motor pin is off digitalWrite(motorPin2, LOW);//Motor pin is off digitalWrite(motorPin3, LOW);//Motor pin is off digitalWrite(motorPin4, HIGH);//Motor pin is on delay(motor_Speed); } val2 = digitalRead(button_2);//if button 2 is pressed motor will go counterclockwise if (val2 == HIGH) { digitalWrite(motorPin4, HIGH);//Motor pin is on digitalWrite(motorPin3, LOW);//Motor pin is off digitalWrite(motorPin2, LOW);//Motor pin is off digitalWrite(motorPin1, LOW);//Motor pin is off delay(motor_Speed); digitalWrite(motorPin4, LOW);//Motor pin is off digitalWrite(motorPin3, HIGH);//Motor pin is on digitalWrite(motorPin2, LOW);//Motor pin is off digitalWrite(motorPin1, LOW);//Motor pin is off delay(motor_Speed); digitalWrite(motorPin4, LOW);//Motor pin is off digitalWrite(motorPin3, LOW);//Motor pin is off digitalWrite(motorPin2, HIGH);//Motor pin is on digitalWrite(motorPin1, LOW);//Motor pin is off delay(motor_Speed); digitalWrite(motorPin4, LOW);//Motor pin is off digitalWrite(motorPin3, LOW);//Motor pin is off digitalWrite(motorPin2, LOW);//Motor pin is off digitalWrite(motorPin1, HIGH);//Motor pin is on delay(motor_Speed); } }
Plus haut est le code que j’ai utilisé pour la machine à bonbons. C’est une base commande de moteur pas à pas deux boutons. Dans l’ordre il s’allume une des broches moteurs tandis que le reste sont désactivés. Alors la prochaine broche moteur s’allume tandis que le reste sont éteints et ainsi de suite. Bouton 1 (val 1) est dans une seule direction (motorpin 1,2,3,4) tandis que le bouton 2 (val 2) est dans l’autre sens (motorpin 4,3,2,1). Le délai est de combien de temps entre chaque étape que l’Arduino attendra. Plus le nombre le plus rapide. J’ai découvert qu’un retard de 3 est le plus rapide que le stepper peut aller.