Paso 3: código
//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); } }
Arriba es el código que usé para la máquina de dulces. Es un control básico del motor paso a paso de dos botones. En secuencia se enciende uno de los pernos de motor mientras que el resto están apagados. A continuación el siguiente perno motor pasa mientras que el resto son apagado y así sucesivamente. Botón 1 (val 1) es en una dirección (motorpin 1,2,3,4) mientras que el botón 2 (val 2) está en la otra dirección (motorpin 4,3,2,1). El retraso es cuánto tiempo entre cada paso que el Arduino va a esperar. Mientras menor sea el número más rápido. Me enteré de que un retraso de 3 es el más rápido el paso a paso puede ir.