Stap 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); } }
Boven is de code die ik voor de snoep machine gebruikte. Het is een fundamentele twee stepper motor knopbesturingselement. In volgorde blijkt één van de motor pinnen op terwijl de rest zijn uitgeschakeld. Vervolgens gaat de volgende motor pin op terwijl de rest zijn uitgeschakeld, enzovoort. Knop 1 (val 1) is in één richting (motorpin 1,2,3,4) terwijl knop 2 (val 2) is in de andere richting (motorpin 4,3,2,1). De vertraging is hoelang tussen elke stap die de Arduino zal wachten. Hoe kleiner het getal des te sneller. Ik heb ontdekt dat een vertraging van 3 is de snelste de stepper kan gaan.