Go Down

Topic: how to change this code to control 2 stepper motors ? (Read 210 times) previous topic - next topic

Abdoubeggari

Code: [Select]
/
*************************************
 * Property of: microcontroller-project.com
 * Written by : Usman Ali Butt
 * Dated      : 30/4/2016
 ************************************/
 void Move_Bacward();     //Move Stepper Bacward
 void StartMotor();       //Start Motor
 unsigned int RotationSpeed=1000; // Variable
int Ss=0;              //Motor Start-Stop
char mov='F';          //Initially move Forward
int step1=2;
int step2=3;
int step3=4;
int step4=5;

void setup() {
pinMode(step1,OUTPUT);
pinMode(step2,OUTPUT);
pinMode(step3,OUTPUT);
pinMode(step4,OUTPUT);
//Serial1.begin(9600);    //Begin Serial Monitor
Serial.begin(9600);
}

void StartMotor(){                   //Move Motor Forward
  digitalWrite(step1,HIGH);
  delay(RotationSpeed);
  digitalWrite(step1,LOW);

  digitalWrite(step2,HIGH);
  delay(RotationSpeed);
  digitalWrite(step2,LOW);

  digitalWrite(step3,HIGH);
  delay(RotationSpeed);
  digitalWrite(step3,LOW);

  digitalWrite(step4,HIGH);
  delay(RotationSpeed);
  digitalWrite(step4,LOW);
  }

void Move_Bacward(){                 //Move Stepper Backward

  digitalWrite(step4,HIGH);
  delay(RotationSpeed);
  digitalWrite(step4,LOW);

  digitalWrite(step3,HIGH);
  delay(RotationSpeed);
  digitalWrite(step3,LOW);

  digitalWrite(step2,HIGH);
  delay(RotationSpeed);
  digitalWrite(step2,LOW);

  digitalWrite(step1,HIGH);
  delay(RotationSpeed);
  digitalWrite(step1,LOW);
 
  }



void loop() {
char received='z';     //Received Command from bluetooth
 
  if(Serial.available()>=0){
    received=Serial.read();
    delay(100);
    Serial.println(received);
    }
    Serial.print(Ss); 
   
      if(received=='s' || received=='S'){
        Ss=(!Ss);
        }
       else{
        if(received=='0')RotationSpeed=1000;
        if(received=='1')RotationSpeed=900;
        if(received=='2')RotationSpeed=800;
        if(received=='3')RotationSpeed=700;
        if(received=='4')RotationSpeed=500;
        if(received=='5')RotationSpeed=400;
        if(received=='6')RotationSpeed=300;
        if(received=='7')RotationSpeed=200;
        if(received=='8')RotationSpeed=100;
        if(received=='9')RotationSpeed=50;
        if(received=='f' || received=='F')mov='F';
        if(received=='b' || received=='B')mov='B';
        }
       
      if(Ss==1){
        if(mov=='F')StartMotor();//Move Forward
        if(mov=='B')Move_Bacward();//Move Forward
        }
        else{
        //stopmotor; 
        }

}

Robin2

I suggest you don't start with that code. All those delay()s are going to make it almost impossible to get sensible movement from 2 motors.

Post a link to the datasheet for the stepper motors you are using. And tell us what stepper motor drivers you are using.

Have a look at these links
Stepper Motor Basics
Simple Stepper Code

also look up the AccelStepper library

For receiving data to control the motors have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up