Stepper and Servo weird code issue. Running simultaneously?

Hey all,
I’ve got a question that I think might be a quick fix but I can’t seem to get it myself.
Below is my mostly complete code.
I have a stepper running off an H-bridge (pins 2,3,4,5) and 2 servos (pin 10 and pin 11).

I need the stepper to run and stop then let the servo turn and stop…etc.
Problem is as soon as the code starts everything starts going. The stepper runs and both servos start turning. Delays are skipped and everything. I’m not even sure if they are reaching their final positions because I have to yank the power as soon as that starts (to prevent damage).

Am I missing something here? I have a vague feeling it’s a timer issue but my searches aren’t coming up with much.

 #include <Stepper.h>
#include <Servo.h> 

#define motorSteps 200     // change this depending on the number of steps

Servo chopSawServo;  // create servo object to control a servo  
Servo clawServo;  // create servo object to control a servo 
int pos = 0;    // variable to store the servo position 

int triggerInput = 1;
#define motorPin5 2
#define motorPin6 3
#define motorPin7 4
#define motorPin8 5
int dualBlades = 6;
int nookMotor = 7;
int nookLight = 8;
int chopSaw = 9;
int chopSawLight = 12;
int dusterLight = 13;
int blindLight = 14;
int dualBladesLight = 15;
int dusterMotor = 16;



// initialize of the Stepper library:


Stepper stepperSliderSlow(motorSteps,2,3,4,5);
Stepper stepperSliderFast(motorSteps,2,3,4,5);

void setup() {
  
  // set the motor speed...
  stepperSliderSlow.setSpeed(18);
  stepperSliderFast.setSpeed(70);
  
   chopSawServo.attach(10);  // attaches the servo on pin 10 to the servo object
   clawServo.attach(11);  // attaches the servo on pin 11 to the servo object

pinMode(triggerInput, INPUT);
pinMode(dualBlades, OUTPUT);
pinMode(nookMotor, OUTPUT);
pinMode(nookLight, OUTPUT);
pinMode(chopSaw, OUTPUT);
pinMode(chopSawLight, OUTPUT);
pinMode(dusterLight, OUTPUT);
pinMode(blindLight, OUTPUT);
pinMode(dualBladesLight, OUTPUT);
pinMode(dusterMotor, OUTPUT);
  

}

void loop() {
  

  
  //dual blade cut//
  //////////////////
  
  digitalWrite(dualBlades, LOW);
  digitalWrite(dualBladesLight, HIGH);
  
  
  stepperSliderSlow.step(360);
  delay(12000);
  
  ////////first claw rotate to chop saw
  /////////////////////////////////////
  
   for(pos = 0; pos <= 88; pos += 1) // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    chopSawServo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(30);                       // waits 15ms for the servo to reach the position 
  }
  
   digitalWrite(dualBlades, LOW);
  digitalWrite(dualBladesLight, LOW);
  
  
  delay(15000); 
  
  
  
 
  
  
  
  

 /////drop and raise chop saw/////
 ///////////////////////
 
 digitalWrite(chopSawLight, HIGH);
 
 for(pos = 0; pos <= 110; pos += 1) // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    chopSawServo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(50);                       // waits 15ms for the servo to reach the position 
  }
 
  for(pos = 110; pos>=0; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    chopSawServo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(30);                       // waits 15ms for the servo to reach the position 
  } 
  
  
   digitalWrite(chopSawLight, LOW);



///////second rotate to duster//////////
//////////////////////////

digitalWrite(dusterLight, HIGH);

 for(pos = 88; pos <= 177; pos += 1) // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    chopSawServo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(30);                       // waits 15ms for the servo to reach the position 
  }
  
  
    delay(15000);
    
    
    
  stepperSliderSlow.step(300);
  delay(5000); 
  
  
  digitalWrite(dusterLight, LOW);
 
 //////////return claw home/////////
 ///////////////////////////////////
 
  
 
}

Something else of note, not only is the first long delay(12000) being skipped but the servo speed of (30) is not being abided to either. The servo is running at a very fast rate.
Really weird.

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png or pdf?

How are you powering your motor and servo?

Tom.... :slight_smile:

delay(12000);

This has got to GO.

EVERY use of the delay() function has got to GO

The delay() function prevents the Arduino from doing anything else until the wait is over.

Look at how timing is managed using millis() in the demo several things at a time

I see the word "chopsaw" which suggests to me this is not a toy project. If so I suggest you replace the H-bridge stepper drivers with specialized stepper drivers. See stepper motor basics.

