Go Down

Topic: Home steppers at beginning and then run the process once (Read 174 times) previous topic - next topic

jay5050

I'm new to arduino so please forgive my stupidity if this is just a simple problem.(sorry for my poor English speaking)

i want to control two stepper motors (Nema 17) using pololu a4988 drivers.


In the setup There are

 A push button [ 'Start button'-Start the process],

 two stepper motors (Z axis , X axis),

 two limit switches (Z axis home limit, X axis home limit)

 One servo motor

 Two led indicators ( Ready status((GREEN)), Processing status((RED)))


At start (when powered on the arduino) Ready Status(Green) indicator should be on so the process can be start by pushing 'Start button'. (This means that the two motors are already at the home position from last time its powered off)

If the two motors are not in the home position when powered on arduino (beacause of power failure or smt) the Processing status(Red) led should be on and both motors get to home position automatically.Then the red led turns off and Green led turns on.


 
The process is like this,(When pressed start button)

   * Servo at 0 position
 
   * Green led turns off Red led turns on

   * Z axis runs 500 steps (from home position) and stops

   * X axis runs 1000 steps (from home position) and stops

   * Servo turns 60 deg

   * Z axis goes back to home position and stops

   * X axis goes back to home position and stops

   * Red led turns off Green led turns on

After that program stops it runs again if start button is pressed.


