How this project works:
I'm making a controller for two 400v motors by steering contactors with arduino relays. My plan is to use four buttons (two NC (normally closed, off) buttons and two NO (normally open, on) buttons connected to four digital pins (input). one set of buttons (one NC and one NO) steers the first motor, the other steers the second. The buttons steering motor 1 is wired from +5v on the arduino to digital pins 10 (off button) and 11 (on button). same for motor 2 only that it's pin 12 and 13 (same button order). Motor 1 will be able to reverse with an off delay of 2 seconds while switching to contactor 2 which has L1, L2 and L3 (400v fases) in a different order (L3, L2, L1), details of how this works is'nt really important, the point is that motor 1 uses two contactors, one for reverse and one for default rotation, and that i'll be able to switch between these with the arduino relays 5 and 6 by pressing the different buttons. Relay one (Relay_m1, pin 5) is the"delay" relay, connected from the L supply fase (230v) through it's NO (normally open) side directly to relay two (relay_m1s, pin 6), NC is connected to nothing, this will be its "off" state. On relay two NO is connected to A1 on contactor one (which steers contactor one, deafult rotation contactor), and NC connected to A1 on contactor two (which is the reverse rotation contactor). For motor 2, its only one relay, NO connected to it's contactor and NC connected to nothing (off state).
How i would be able to start motor one and reverse it:
-After one button press (on button), motor one will start (default rotation)
-After a second press, it'll turn off the motor, stay off in two second while reversing and turn on
again (while still in reverse)
-After a third press, it'll turn off again, stay off in two second while changing to default rotation
and turn on
-it'll switch between the two last statements (reverse and default rotation with 2 seconds delay)
after the next presses
-if the off button gets pressed, it'll turn of independent of which way it's rotating
The problem:
My problem is that it's defaulting to m1_state "1" instead of "0". I have no way of changing states either. I can asure that i've connected things properly, im also only in the test stage of the project so its kinda hard to miss any connection problems i could be having. I'm using pull-down resistors, so that's not the problem either. It has to do with my code. I would like to use "switch/case" statements, i personally think it's easier using states within cases instead of if/else containing states. So if anyone could help me with setting up cases for the code, i'd really appreciate it.
My code:
int m1_state = 0;
int m2_state = 0;
int relay_m1 = 5;
int relay_m1s = 6;
int relay_m2 = 7;
int s1 = 10;
int s2 = 11;
int s1_2 = 12;
int s2_2 = 13;
void setup()
{
Serial.begin(9600);
pinMode(relay_m1,OUTPUT);
pinMode(relay_m1s,OUTPUT);
pinMode(relay_m2,OUTPUT);
pinMode(s1,INPUT);
pinMode(s2,INPUT);
pinMode(s1_2,INPUT);
pinMode(s2_2,INPUT);
}
void loop()
{
Serial.print("m1 state: ");
Serial.print(m1_state);
Serial.print(" | m2 state: ");
Serial.println(m2_state);
delay(500);
if (m1_state == 1 || m1_state == 2 || m1_state == 3 && (digitalRead(s1) == LOW))
{
m1_state = 0;
}
if (m1_state == 0 && (digitalRead(s2) == HIGH))
{
m1_state = 1;
}
else if (m1_state == 1 || m1_state == 3 && (digitalRead(s2) == HIGH))
{
m1_state = 2;
}
else if (m1_state == 2 && (digitalRead(s2) == HIGH))
{
m1_state = 3;
}
//__________________________________________________
if (m2_state == 1 && (digitalRead(s1_2) == LOW))
{
m2_state = 0;
}
else if (m2_state == 0 && (digitalRead(s2_2) == HIGH))
{
m2_state = 1;
}
//__________________________________________________
if (m1_state == 0)
{
digitalWrite(relay_m1,LOW);
digitalWrite(relay_m1s,HIGH);
}
else if (m1_state == 1)
{
digitalWrite(relay_m1,HIGH);
digitalWrite(relay_m1s,HIGH);
}
else if (m1_state == 2)
{
digitalWrite(relay_m1,LOW);
delay(1000);
digitalWrite(relay_m1,LOW);
digitalWrite(relay_m1s,LOW);
delay(1000);
digitalWrite(relay_m1,HIGH);
digitalWrite(relay_m1s,LOW);
}
else if (m1_state == 3)
{
digitalWrite(relay_m1,LOW);
delay(1000);
digitalWrite(relay_m1,LOW);
digitalWrite(relay_m1s,HIGH);
delay(1000);
digitalWrite(relay_m1,HIGH);
digitalWrite(relay_m1s,HIGH);
}
//__________________________________________________
if (m2_state == 0)
{
digitalWrite(relay_m2, LOW);
}
else if (m2_state == 1)
{
digitalWrite(relay_m2, HIGH);
}
}