...R

TomGeorge:
Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png or pdf?

How are you powering your motor and servo?

Tom.... :slight_smile:

Tom, I can try getting a picture to you later but I doubt it would be of much use. It's a great tangle of wires flying everywhere :sweat_smile: I am powering the arduino from a 9v power supply, the stepper is being controlled from a 12v power supply and the servo from the save 12v supply with a 5v regulator in the circuit. Everything tests out great alone. I can move the stepper fine, the servo works fine, then when I try combining them in the same code it's a mess.

Hi, have you got all the earths connected together and are you using decent gauge wire where power supply is concerned?

Tom..... :slight_smile:
Circuit diagram is essential, if you are to follow how your connections.
If you do not have a diagram, then draw one now, reverse engineer, you may find your problem as you do it.

Robin2:

delay(12000);

This has got to GO.

EVERY use of the delay() function has got to GO

The delay() function prevents the Arduino from doing anything else until the wait is over.

Look at how timing is managed using millis() in the demo several things at a time

I see the word "chopsaw" which suggests to me this is not a toy project. If so I suggest you replace the H-bridge stepper drivers with specialized stepper drivers. See stepper motor basics.

...R

Really though I was using the delay() because I didn't want anything to happen. I just need the thing to silently sit there for 12 seconds, delay(12000); Running things simultaneously is actually what I don't want except it's happening!! :stuck_out_tongue_closed_eyes:

As for the chop saw part, this is actually a sculpture signed to look like a real saw. Looks dangerous but in reality is not. It's an overgrown toy. It's going behind a sheet of plexi anyways to keep away curious fingers.

TomGeorge:
Hi, have you got all the earths connected together and are you using decent gauge wire where power supply is concerned?

Tom..... :slight_smile:
Circuit diagram is essential, if you are to follow how your connections.
If you do not have a diagram, then draw one now, reverse engineer, you may find your problem as you do it.

Sorry, I did mean to mention the grounds were all tied together. Also 10k pulldowns on the signal line of the servos just to see if that helped, it did not. I'll take a closer look at my circuit. I was thinking / hoping this was just a code issue though...

tatticus:
Really though I was using the delay() because I didn't want anything to happen. I just need the thing to silently sit there for 12 seconds, delay(12000); Running things simultaneously is actually what I don't want except it's happening!!

I guess I saw "chopsaw" and got frightened. I assumed you would need to retain control during the delay() period.

Nice image.

Can you put some Serial.printl() messages into your code so you can see what stage the code is at when different things are happening.

Also, organize things mechanically so no damage will happen if the code is wrong as that will allow you time to see what the code actually does.

Is it possible that the servos are just moving to their initial positions when power is connected, and before the Arduino takes control?

...R

Robin2:
I guess I saw “chopsaw” and got frightened. I assumed you would need to retain control during the delay() period.

Nice image.

Can you put some Serial.printl() messages into your code so you can see what stage the code is at when different things are happening.

Also, organize things mechanically so no damage will happen if the code is wrong as that will allow you time to see what the code actually does.

Is it possible that the servos are just moving to their initial positions when power is connected, and before the Arduino takes control?

…R

Yes, when one sees the words ‘chop saw’ and ‘dual blades’ they should be concerned i guess :wink:

I’ll need to look into Serial.print() It’s not something I’ve needed to use much in the past.

As far as the servos moving to their initial position on power connection, that’s unfortunately not the case. When I strip the code of all references to the stepper motor it runs fine. When I do the revers and strip the code of all references to the servos the stepper then runs fine. The code with both the servos and steppers fails. It’s as if the code is for some reason causing BOTH servos to immediately jump to action, skipping all delays etc.

Really aggravating. Worst case scenario I might need to move the servos to a separate board and just have it triggered from the main board that has the stepper on it. It’s a kludgy hackish solution at best but I’ve got a deadline looming and I’m silently freaking out :cold_sweat:

I guess it's possible that the servo library and the stepper library are not compatible. This is a common problem with Arduino libraries. Try the ServoTimer2 library and see if that makes a difference. I will try to experiment with the stepper and servo libraries tomorrow.

You could control the stepper motors without any library but that would be very much easier if you had a specialized stepper driver that just needs step and direction signals from the Arduino.

...R

I face the same issue here, I also try to program servo without library but the result is still the same. Please reply if you already have the solution.
The coding work just fine when I'm running it separately. Only when I'm combine it, it become weird