problems combining code

Hi I have been trying since a while already to combine these two codes. One code is to control a relay with a Bluetooth module. And the other is a stepper motor that i want to be able to switch on and off via Bluetooth.

On its own each code works as I want it but when i set them together the stepper motor first has to run its loop before the Bluetooth command takes affect.

Is there a way to have both loops run independent, instead of one after the other?

After all I want the stepper motor and another separate device be hooked up to the relay to be able to turn it on and off via Bluetooth.

And yes I have read how to combine Arduino codes, but it just wont work the way I want it to

Stepper code

/*-----( Import needed libraries )-----*/
#include <Stepper.h>

/*-----( Declare Constants, Pin Numbers )-----*/
//---( Number of steps per revolution of INTERNAL motor in 4-step mode )---
#define STEPS_PER_MOTOR_REVOLUTION 32   

//---( Steps per OUTPUT SHAFT of gear reduction )---
#define STEPS_PER_OUTPUT_REVOLUTION 32 * 64  //2048  

/*-----( Declare objects )-----*/
// create an instance of the stepper class, specifying
// the number of steps of the motor and the pins it's
// attached to

//The pin connections need to be 4 pins connected
// to Motor Driver In1, In2, In3, In4  and then the pins entered
// here in the sequence 1-3-2-4 for proper sequencing
Stepper small_stepper(STEPS_PER_MOTOR_REVOLUTION, 5, 7, 6, 8);


/*-----( Declare Variables )-----*/
int  Steps2Take;

void setup()   /*----( SETUP: RUNS ONCE )----*/
{
// Nothing  (Stepper Library sets pins as outputs)
}/*--(end setup )---*/

void loop()   /*----( LOOP: RUNS CONSTANTLY )----*/
{
  small_stepper.setSpeed(1);   // SLOWLY Show the 4 step sequence 
  Steps2Take  =  4;  // Rotate CW
  small_stepper.step(Steps2Take);
  delay(0);

  Steps2Take  =  STEPS_PER_OUTPUT_REVOLUTION / 0.2;  // Rotate CW 1/2 turn
  small_stepper.setSpeed(700);   
  small_stepper.step(Steps2Take);
  delay(000);
  
  Steps2Take  =  - STEPS_PER_OUTPUT_REVOLUTION / 0.2;  // Rotate CCW 1/2 turn  
  small_stepper.setSpeed(700);  // 700 a good max speed??
  small_stepper.step(Steps2Take);
  delay(00);

}/* --(end main loop )-- */

/* ( THE END ) */

Bluetooth code

int data;
int led1=11;
int led2=12;
int led3=13;


void setup ()
{
  pinMode(led1,OUTPUT);
  pinMode(led2,OUTPUT);
  pinMode(led3,OUTPUT);
  Serial.begin(9600);
}
 void loop()
{
  if(Serial.available())
{
// read the data
  int data = Serial.read();
  delay(100);
  if(data=='1')
    digitalWrite(led1,1);
  if(data=='2')
    digitalWrite(led1,0);
  if(data=='3')
    digitalWrite(led2,1);
  if(data=='4')
    digitalWrite(led2,0);
  if(data=='5')
    digitalWrite(led3,1);
  if(data=='6')
    digitalWrite(led3,0);
  delay(100);
  

  }
}

My combined code

#include <Stepper.h>

/*-----( Declare Constants, Pin Numbers )-----*/
//---( Number of steps per revolution of INTERNAL motor in 4-step mode )---
#define STEPS_PER_MOTOR_REVOLUTION 32   

//---( Steps per OUTPUT SHAFT of gear reduction )---
#define STEPS_PER_OUTPUT_REVOLUTION 32 * 64  //2048  

/*-----( Declare objects )-----*/
// create an instance of the stepper class, specifying
// the number of steps of the motor and the pins it's
// attached to

