got a Stepper Motor project dumped on me.....

Hello, i am a total newb. So apologies if i am not listing all the info needed.

I got a project of running 2 stepper motors with a Mega2560

One is a rotating platform and the other is a screw drive.

I was told that the program i was given worked. It doesnt

It is supposed to run the screw drive to a home switch to set it to a known position at every restart.
The turntable is supposed to rotate to a proximity sensor to set known position.

Once i got all the connections made the screw drive moves 2.5" and stops. Even if i move it closer to the home switch nothing happens.
The turn table just twitches and then nothing...

Ive been reading as much as i can about programming. Not going to lie, its not my thing. I think ill eventually get it, but right now what i see in this program doesnt compare to what i read.

I have connections made to pins 2,3, 8,9,10 & 12 for controls and then to 5v and GND

For the Turn Table

Pin 2 is DIR-
Pin 3 is STP-
5v to both DIR+ & STP+
Pin 12 is to the Prox Sensor

For the Screw Drive

Pin 8 is for STP-
Pin 9 is for DIR-
5v out to both STP+ & DIR+
Pin 10 is to the Home Switch

When i read the code i can only see that pin 10 is assigned for sure.

I am not sure where all the other pins are being defined. Some of the code on top appears to match my pin ID, but then other code seems to be doing something different.

example:
int ttsteppin = 3;
int ttdirectionpin = 2;

and then

int cr = 13;
int buttonright = 6;
int buttonleft =5;

It looks like the proximity sensor code is disabled. I think that is because it was written before it was installed.

Any help is appreciated.

Thanks!

int steppin = 8;
int directionpin = 9;
const int buttonPin = 10;
int Distance = 0;
int Speed = 1;
long NumSteps =(0);
int i=2;
int j=2;
long jogsteps = 200000;
long systemsteps = 787500;
long homesteps = 9000;
int buttonState = 0;
int buttonStateright = 3;
int buttonStateleft = 3;
long setmovesteps = 275000;
int done = 0;
//int relayPin = 12;
int relayState = 0;
int cr = 13;
int buttonright = 6;
int buttonleft =5;

int ttsteppin = 3;
int ttdirectionpin = 2;
long ttsystemsteps = 12500;
long rightsystemsteps = -1*systemsteps;
int ttSpeed = 80;
int ttbutton = 12;
int ttbuttonState = 3;

int prox = 13;
int proxState = 3;
int proxdone = 0;


void setup() {
Serial.begin(9600);
Serial1.begin(9600);
pinMode (steppin, OUTPUT);
pinMode (directionpin, OUTPUT);
digitalWrite (steppin, LOW);
pinMode (buttonPin, INPUT_PULLUP);
//pinMode (relayPin, INPUT_PULLUP);
pinMode(buttonright,INPUT_PULLUP);
pinMode(buttonleft, INPUT_PULLUP);

pinMode (ttsteppin, OUTPUT);
pinMode (ttdirectionpin, OUTPUT);
digitalWrite (ttsteppin, LOW);
pinMode (ttbutton, INPUT_PULLUP);
pinMode (prox, INPUT_PULLUP);
}

void ttleft (){
for( long NumSteps=0; NumSteps <= ttsystemsteps; NumSteps = NumSteps + 1){
   digitalWrite (ttdirectionpin, LOW);
    digitalWrite (ttsteppin,HIGH);
    delayMicroseconds (ttSpeed);
    digitalWrite(ttsteppin, LOW);
    delayMicroseconds (ttSpeed); 
    
}}

void ttright (){
for( long NumSteps=0; NumSteps <= ttsystemsteps; NumSteps = NumSteps + 1){
   digitalWrite (ttdirectionpin, HIGH);
    digitalWrite (ttsteppin,HIGH);
    delayMicroseconds (ttSpeed);
    digitalWrite(ttsteppin, LOW);
    delayMicroseconds (ttSpeed); 
    
}}

void left (){
for( long NumSteps=0; NumSteps <= systemsteps; NumSteps = NumSteps + 1){
   digitalWrite (directionpin, LOW);
    digitalWrite (8,HIGH);
    delayMicroseconds (Speed);
    digitalWrite(8, LOW);
    delayMicroseconds (Speed); 
 
}
}
void right (){
for( long NumSteps=0; NumSteps <= systemsteps; NumSteps = NumSteps + 1){
   digitalWrite (directionpin, HIGH);
    digitalWrite (8,HIGH);
    delayMicroseconds (Speed);
    digitalWrite(8, LOW);
    delayMicroseconds (Speed); 
}
}



void movehome (){
 buttonState = digitalRead (buttonPin);

 while(buttonState != 0)
 {
  digitalWrite (directionpin, HIGH);
  digitalWrite (8,HIGH);
    delayMicroseconds (Speed);
    digitalWrite(8, LOW);
    delayMicroseconds (Speed);
    buttonState = digitalRead (buttonPin);
  
 }

 done = 1;
   }  

  void tthome (){
 proxState = digitalRead (prox);

 while(proxState != 1)
 {
  digitalWrite (ttdirectionpin, HIGH);
  digitalWrite (ttsteppin,HIGH);
    delayMicroseconds (ttSpeed);
    digitalWrite(ttsteppin, LOW);
    delayMicroseconds (ttSpeed);
    proxState = digitalRead (prox);
  
 }
 proxdone=1;
   }  
   
