Go Down

Topic: Reed switch to existing code (Read 379 times) previous topic - next topic

klemo4

hi. i am working on a project and i want to add 2 reed switch to my existing project. i want to the button to be active when the door is close but if the door open until the 20 sec has pass i need to reset the hole thing.



Code: [Select]
int pin = 2;
int led = 5;
int led1 = 6;
int led2 = 7;
void setup()
{
  pinMode(pin, INPUT_PULLUP);
  pinMode(led, OUTPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);

}

long offAt = 0;
void loop()
{
  if( (digitalRead(led2) == HIGH ) && (digitalRead(led) == HIGH ) && (digitalRead(pin) == HIGH) ) //if LED is off  and button is pressed [low because it has pullup resistor]
  {
    digitalWrite(led1, LOW);
    delay(1500);
    digitalWrite(led, LOW);
    offAt = millis() + 18000; //store var of now + 5 seconds
  }
 
  if(digitalRead(led) == LOW) //if led is on
  {
      if(millis() >= offAt) //see if it's time to turn off LED
      {
         digitalWrite(led, HIGH); //it's time. this also re-enables the button
         digitalWrite(led1, HIGH); //it's time. this also re-enables the button
      }
  }
}

AWOL

How do you determine proximity of the door?

klemo4

sorry i did not understand what you mean.

AWOL

You said you want the button to be active when the door is close.

How do you detect proximity of the door?

klemo4

You said you want the button to be active when the door is close.

How do you detect proximity of the door?
If i understand correctly you asking me what i want to use? sorry but English is not my first language so bear with me plz. I want to add a magnetic switch to the door and if the door is close the code i have can be executed and if open before the 20sec i need to reset. so you can understand, i am making a simulator elevator. with the code i have now i am arming some relays and giving a signal to raspberry pi to play a video. so i want all this to happen only if the door is close.

dougp

if the door is close
I speculate the OP means the door is closed.  As in, shut.

Would @klemo4 clarify?
Everything we call real is made of things that cannot be regarded as real.  If quantum mechanics hasn't profoundly shocked you, you haven't understood it yet. - Niels Bohr

No private consultations undertaken!

klemo4

Yes when the door is open the switch is close. If is difficult I can get a different switch.

wvmarle

Close or closed? That one letter makes a world of difference here.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

dougp

Yes when the door is open the switch is close. If is difficult I can get a different switch.
It isn't the switch that's the difficulty, it's the language.  If the door is open it's because someone opened it.  After you close the door it's closed.  See the difference?

Close  has multiple meanings in English.
Everything we call real is made of things that cannot be regarded as real.  If quantum mechanics hasn't profoundly shocked you, you haven't understood it yet. - Niels Bohr

No private consultations undertaken!

GoForSmoke

You might try a weaker magnet or arranging magnet and switch differently.

With a Hall sensor we can measure magnetic field strength along 1 axis. Just turning the magnet makes a difference. If I put a weak magnet on an outer door edge and the detector in the frame (unless it's an iron/steel frame/door) the field won't get detected until the door is in the frame.
Iron/steel will affect the magnet field by "soaking it up". Usually the magnet gets mounted on a back of the door mount (a very short distance is needed, a finger thickness if that) with sensor on a frame mount. The detector must be very close to a weak magnet/field to detect it.

1) http://gammon.com.au/blink  <-- tasking Arduino 1-2-3
2) http://gammon.com.au/serial <-- techniques howto
3) http://gammon.com.au/interrupts
Your sketch can sense ongoing process events in time.
Your sketch can make events to control it over time.

klemo4

It isn't the switch that's the difficulty, it's the language.  If the door is open it's because someone opened it.  After you close the door it's closed.  See the difference?

Close  has multiple meanings in English.
yes now i see what do you mean and i am very sorry for the trouble. Closed i mean.

You might try a weaker magnet or arranging magnet and switch differently.

With a Hall sensor we can measure magnetic field strength along 1 axis. Just turning the magnet makes a difference. If I put a weak magnet on an outer door edge and the detector in the frame (unless it's an iron/steel frame/door) the field won't get detected until the door is in the frame.
Iron/steel will affect the magnet field by "soaking it up". Usually the magnet gets mounted on a back of the door mount (a very short distance is needed, a finger thickness if that) with sensor on a frame mount. The detector must be very close to a weak magnet/field to detect it.


what other switch can i add to the door? i dont have any problem using other. i just want to work.

GoForSmoke

An IR led and IR detector can be set as proximity detector in the door frame to sense door closed.

For proximity (closeness, but search word is prox...) detector you aim the led light and detector so that the light must reflect off a close object to be detected. Cross a finger of each hand, if one hand is light and the other detector, the fingers are the path when something is as close as the crossing. And without the reflector, nothing to detect.

If the door has a gap in the frame, glue a folded piece of cardboard or plastic with wires and contacts so when the door closes it also closes the wedge and contacts, is a switch. Connect one side to ground and the other to a pin moded INPUT_PULLUP and if the room does not have strong changing magnetic fields near the wires you will be okay to know door open-close even over a good bit of distance, one Hot Wheels car racer ran 15m wire to finish gate (and 15m back) and it worked because his need is very slow compared to Arduino even though too fast for human senses. 
1) http://gammon.com.au/blink  <-- tasking Arduino 1-2-3
2) http://gammon.com.au/serial <-- techniques howto
3) http://gammon.com.au/interrupts
Your sketch can sense ongoing process events in time.
Your sketch can make events to control it over time.

Go Up