//The pin connections need to be 4 pins connected
// to Motor Driver In1, In2, In3, In4  and then the pins entered
// here in the sequence 1-3-2-4 for proper sequencing
Stepper small_stepper(STEPS_PER_MOTOR_REVOLUTION, 5, 7, 6, 8);

int Steps2Take;
int data;
int led1=11;
int led2=12;
int led3=13;

void setup()   /*----( SETUP: RUNS ONCE )----*/
{
  pinMode(led1,OUTPUT);
  pinMode(led2,OUTPUT);
  pinMode(led3,OUTPUT);
  Serial.begin(9600);
}
void loop()
{
  if(Serial.available())
{
// read the data
  int data = Serial.read();

  if(data=='1')
    digitalWrite(led1,1);
  if(data=='2')
    digitalWrite(led1,0);
  if(data=='3')
    digitalWrite(led2,1);
  if(data=='4')
    digitalWrite(led2,0);
  if(data=='5')
    digitalWrite(led3,1);
  if(data=='6')
    digitalWrite(led3,0);

  }
  small_stepper.setSpeed(1);   // SLOWLY Show the 4 step sequence 
  Steps2Take  =  4;  // Rotate CW
  small_stepper.step(Steps2Take);
  delay(0);

  Steps2Take  =  STEPS_PER_OUTPUT_REVOLUTION / 1;  // Rotate CW 1/2 turn
  small_stepper.setSpeed(700);   
  small_stepper.step(Steps2Take);
  delay(000);
  
  Steps2Take  =  - STEPS_PER_OUTPUT_REVOLUTION / 1;  // Rotate CCW 1/2 turn  
  small_stepper.setSpeed(700);  // 700 a good max speed??
  small_stepper.step(Steps2Take);
  delay(00);

}

Am getting desperate getting this running for a project.

Thank you

"but it just wont work the way I want it to"

What's not working?

.

Well there where delays after each direction change of the motor,
but I put them all down to 0 . But wanted to keep it in there to still have easy the option later to put a number in.

You reckon that might be a problem?

Will delete it out now to see if it makes a difference.

As I mentioned,

It only allows me to switch the relays via Bluetooth once the stepper motor has run its cycle of going back and force.

If I press turn on relay number on, for example, only once the stepper motor finishes its loop it will turn on

Sascha:
Well there where delays after each direction change of the motor,
but I put them all down to 0 . But wanted to keep it in there to still have easy the option later to put a number in.

You reckon that might be a problem?

Will delete it out now to see if it makes a difference.

As I mentioned,

It only allows me to switch the relays via Bluetooth once the stepper motor has run its cycle of going back and force.

If I press turn on relay number on, for example, only once the stepper motor finishes its loop it will turn on

Do you understand what 'blocking code' means?

.

cool, yeah that's very helpful.

So what command would i have to set in between each step so that it checks first if anything else is still to do.

I want the stepper to go cw stop, and then turn ccw. How can I give the Bluetooth in between a chance to do its thing?

Cheers

I assume you do not know what blocking code is.

Put the stepper code into a function.
Run that function on a delay which is based on millis() similar to below.
See 'Blink Whithout Delay' examples.

if (millis() - stepperMillis >= delayTime)
  {
    //code here runs every delayTime ms
    stepperMillis += delayTime; //re-initilize Timer
    steppers();   
  }

Ah yes, I understand now. Thanks guys

Now I will have to learn a bit more basic stuff, how to put place an if statement in between the steps

if (millis() - stepperMillis >= delayTime)
  {
    //code here runs every delayTime ms
    stepperMillis += delayTime; //re-initilize Timer
    steppers();   
  }

To use this I will first have to define stepperMillis and delayTime right?

"To use this I will first have to define stepperMillis and delayTime right?"

Yes

Make these global.
unsigned long stepperMillis;

unsigned long delayTime; // This can be 'unsigned int' or 'byte', make sure the 'type' can contain the number needed.

I understand what the problem is , but I don't know how to do what you said.

I know I am asking a lot, but do you think you could fix up my code to make it a non blocking code?