Constant servo sweep, while stepper motor run by nunchuk button press

Is it possible to have a constant servo sweep while 2 stepper motor run forward or backward, controlled by nunchuk
What i have :

  • 2 servo motor from a printer
  • 1 servo
  • adafruits motorshield v2
  • nunchuk
  • arduino

Down below the scetch i use, but with this, only the servo sweeps. If i push the buttons or the joystick nothing happens.
If I remove the sweep function the stepper motor runs.
I tired to look at the " blinkwithoutdelay " and " state machine " with out sucess. I am a newbie in the writing programs.

The goal with this is, to make a " robotcar " first controlled by nunchuck and the driven by it own. The servo getting later a ultrasonic/sonar distance sensor on top, therefor the sweep.

Hop some one kan give hint or somthing i can work futher one.

wtih greetings Gregor

#include <AccelStepper.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
#include <ArduinoNunchuk.h>
#include <Servo.h> 

Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 

ArduinoNunchuk nunchuk = ArduinoNunchuk();

Servo servo1;
int pos = 30;    // variable to store the servo position 

// Connect two steppers with 200 steps per revolution (1.8 degree)
// to the top shield
Adafruit_StepperMotor *myStepper1 = AFMS.getStepper(200, 1);
Adafruit_StepperMotor *myStepper2 = AFMS.getStepper(200, 2);


// you can change these to DOUBLE or INTERLEAVE or MICROSTEP!
// wrappers for the first motor!
void forwardstep1() {  
  myStepper1->onestep(FORWARD, INTERLEAVE);
}
void backwardstep1() {  
  myStepper1->onestep(BACKWARD, INTERLEAVE);
}
// wrappers for the second motor!
void forwardstep2() {  
  myStepper2->onestep(FORWARD, INTERLEAVE);
}
void backwardstep2() {  
  myStepper2->onestep(BACKWARD, INTERLEAVE);
}


// Now we'll wrap the 2 steppers in an AccelStepper object
AccelStepper stepper1(forwardstep1, backwardstep1);
AccelStepper stepper2(forwardstep2, backwardstep2);


void setup()
{ 
  servo1.attach(9); 

  nunchuk.init(); // nunchuk

  AFMS.begin(); // Start the motor shield

  stepper1.setMaxSpeed(200.0);
  //stepper1.setAcceleration(100.0);
  stepper1.moveTo(50000);

  stepper2.setMaxSpeed(200.0);
  //stepper2.setAcceleration(100.0);
  stepper2.moveTo(50000);
}

void zButton() // forward
{ 
  if (nunchuk.zButton == 1)
  {  
    stepper1.setSpeed(200);   
    stepper1.run();
    stepper2.setSpeed(200);   
    stepper2.run();

  }
  if (nunchuk.zButton == 0)
  { 
    stepper1.stop();
    stepper2.stop();
  }
}

void cButton() // backward
{ 
  if (nunchuk.cButton == 1) 

  {  
    stepper1.setSpeed(-200);   
    stepper1.run();
    stepper2.setSpeed(-200);   
    stepper2.run();
  }
  if (nunchuk.cButton == 0)
  { 
    stepper1.stop();
    stepper2.stop();
  }

}

void sweep() // servo
{ 

  for(pos = 30; pos < 150; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    servo1.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);  
  } 

  for(pos = 150; pos > 30; pos -= 1)     // goes from 180 degrees to 0 degrees 
  {                                
    servo1.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 

} 


void loop()

{   
  nunchuk.update();
  int analogYraw = nunchuk.analogY;
  int analogXraw = nunchuk.analogX;
  zButton();
  cButton();
  sweep();

  if (analogXraw < 40)

  {  
    stepper1.setSpeed(-100);   
    stepper1.run();
    stepper2.setSpeed(100);   
    stepper2.run();
  }

  if (analogXraw > 230)

  {  
    stepper1.setSpeed(100);   
    stepper1.run();
    stepper2.setSpeed(-100);   
    stepper2.run();
  }


}

Please edit your post to put your code within code tags as explained in the How to use the forum

If you want mutiple things to happen at the same time you need to master the Blink Without Delay technique. I wrote an extended demo in the first post of this Thread. It may help to explain things.

...R

Thanks, I will look again to the Blink Without Delay technique.

with greetings Gregor

it work with the Blink Without Delay technique. I build the sketch on a breadboard. That helps to understand.
@Robin2 thanks for your advice/tutourial

with greetings Gregor