Go Down

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

MKSaeed

I have a dry contact switch want to use it as door sensor if door open then send "open" in serial one time

I tried this code but it gives me open in loop! how can I make it one time when switch on or door opened

Code: [Select]

byte Switch = 2;

void setup()
{
  pinMode(Switch, INPUT);
  Serial.begin(9600);
}
void loop()
{

  if (digitalRead(Switch) == HIGH) {
    Serial.println("Open");
    delay(10);
  }
  else {
        Serial.println("Close");

  }
}


Thanks for all


MKSaeed


Grumpy_Mike

How is the switch wired?
Your code assumes you have an external pull down resistor?
Is it connected correctly.

The best way to wire a switch is between input pin and ground. Then use INPUT_PULLUP in the pinMode call and look for == LOW in the if statement.

MKSaeed

#4
Nov 23, 2018, 11:35 am Last Edit: Nov 23, 2018, 11:44 am by MKSaeed
@Grumpy_Mike

I have resistor (brown-red-black) connected with ground!

Is it correct the resistor value?
I checked my resistor value from this site : https://www.digikey.com/en/resources/conversion-calculators/conversion-calculator-resistor-color-code-4-band

and found it is 15 ohm!

I think need 10K ohm right?

Grumpy_Mike

#5
Nov 23, 2018, 03:18 pm Last Edit: Nov 23, 2018, 03:19 pm by Grumpy_Mike
Quote
I think need 10K ohm right?
Yes but that would not matter if that was the only fault. Sure it would draw 0.3 mA when the button was pressed but it would still read a zero. Anyway try it and see.

Quote
I have resistor (brown-red-black) connected with ground!
That is connected from the input pin to ground, with the switch connected to the input and +5V.
Those colours would be a 12 Ohm resistor.

Is this a four pin push button? If so you should wire the button across the diagonal because pairs of contacts are connected together.

MKSaeed

Yes I have 4 pins button and connected diagonal as you said.

I want when I press the button write with serial "open" one time and when release the button write "close" one time.

with this code I got it but I have contentious  open and close like loop

Code: [Select]

byte Switch = 2;

void setup()
{
  pinMode(Switch, INPUT);
  Serial.begin(9600);
}
void loop()
{

  if (digitalRead(Switch) == HIGH) {
    Serial.println("Open");
    delay(10);
  }
  else {
        Serial.println("Close");

  }
}

Grumpy_Mike

Code: [Select]
if (digitalRead(Switch) == HIGH) {
    Serial.println("Open");
    delay(10);
    while( digitalRead(Switch) == HIGH) { } // do nothing until low
  }
  else {
        Serial.println("Close");
       while( digitalRead(Switch) == LOW) { } // do nothing until high
  }

MKSaeed

I did this code and when button is released open message is in loop but when I press the button the "close" message keep no loop

how can I made "open" message not to show in loop...I mean when button release then the "open" message shows one time?

Code: [Select]

int Switch = 2;

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

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

  pinMode(Switch, INPUT);
  Serial.begin(9600);
 
}
void loop()
{
    buttonState = digitalRead(Switch);

  if (digitalRead(Switch) == LOW && buttonState == lastButtonState) {
    Serial.println("Open");
    delay(100);
   
   
  }

  if (digitalRead(Switch) == HIGH && buttonState != lastButtonState){
        Serial.println("Close");
   
  }
 lastButtonState = buttonState;
   
}

Grumpy_Mike

Do not include the last button state in the if statement but a Boolean variable that is set to the state of the door.

MKSaeed

@ Grumpy_Mike thanks for reply friend

any example can explain how to do it?

MKSaeed

#11
Nov 23, 2018, 05:51 pm Last Edit: Nov 23, 2018, 05:54 pm by MKSaeed

[/quote]
Code: [Select]
if (digitalRead(Switch) == HIGH) {
    Serial.println("Open");
    delay(10);
    while( digitalRead(Switch) == HIGH) { } // do nothing until low
  }
  else {
        Serial.println("Close");
       while( digitalRead(Switch) == LOW) { } // do nothing until high
  }

I tried it and gave me this output :


Open
Close
Open
Close



MKSaeed


outsider

#14
Nov 23, 2018, 06:36 pm Last Edit: Nov 23, 2018, 06:37 pm by outsider
Why not do as GMike said in reply #3, you won't need a resistor.

Go Up