Help: Stepper motor and relay control from one Arduino

Hey guys, I am doing a project where I have to switch on 3 push pull magnet one by one in a loop with some delay in between on and off with the help of arduino relay module connected to arduino board. And in between the states on and off of these relays I am running my stepper motor to a couple of degrees CW and CCW.
I am new to this field and took lessons online to program my stepper motor which runs the way I want to in terms of speed and degree.

I came up with the two separate codes one for the relay and for the motor. I need to combine them to one code so that it turns on the relay1 makes the defined motor movement turns off the relay1 after some seconds. till the third relay operation and so on in a loop...

If anyone can help me combining the relay and stepper motor code as one single code, I'd be obliged..

MydegStepper.ino (857 Bytes)

relay.ino (883 Bytes)

http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html

Thank you Grumpy_Mike, I don't have much experience with the programming but I hope it will help somehow.

You may also find some useful ideas in Several Things at a Time

...R

@Robin2
Thanks for your help. I tried reading your thread. It was helpful.
But regarding this project I end up combining the codes but the problem is I want my motor to run between the time when the relays are on

#define DIR_PIN 8
#define STEP_PIN 9
#define RELAY1  2                        
#define RELAY2  3                        
#define RELAY3  4 

void setup() {
  setupMotor();
  setupRelay();
}

void loop() {
  loopMotor();
  loopRelay();
}

void setupRelay()
{    
// Initialise the Arduino data pins for OUTPUT
  pinMode(RELAY1, OUTPUT);       
  pinMode(RELAY2, OUTPUT);
  pinMode(RELAY3, OUTPUT);

}

void setupMotor() { 
  pinMode(DIR_PIN, OUTPUT); 
  pinMode(STEP_PIN, OUTPUT);
  
} 


void loopRelay()
{
   digitalWrite(RELAY1,LOW);           // Turns ON Relays 1
   delay(3000);                                      // Wait 3 seconds
   digitalWrite(RELAY1,HIGH);          // Turns Relay Off
 
   digitalWrite(RELAY2,LOW);           // Turns ON Relays 2
   delay(3000);                                      // Wait 3 seconds
   digitalWrite(RELAY2,HIGH);          // Turns Relay Off
 
   digitalWrite(RELAY3,LOW);           // Turns ON Relays 3
   delay(3000);                                      // Wait 3 seconds
   digitalWrite(RELAY3,HIGH);          // Turns Relay Off
 
   
 }
void loopMotor(){ 

  //rotate a specific number of degrees 
  rotateDeg(180, .2); 
  delay(1000);

  rotateDeg(-180, .09);  //reverse
  delay(2000); 
}

void rotateDeg(float deg, float speed){ 
  //rotate a specific number of degrees (negitive for reverse movement)
  //speed is any number from .01 -> 1 with 1 being fastest - Slower is stronger
  int dir = (deg > 0)? HIGH:LOW;
  digitalWrite(DIR_PIN,dir); 

  int steps = abs(deg)*(2/0.225);
  float usDelay = (1/speed) * 70;

  for(int i=0; i < steps; i++){ 
    digitalWrite(STEP_PIN, HIGH); 
    delayMicroseconds(usDelay); 

    digitalWrite(STEP_PIN, LOW); 
    delayMicroseconds(usDelay); 
  } 
}

It executes properly but the motor runs CW and CCW then waits for approx 10sec to make another rotation. This is quite clear that in between these 10secs the 3 electromagnets are getting on and off. How can I modify this code so that the relay1 is on, the motor rotates, relay1off. relay2 is on... and so on..

I'd be a great help if you can guide me through this.

...Ajay :slight_smile:

I'd be a great help if you can guide me through this.

You are going to have to get over the use of delay(). Pretend that that useless function did not even exist.

How would YOU perform the task that you want the Arduino to perform. Write out the steps exactly as you would perform them.

When you can do that, writing the code is trivial.

ajayr14:
Thanks for your help. I tried reading your thread. It was helpful.
But regarding this project

That, together with @PaulS's comment suggests that you have not applied to your project the techniques from my tutorial - which is what I had in mind.

...R

@PaulS

You are going to have to get over the use of delay(). Pretend that that useless function did not even exist.

Thank you for the valuable info, I will lern these new operators other than delay and then write them back.. Thanks again..

@Robin2

That, together with @PaulS's comment suggests that you have not applied to your project the techniques from my tutorial - which is what I had in mind.

I will read your post again carefully and will try to implement the techniques you mentioned. Thanks a lot..

One last question, I was wondering if it's possible to control two drivers with arduino? I mean Big easy driver for motor and relay module..

ajayr14:
One last question, I was wondering if it's possible to control two drivers with arduino? I mean Big easy driver for motor and relay module..

That would be no problem. An Uno could easily control half a dozen devices.

...R