Talk:S&a prototype 1 group 1
Stepper motor tutorial: making quarter turns and back
This tutorial is about a stepper motor than can make a quarter turn and goes back.
It can do this multiple times.
Parse the amount of cycles in the Serial Monitor!
Needed:
wires (catagorized in color preferably) breadboard
L239d H-bridge (scheme: [1]) 28BYJ-48 Stepper motor
Setup:
1. Place the bridge (note the little dot)
2. Connect the supply wires and ground wire
3. Connect the output wires (here it is 9, 10, 11 and 12)
4. Connect the wires for the stepper motor
Code:
/*
UniHobbies Arduino
edits by Jorik van den Bos
- /
//this is a built-in library
- include<Stepper.h>
int steps=0; int cycles=0; int stepsamount=512; int cycleamount=0;
int in1Pin =12; int in2Pin =11; int in3Pin =10; int in4Pin =9;
Stepper motor(512, in1Pin, in2Pin, in3Pin, in4Pin);
void setup() { pinMode(in1Pin, OUTPUT); pinMode(in2Pin, OUTPUT); pinMode(in3Pin, OUTPUT); pinMode(in4Pin, OUTPUT);
// this line is for Leonardo’s, it delays the serial interface // until the terminal window is opened while(!Serial); Serial.begin(9600); motor.setSpeed(40); }
void loop() { //parse the amount of cycles if(Serial.available()) cycleamount = Serial.parseInt();
//cycleamount should not be zero, can be set with parseInt()
if (cycleamount!=0)
{
//before the amount of cycles is reached, cycles<cyceleamount if (cycles<cycleamount) { //stepsamount should not be zero if (stepsamount!=0) { //do the steps for 1 cycle steps+=1; if (steps<stepsamount) motor.step(1); else if (steps<stepsamount*2) motor.step(-1); else /*reset step variables*/ {steps=0; cycles+=1;} } } //else reset cycle variables else {cycles=0; cycleamount=0;}
}
}