| 
View
 

unipolar stepper motor rotation

Page history last edited by Kristin Lucas 17 years, 8 months ago

int motorPin1 = 2;

int motorPin2 = 3;

int motorPin3 = 4;

int motorPin4 = 5;

int analogPin = 1;

int delayTime = 50; //10

int val = 100;

int stepcount = 1; // counter counts each step

int chagenumber = 80; // the # of stepcount when the direction changes.

int stepdirection =1; // this is the variable for the direction

void setup() {

 Serial.begin(9600);

 pinMode(motorPin1, OUTPUT);

 pinMode(motorPin2, OUTPUT);

 pinMode(motorPin3, OUTPUT);

 pinMode(motorPin4, OUTPUT);

}

//   button();

void loop() {

 val = analogRead(analogPin);   // read the input pin

 delayTime = (val/5) + 10 ;

 if (stepcount > chagenumber){ // when it's ready to chage direction

  //  chagenumber = random(200); // put this line in to randomly change when it changes direction

   stepcount = 0; // reset the counter

   if (stepdirection == 1){ // if it's going clockwise

     stepdirection = 0; // set the switch the other way

   }

   else {

     stepdirection = 1;

   }

 }

 if (stepdirection == 1){

   forward(); // this jumps down to the forward section of the code below

 }

 if (stepdirection == 0){

   backwards();// this jumps down to the backwards section of the code below

 }

}

void forward(){

 digitalWrite(motorPin1, HIGH);

 digitalWrite(motorPin2, HIGH);

 digitalWrite(motorPin3, LOW);

 digitalWrite(motorPin4, LOW);

 stepcount ++;

 delay(delayTime);

 digitalWrite(motorPin1, LOW);

 digitalWrite(motorPin2, HIGH);

 digitalWrite(motorPin3, HIGH);

 digitalWrite(motorPin4, LOW);

 stepcount ++;

 delay(delayTime);

 digitalWrite(motorPin1, LOW);

 digitalWrite(motorPin2, LOW);

 digitalWrite(motorPin3, HIGH);

 digitalWrite(motorPin4, HIGH);

 stepcount ++;

 delay(delayTime);

 digitalWrite(motorPin1, HIGH);

 digitalWrite(motorPin2, LOW);

 digitalWrite(motorPin3, LOW);

 digitalWrite(motorPin4, HIGH);

 stepcount ++;

 delay(delayTime);

}

void backwards(){

 digitalWrite(motorPin1, HIGH);

 digitalWrite(motorPin2, HIGH);

 digitalWrite(motorPin3, LOW);

 digitalWrite(motorPin4, LOW);

 stepcount ++;

 delay(delayTime);

 digitalWrite(motorPin1, HIGH);

 digitalWrite(motorPin2, LOW);

 digitalWrite(motorPin3, LOW);

 digitalWrite(motorPin4, HIGH);

 stepcount ++;

 delay(delayTime);

 digitalWrite(motorPin1, LOW);

 digitalWrite(motorPin2, LOW);

 digitalWrite(motorPin3, HIGH);

 digitalWrite(motorPin4, HIGH);

 stepcount ++;

 delay(delayTime);

 digitalWrite(motorPin1, LOW);

 digitalWrite(motorPin2, HIGH);

 digitalWrite(motorPin3, HIGH);

 digitalWrite(motorPin4, LOW);

 stepcount ++;

 delay(delayTime);

} 

Comments (0)

You don't have permission to comment on this page.