latching an if statement?

Bare with me as this is the first post.
The code that i am trying to achieve looks at the speed of a motor driving a pump, if the RPM goes above a threshold it illuminates a green LED, if it goes to another threshold it illuminates an amber LED and if it goes to a unsafe level it trips" the system, red LED illuminates and opens a safety relay which then shuts down the pump. The code then stays in this “tripped” status until a reset button is pressed. The original code uses a frequency signal however to test i have wired a 10k pot, so have commented out the code around the frequency. The LEDs are lighting and the relay is switching but the state is not latching, any help?

//#include <FreqCount.h>
//const int sensorPin = 6;
int RelayPin = 5;
int latch;
int reset;
int reset_pin = 7;
int x;
int red = 2;
int amber =3;
int green =4;
void setup() {
Serial.begin(57600);
pinMode(sensorPin, INPUT);
// FreqCount.begin(1000);
pinMode(reset_pin,INPUT);
// for (int pinNumber = 2; pinNumber < 5; pinNumber++) {
// pinMode(pinNumber, OUTPUT);
// digitalWrite(pinNumber, LOW);
pinMode( RelayPin, OUTPUT);

//}
}
void loop() {
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
// print out the value you read:
Serial.println(voltage);
Serial.println(latch);
reset=digitalRead(reset_pin);
// if (FreqCount.available()) {
// unsigned long count = FreqCount.read();
// unsigned long x = count / 6;
// Serial.print(", Frequency Hz: ");
// Serial.println(x);

// if the current frequency is lower than the baseline turn off all LEDs
if (voltage < 1) {
digitalWrite(red, LOW);
digitalWrite(amber, LOW);
digitalWrite(green, LOW);
digitalWrite(RelayPin, HIGH);
} // if the frequency rises 20-40 Hz, turn green LED on
else if (voltage >= 1 && voltage < 3 && latch == LOW) {
digitalWrite(red, HIGH);
digitalWrite(amber, LOW);
digitalWrite(green, LOW);
digitalWrite(RelayPin, HIGH);
} // if the frequency rises 40-60 Hz, turn amber LED on
else if (voltage >= 3 && voltage < 4 && latch == LOW) {
digitalWrite(red, LOW);
digitalWrite(amber, HIGH);
digitalWrite(green, LOW);
digitalWrite(RelayPin, HIGH);
} // if the frequency rises more than 60 Hz, turn red LED on
else if (voltage >= 4.5 || latch == HIGH) {
digitalWrite(red, HIGH);
digitalWrite(amber, LOW);
digitalWrite(green, LOW);
digitalWrite(RelayPin, LOW);
digitalWrite(latch,HIGH);
} // if reset button is pressed and speed is below setpoint reset the interlock
else if (voltage <= 4 && reset == HIGH) {
digitalWrite(latch,LOW);
}
delay(1);

}

Please use code tags, whenever you post code. It’s the </> button in the top left corner.
What do you mean by latching the state? Also I can’t see you changing the value of latch anywhere.

Your code has an int latch variable with initial value 0. And then your code does things like

digitalWrite(latch,HIGH);
...
digitalWrite(latch,LOW);

What are you trying to achieve by this? I.e. you are doing digitalWrite through a pin #0. Why?

It appears that you intended to use latch as an ordinary variable. But instead for some unexplainable reason you are trying to do digitalWrite(latch,. Why?

Thanks for the replies,
LightuC
“What do you mean by latching the state? Also I can’t see you changing the value of latch anywhere.”

by latch i mean that once the voltage rises beyond 4.5v it stays in that state i.e it latches in, until a reset button is pressed, i.e if the voltage rises above 4.5v then it will illuminate the red LED symbolising a trip,and open the relay stopping the pump, it remains in this state with the LED illuminated and the relay open until the voltage falls below 4.5v and the reset button is pressed.

  else if (voltage >= 4.5 || latch == HIGH) {
    digitalWrite(red, HIGH);
    digitalWrite(amber, LOW);
    digitalWrite(green, LOW);
    digitalWrite(RelayPin, LOW);
    digitalWrite(latch,HIGH);
    Serial.println(latch);
  } 
  // if reset button is pressed and speed is below setpoint reset the interlock
     else if (voltage <= 4 && reset == HIGH) {
    digitalWrite(latch,LOW);
  }

i thought here i was changing the state of the latch by digitalWrite HIGH/LOW? please correct me if i am wrong?

Montmorency

"What are you trying to achieve by this? I.e. you are doing digitalWrite through a pin #0 and it seems that you expect that pin to maintain its last written state. Why and how?

It appears that you intended to use latch as an ordinary variable. But instead for some unexplainable reason you are trying to do digitalWrite(latch,. Why?"

int latch;

here i do not think i am setting it up as pin 0? but again i may be wrong, i am trying to set it up as a variable i guess so that it is not a physical IO but used by the program to maintain a state until a reset button is pressed even after the voltage has dropped below 4.5v.

any help is much appreciated.

Bare with me

Ask AWOL

adeery1:
Bare with me

Never on a first date.

digitalWrite(2,LOW);  //Yes
digitalWrite(3,LOW);  //Yes
digitalWrite(4,LOW);  //Yes
digitalWrite(5,LOW);  //Yes
digitalWrite(6,LOW);  //Yes
digitalWrite(7,LOW);  //Yes
digitalWrite(8,LOW);  //Yes
digitalWrite(9,LOW);  //Yes
digitalWrite(10,LOW);  //Yes
digitalWrite(11,LOW);  //Yes
digitalWrite(12,LOW);  //Yes
digitalWrite(13,LOW);  //Yes
digitalWrite(14,LOW);  //Yes
digitalWrite(15,LOW);  //Yes
digitalWrite(16,LOW);  //Yes
digitalWrite(17,LOW);  //Yes
digitalWrite(18,LOW);  //Yes
digitalWrite(19,LOW);  //Yes

digitalWrite(latch,LOW); //No

latch = LOW; // Yes

i have solved this one myself,

Congratulations! You are turning into a programmer. :slight_smile:

int latch;
...
void setup() {
...
...
  digitalWrite(latch,LOW);
}

Oops