int relay1 = 2;
int relay2 = 3;
int Limit1 = 4;
int Limit2 = 5;
const int triggerType = LOW; // your relay type
int relayOFF, relayON; // relay ON and OFF values for LOW and HIGH Trigger relays
void setup() {
pinMode(relay1, OUTPUT); // set pin as output for relay 1
pinMode(relay2, OUTPUT); // set pin as output for relay 2
pinMode(Limit1, INPUT); // set pin as input for Limit switch 1
pinMode(Limit2, INPUT); // set pin as input for Limit switch 2
if (triggerType == LOW) {
relayON = LOW;
relayOFF = HIGH;
digitalWrite(relay1, relayOFF); //keep both relay OFF so motor is stopped
digitalWrite(relay2, relayOFF); //keep both relay OFF so motor is stopped
} else {
relayON = HIGH;
relayOFF = LOW;
digitalWrite(relay1, relayOFF); //keep both relay OFF so motor is stopped
digitalWrite(relay2, relayOFF); //keep both relay OFF so motor is stopped
}
Serial.begin(9600); // initialize serial monitor with 9600 baud
Serial.println("Robojax Motor Direction of Rotation");
Serial.println("Using 2 Relays");
delay(2000);
}
void loop() {
// Rotate in CCW direction
motorCCW();
while (1)
{
int state1 = digitalRead(Limit1);
while (state1 == HIGH && digitalRead(Limit2) == LOW) {
motorCCW();
// delay(3000); // wait for 3 seconds
if (digitalRead(Limit2) == LOW) {
continue;
} else {
motorStop();
break;
// delay(2000);
}
}
int state2 = digitalRead(Limit2);
while (state2 == HIGH && digitalRead(Limit1) == LOW) {
// Rotate in CW direction
motorCW();
// delay(3000); // wait for 3 seconds
if (digitalRead(Limit1) == LOW) {
continue;
} else {
motorStop();
break;
// delay(2000);
}
}
Serial.println("===============");
}
Serial.println("OUT");
} // loop end
/*
motorCCW()
controls the relay so the motor rotates in CCW
*/
void motorCCW() {
digitalWrite(relay1, relayON); // turn relay 1 ON
digitalWrite(relay2, relayOFF); // turn relay 2 OFF
Serial.println("Rotating in CCW");
} // motorCCW()
/*
motorCW()
controls the relay so the motor rotates in CW
*/
void motorCW() {
digitalWrite(relay1, relayOFF); // turn relay 1 OFF
digitalWrite(relay2, relayON); // turn relay 2 ON
Serial.println("Rotating in CW");
} //motorCW()
/*
motorStop()
controls the relay so the motor is stopped
*/
void motorStop() {
digitalWrite(relay1, relayOFF); // turn relay 1 OFF
digitalWrite(relay2, relayOFF); // turn relay 2 OFf
Serial.println("Stopped");
}