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..
@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.