Relay won't shut off

I have written a script that looks at 3 inputs and if they are all high then it should turn on a relay and then turn it off after 1 second. It then starts a timer to create a delay before turning a second relay on and then off after 1 second. It then repeats the loop. It is turning on the relay but then just leaves it on and then nothing else occurs. The code appears to be looping because if I trigger one of the 3 inputs, it responds as expected. I can't for the life of me figure out why it is hanging up. Please help.

Here is my code:


int relay3=5;
int relay4=4;
int faultLED=7; //this is relay1 on the relay shield
int scaleFactor=1000; // this converts seconds to miliseconds
int switchDelay=1000; //length of button press in miliseconds
unsigned long altDelay=90; //interval between button presses in seconds
int sovr1=1;
int sovr2=2; 
int sovr3=3;
int toggleState=0;
unsigned long startDelay;


void setup() {
  // put your setup code here, to run once:
pinMode(relay3,OUTPUT);
pinMode(relay4,OUTPUT);
pinMode(faultLED,OUTPUT);
pinMode(sovr1,INPUT_PULLUP);
pinMode(sovr2,INPUT_PULLUP);
pinMode(sovr3,INPUT_PULLUP);
}

void loop() {
  // put your main code here, to run repeatedly:

if (digitalRead(sovr1)==LOW || digitalRead(sovr2)==LOW || digitalRead(sovr3)==LOW){
digitalWrite (faultLED,HIGH);
exit(0); //this makes the fault permanent till a reset occurs
}

else if (toggleState==0){
digitalWrite (relay3,HIGH); //pressing selection 1 on first machine
delay(switchDelay);
digitalWrite(relay3,LOW); //releasing selection 1 on first machine
toggleState == 1;
startDelay == millis();
}

else if(toggleState == 1 && (millis()-startDelay) >= (altDelay * scaleFactor)){
toggleState == 2;  
}

else if(toggleState == 2){
digitalWrite (relay4,HIGH); //pressing selection 2 on first machine
delay (switchDelay);
digitalWrite (relay4,LOW);  //releasing selection 2 on first machine
toggleState == 3;
startDelay == millis();
}

else if (toggleState == 3 && (millis()- startDelay) >= (altDelay * scaleFactor)){
toggleState == 0;
}

}

toggleState == 1;
startDelay == millis();

OR

toggleState = 1;
startDelay = millis();

Hello justinpigott
It seem to be a school asignment, isn´t it?
I have seen a lot of these school asignments here. Take some time and use the Serach Forum function to get some ideas for a proper solution.
Have a nice day and enjoy coding in C++.

No. Not a school project. I have been out of school for 20 years. This is a work project. I don't usually need to write code but needed to in this instance. I don't see anything wrong with the code and the place it is hanging up seem the most unlikely spot to hang up on. I have spent the last few hours trying to figure out what is going wrong. If you see my error please help a brother out.

That was it. I knew it was something stupidly simple. I appreciate your help.

Hello justinpigott
Replace all delay()´s by a timer function.
Design a FSM to simplify the sketch, using the switch case statement, to handle the "toggle states".
Have a nice day and enjoy coding in C++.

Hi,
See what @LarryD said:

This is comparison and not assignment

.

startDelay == millis(); Checks if startDelay is equal to millis();
startDelay = millis(); startDelay takes the value of millis();

toggleState == 0;
toggleState == 1;
toggleState == 2;
toggleState == 32;
toggleState == ?; Checks if toggleState is equal to ?;
toggleState = ?; toggleState receives the value of ?;

One of the most common mistakes that new programmers make is to confuse the assignment operator (= ) with the equality operator (== ). Assignment (= ) is used to assign a value to a variable. Equality (== ) is used to test whether two operands are equal in value.

Ref: https://www.learncpp.com/cpp-tutorial/variable-assignment-and-initialization/

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.