void setmove (){
 
    for( long NumSteps=0; NumSteps < setmovesteps; NumSteps = NumSteps + 1){
    digitalWrite (directionpin, LOW);
    digitalWrite (8,HIGH);
    delayMicroseconds (Speed);
    digitalWrite(8, LOW);
    delayMicroseconds (Speed);
    buttonState = digitalRead (buttonPin);
    
    }
    done = 2;
   
    }  
    

void loop() {

 proxState = digitalRead (prox);
 Serial.println(proxState);
if (proxdone ==0){
tthome();
}
  buttonState = digitalRead (buttonPin);
  // relayState = digitalRead (relayPin);
   

   
if (buttonState == 1 && done == 0){
movehome();


  
}else if (buttonState == 0 && done == 1){

setmove();
}


//Serial.println(digitalRead(button));

  if (Serial.available() >= 0) {
  
     
     
       
       buttonStateleft = digitalRead (buttonleft);
       ttbuttonState = digitalRead(ttbutton);
       Serial.println(ttbuttonState);
      // Serial.println(buttonStateleft);
       
 if (buttonStateleft==0  && i == 2){
  
         i = 1;
          left();
          buttonStateleft = digitalRead (buttonleft);
           ttbuttonState = digitalRead(ttbutton);
       }else if (buttonStateleft ==0 && i == 1){
        i = 2;
         right();
         buttonStateleft = digitalRead (buttonleft);
         ttbuttonState = digitalRead(ttbutton);
         }else if (ttbuttonState ==0 && j==2){
          ttleft();
          j=1;
          ttbuttonState = digitalRead(ttbutton);
         }else if(ttbuttonState==0 && j==1){
          ttright();
          j=2;
          ttbuttonState = digitalRead(ttbutton);
         }
         
         
//Jog to the Left

      // }else if ( letter == '4'){
       // for( long NumSteps=0; NumSteps <= jogsteps; NumSteps = NumSteps + 1){
  // digitalWrite (directionpin, LOW);
    //digitalWrite (8,HIGH);
    //delayMicroseconds (Speed);
    //digitalWrite(8, LOW);
    //delayMicroseconds (Speed); 
    //  }
       
//Jog to the Right
      // } else if ( letter == '5'){ 
        //for( long NumSteps=0; NumSteps <= jogsteps; NumSteps = NumSteps + 1){
   //digitalWrite (directionpin, HIGH);
    //digitalWrite (8,HIGH);
    //delayMicroseconds (Speed);
    //digitalWrite(8, LOW);
    //delayMicroseconds (Speed); 
      // }
       //}
       
        
   }//void Serial bracket 
}

//}//void loop bracket

StepperControlFinal.ino (5.16 KB)

Problems are solved one at a time. Which problem do you want to start on?

Paul

The pin definitions are scatter about in the code, here they are with comments added to give a better indication of their use.

int steppin = 8;          //screw drive step
int directionpin = 9;     //screw drive direction
const int buttonPin = 10; //screw drive home switch

int buttonright = 6;      //unknown button
int buttonleft =5;        //unknown button

int ttsteppin = 3;        //turntable step
int ttdirectionpin = 2;   //turntable direction

int prox = 13;            //turntable proximity switch

//int relayPin = 12;      //relay - commented out so not used

How are you powering the stepper motors and the arduino? Stepper motors generally need more current than can be supplied by the USB cable or even worse the built-in voltage regulator on an arduino board.

Hi David, thanks for the reply

The arduino is powered by a 5v PS.
The steppers have a 24v power supply
It's for an automation project for industrial use.

I have a solid 5v going in. Roughly 4.3v going out

Could you give a description of what exactly this project is suppose to do?

The sketch is clearly nowhere near complete, in setup() there is a statement to initialize the Serial1 port, but it is not used anywhere in the sketch. There is also an if statement implying that something is expected on the Serial port connected to the computer through the USB cable, but the if statement is written incorrectly (it will always be true), and nowhere is there any attempt to actually read any input from the Serial port.

It's going to position a camera over bons for part picking with a robot arm. Another robot will load them from the turn table into an injection molding machine.

The robots are not configured yet. I'm guessing that's why communication has been written yet???

It might actually be quicker to throw that code in the trash and start afresh.

For example why is there the line

int steppin = 8;

and then further on the line

digitalWrite (8,HIGH);

And, to follow on from Reply #2, why isn't it defined as

int screwDriveStepPin = 8;

which would make things much clearer and avoid the need for a comment.

This style of programming

for( long NumSteps=0; NumSteps <= systemsteps; NumSteps = NumSteps + 1){

will block the Arduino until all those steps are complete. It's much better to have code that allows things to be tested between steps - for example to check if a limit switch has been triggered.

...R
Stepper Motor Basics
Simple Stepper Code

also look up the AccelStepper library

Planning and Implementing a Program

Thanks for the help.

Those are the things that were confusing me.

Robin2:
It might actually be quicker to throw that code in the trash and start afresh.

For example why is there the line

int steppin = 8;

and then further on the line

digitalWrite (8,HIGH);

And, to follow on from Reply #2, why isn't it defined as

int screwDriveStepPin = 8;

which would make things much clearer and avoid the need for a comment.

This style of programming

for( long NumSteps=0; NumSteps <= systemsteps; NumSteps = NumSteps + 1){

will block the Arduino until all those steps are complete. It's much better to have code that allows things to be tested between steps - for example to check if a limit switch has been triggered.

...R
Stepper Motor Basics
Simple Stepper Code

also look up the AccelStepper library

Planning and Implementing a Program

Thanks Robin, i hope i dont have to rewrite everything. I am hoping just to edit and get it working.

Thanks for the links