Go Down

Topic: How to code for dry contact open/close not in loop (Read 601 times) previous topic - next topic

MKSaeed

with this code I got one time "open" when button release but when press don't for to next IF statement to write "close"  why?

Code: [Select]
int Switch = 2;
int state = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(Switch, INPUT);
}
void loop()
{
  if (state == 0){
  if (digitalRead(Switch) == LOW) {
    state = 1;
    Serial.print("open");
  }
 
 if (state == 1 && digitalRead(Switch) == HIGH) {   
    state =0;
    Serial.print("close");

  }
  }
   
}

outsider

#16
Nov 23, 2018, 07:04 pm Last Edit: Nov 23, 2018, 07:12 pm by outsider
See if this code works.
Code: [Select]


// this constant won't change:
const byte  buttonPin = 2;    // the pin that the pushbutton is attached to
const byte ledPin = 13;       // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
bool buttonState = false;         // current state of the button
bool lastButtonState = false;     // previous state of the button

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT_PULLUP);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;


  // turns on the LED every four button pushes by checking the modulo of the
  // button push counter. the modulo function gives you the remainder of the
  // division of two numbers:
  if (buttonPushCounter % 4 == 0) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }

}

outsider

#17
Nov 23, 2018, 07:20 pm Last Edit: Nov 23, 2018, 07:45 pm by outsider
Don't know who made that stupid Fritz picture, if the pushbutton is inserted sideways, the button will still work, but there is NO pulldown.
                                                                                Best way, orientation don't matter.

MKSaeed

@outsider

what if I have button with two pins how would be the resistor connection?


sherzaad

what if I have button with two pins how would be the resistor connection?


if you can't figure that out, you have need to go back to and learn how to build basic electronic circuits!

MKSaeed

if you can't figure that out, you have need to go back to and learn how to build basic electronic circuits!
I know but because I followed the sketch before with 4 buttons and need to make sure. I connected with one pin with 5 V and pin #2 connected with control and resistor, second leg of resistor with ground!


MKSaeed

I did this code and now I can when I press one time will write "open" one time without loop.

Code: [Select]
int Switch = 2;

int buttonState;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup()
{
Serial.begin(9600);

  pinMode(Switch, INPUT);
  Serial.begin(9600);

}
void loop()
{
buttonState = digitalRead(Switch);

 if (digitalRead(Switch) == HIGH && buttonState != lastButtonState) {
   
  Serial.println("Open");
delay(200);

  }


 }


now I want when release the button write"close" one time every release!

MKSaeed

Thanks for all. Now I made it like this and I got what I want from it:

Code: [Select]
int Switch = 2;  //Pin for sensor switch

int buttonState;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup()
{
Serial.begin(9600);

  pinMode(Switch, INPUT);
  Serial.begin(9600);

}
void loop()
{
buttonState = digitalRead(Switch);

 if (digitalRead(Switch) == HIGH && buttonState != lastButtonState) {
   
  Serial.println("open");
  delay(180);

  }
      lastButtonState = buttonState;

  if  (digitalRead(Switch) == LOW && buttonState == 1){
    Serial.println("Close");
    delay(200);

  }


 }

Go Up