I need your help with a problem in my software

Hi Dear Colleagues, I need your help with a problem in my software, it's very simple for you.

I have a compressor and I'm opening the drain with a solenoid valve after it turn off, but I have just one contact NO to

send me the state if it's ON or OFF.

When it's ON the contact is closed and when it's OFF the contact is open, I did the software below, but it isn't working very

well the solenoid valve is opening in diferent times please show me what I'm doing wrong. I need to turn OFF solenoide

because of the temperature so I'm opening just during five seconds. Thank You.


const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin

// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
int AVISA = 0;

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}

void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {

AVISA = 1; //move 1 para variável
}

else if (buttonState == LOW && AVISA == 1) {
// turn LED off:
digitalWrite(ledPin, LOW);
delay (5000);
digitalWrite(ledPin, HIGH);
delay (5000);
digitalWrite(ledPin, LOW);

AVISA = 0;
}

else (buttonState == LOW); {
digitalWrite(ledPin, LOW);

}

}

unnamed.jpg

You don't have any solenoid or contacts in your code. You just have a pushbutton and a LED.

Hi,
How have you got your switch connected, you will need a pullup or pulldown resistor.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png or pdf?

Tom... :slight_smile:

This is a mistake:

  else (buttonState == LOW); {
   digitalWrite(ledPin, LOW);
}

That is equivalent to:

  else {
    (buttonState == LOW);  // This expression statement does nothing.
  } 
{  // This block statement will execute regardless of the state of the button
   digitalWrite(ledPin, LOW);
}

The else clause can't include a conditional. If you want a comment there, use a comment:

  else {  // buttonState == LOW
   digitalWrite(ledPin, LOW);
}