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