im a beginner to arduino and dont have programming knowledge. tried lot of forums, codes and stuff but nothing was helpful. Please help me to solve this  :( 

can some one give me some idea, tutorial, a sample code or any guidance if you have already done something like this...

Thank you !!!!

MorganS

"The problem is in the code you didn't post."

Robin2

At start (when powered on the arduino) Ready Status(Green) indicator should be on so the process can be start by pushing 'Start button'. (This means that the two motors are already at the home position from last time its powered off)

If the two motors are not in the home position when powered on arduino (beacause of power failure or smt) the Processing status(Red) led should be on and both motors get to home position automatically.Then the red led turns off and Green led turns on.
At startup there is no means for the Arduino to know where the motors are. You need to do the homing movement every time the Arduino is started or reset.

In general, the way to home a stepper motor is to move it one step at a time towards the limit switch and check the switch between steps. If the switch is triggered before the motor moves at all then it would be wise to move the motor away from the switch and then back towards it.

If you want more help please post the code that represents your best attempt and tell us exactly what happens when you run it and what you want it to do that is different.

...R
Stepper Motor Basics
Simple Stepper Code
Two or three hours spent thinking and reading documentation solves most programming problems.

jay5050

I got a code from one of my friend This is the code

Code: [Select]

#include <Servo.h>
Servo servo;
const int stepPin_X = 3;
const int dirPin_X = 4;
const int Enable_X = 7;
const int Enable_Z = 8;
const int stepPin_Z = 13;
const int dirPin_Z = 6;
const int Start_pin = 12;
const int green_led = 2;
const int red_led = 1;

const int limit_X = 9;//need GND to active
const int limit_Z = 10;//need GND to active
bool lim_X;
bool lim_Z;
int x;
int start;

void setup() {
 // Sets the two pins as Outputs
 pinMode(stepPin_X, OUTPUT);
 pinMode(dirPin_X, OUTPUT);
 pinMode(stepPin_Z, OUTPUT);
 pinMode(dirPin_Z, OUTPUT);
 pinMode(limit_X, INPUT_PULLUP);
 pinMode(limit_Z, INPUT_PULLUP);
 pinMode(Start_pin, INPUT_PULLUP);
 pinMode(Enable_X, OUTPUT);
 pinMode(Enable_Z, OUTPUT);
 pinMode(green_led, OUTPUT);
 pinMode(red_led, OUTPUT);
 servo.attach(11);
 Serial.begin(9600);
servo.write(50);
 x = 0;
}
void loop() {

 lim_X = digitalRead(limit_X);
 lim_Z = digitalRead(limit_Z);

 if (lim_X == 1) {
   stepper(1, 1);
 } else if (lim_X == 0) {
   stepper(1, 2);
 }
 if (lim_Z == 1) {
   stepper(0, 1);
 } else if (lim_Z == 0) {
   stepper(0, 2);
 }
 if (lim_X == 0 && lim_Z == 0) {
   Serial.print("--X&Z limit");
   stepper(1, 0);
   stepper(0, 0);
   operate();
 }



}
void operate() {
 while (1) {
   stepper(1, 2);
   stepper(0, 2);
   Serial.println("press_start");
   digitalWrite(red_led,LOW);
   start = digitalRead(Start_pin);
   digitalWrite(green_led,HIGH);
   if (start == 0) {
     for (int i = 0; i < 500; i++) {
       stepper(0, 0);
     }
     for (int j = 0; j < 1000; j++) {
       stepper(1, 0);
     }
     for (int k = 0; k < 500; k++) {
       stepper(0, 1);
     }
     servo.write(5);
     for (int l = 0; l < 1000; l++) {
       stepper(1, 1);
     }
     servo.write(50);
   }

 }
}
/*
  stpr 1=x, 0=z
  dir 1=high,0=low,2= no_rotate


*/
void stepper(int stpr, int dir) {

 int stepPin;
 int direc;
 if (stpr == 0) {
   direc = dirPin_X;
   digitalWrite(green_led,LOW);
   digitalWrite(Enable_X, LOW);
   digitalWrite(red_led,HIGH);
   stepPin = stepPin_X;
   Serial.println("x_on");
 } else if (stpr == 1) {
   direc = dirPin_Z;
   stepPin = stepPin_Z;
   digitalWrite(green_led,LOW);
   digitalWrite(Enable_Z, LOW);
   digitalWrite(red_led,HIGH);
   Serial.println("z_on");

 } else {
   return;
 }
 if (dir == 1) {
   digitalWrite(direc, HIGH);
   digitalWrite(stepPin, HIGH);
   delayMicroseconds(500);
   digitalWrite(stepPin, LOW);
   delayMicroseconds(500);

 } else if (dir == 0) {
   digitalWrite(direc, LOW);
   digitalWrite(stepPin, HIGH);
   delayMicroseconds(500);
   digitalWrite(stepPin, LOW);
   delayMicroseconds(500);
 } else if (dir == 2) {
   digitalWrite(direc, LOW);
   digitalWrite(stepPin, HIGH);
   Serial.print("stop");
 }
}


but the problem is z , x steppers dont stop even they hit limit switches. they just keep running .

and also the led indicaters dont work properly.

where could be the problem ?

Robin2

To make it easy for people to help you please modify your post and use the code button </>
Code: [Select]
so your code looks like this and is easy to copy to a text editor. See How to use the Forum

Your code is too long for me to study quickly without copying to a text editor.

Also please use the AutoFormat tool to indent your code for easier reading.

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

MorganS

It looks like 1=X and 0=Z got swapped in the middle there.  That is why magic numbers like 0 and 1 are bad.
"The problem is in the code you didn't post."

jay5050

Thank you guys for helping me on this. im new to this forum and sorry for mistakes i made.


  Now the limit switches work as i wanted, but led indicator problem remains. Red led always stay on even after the homing process. Green led works fine but its brightness is very low. where could be the problem ?   

this is the final code i have right now

Code: [Select]

#include <Servo.h>
Servo servo;
const int stepPin_X = 3;
const int dirPin_X = 4;
const int Enable_X = 7;
const int Enable_Z = 8;
const int stepPin_Z = 5;
const int dirPin_Z = 6;
const int Start_pin = 12;
const int green_led = 2;
const int red_led = 1;

const int limit_X = 9;//need GND to active
const int limit_Z = 10;//need GND to active
bool lim_X;
bool lim_Z;
int x;
int start;

void setup() {
  // Sets the two pins as Outputs
  pinMode(stepPin_X, OUTPUT);
  pinMode(dirPin_X, OUTPUT);
  pinMode(stepPin_Z, OUTPUT);
  pinMode(dirPin_Z, OUTPUT);
  pinMode(limit_X, INPUT_PULLUP);
  pinMode(limit_Z, INPUT_PULLUP);
  pinMode(Start_pin, INPUT_PULLUP);
  pinMode(Enable_X, OUTPUT);
  pinMode(Enable_Z, OUTPUT);
  pinMode(green_led, OUTPUT);
  pinMode(red_led, OUTPUT);
  servo.attach(11);
  Serial.begin(9600);
servo.write(50);
  x = 0;
}
void loop() {

  lim_X = digitalRead(limit_X);
  lim_Z = digitalRead(limit_Z);

  if (lim_X == 1) {
    stepper(1, 1);
  } else if (lim_X == 0) {
    stepper(1, 2);
  }
  if (lim_Z == 1) {
    stepper(0, 1);
  } else if (lim_Z == 0) {
    stepper(0, 2);
  }
  if (lim_X == 0 && lim_Z == 0) {
    Serial.print("--X&Z limit");
    stepper(1, 0);
    stepper(0, 0);
    operate();
  }



}
void operate() {
  while (1) {
    stepper(1, 2);
    stepper(0, 2);
    Serial.println("press_start");
    digitalWrite(red_led,LOW);
    start = digitalRead(Start_pin);
    digitalWrite(green_led,HIGH);
    if (start == 0) {
      for (int i = 0; i < 500; i++) {
        stepper(0, 0);
      }
      for (int j = 0; j < 1000; j++) {
        stepper(1, 0);
      }
      for (int k = 0; k < 500; k++) {
        stepper(0, 1);
      }
      servo.write(5);
      for (int l = 0; l < 1000; l++) {
        stepper(1, 1);
      }
      servo.write(50);
    }

  }
}
/*
   stpr 1=x, 0=z
   dir 1=high,0=low,2= no_rotate


*/
void stepper(int stpr, int dir) {

  int stepPin;
  int direc;
  if (stpr == 0) {
    direc = dirPin_X;
    digitalWrite(green_led,LOW);
    digitalWrite(Enable_X, LOW);
    digitalWrite(red_led,HIGH);
    stepPin = stepPin_X;
    Serial.println("x_on");
  } else if (stpr == 1) {
    direc = dirPin_Z;
    stepPin = stepPin_Z;
    digitalWrite(green_led,LOW);
    digitalWrite(Enable_Z, LOW);
    digitalWrite(red_led,HIGH);
    Serial.println("z_on");

  } else {
    return;
  }
  if (dir == 1) {
    digitalWrite(direc, HIGH);
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(500);

  } else if (dir == 0) {
    digitalWrite(direc, LOW);
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(500);
  } else if (dir == 2) {
    digitalWrite(direc, LOW);
    digitalWrite(stepPin, HIGH);
    Serial.print("stop");
  }
}



Robin2

Now the limit switches work as i wanted, but led indicator problem remains. Red led always stay on even after the homing process. Green led works fine but its brightness is very low. where could be the problem ? 
Why have you connected your red LED to one of the Serial pins?

What proportion of the time is the green LED on?

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

Go Up