Go Down

Topic: Garage project (Read 193 times) previous topic - next topic

craiginnes911

Hello,

Newbie to the forum.

I have been trying to get a working model that will close my garage door when I forget to.

Along with this I have a parking sensor in the garage, and also a sensor to indicate if there is a blockage in the path of the garage door.

I have all the above working aswell as a sensor to detect if door is open or closed. But I can;t get the door to close after 5 mins of being left open, with no blockage in its path. I have used a delay command but this then prevents all other actions occurring.

Ideally I would like a command where if the path is clear, and the door has been open for 5 mins then close the door. In my code I have the LED simulating whether the door is open or closed.

Thanks

My code is below


#define trigPin 4

#define blockLED 13

#define SENSORPIN 7

#define echoPin 6

#define parkled 9

#define doorled 11

#define doortrig 3

#define doorecho 2

// variables will change:
int sensorState = 0, lastState = 0;       // variable for reading the pushbutton status

void setup()

{ Serial.begin (9600);

  pinMode(trigPin, OUTPUT);

  pinMode(echoPin, INPUT);

  pinMode(parkled, OUTPUT);

  pinMode(doortrig, OUTPUT);

  pinMode(doorecho, INPUT);

  pinMode(doorled, OUTPUT);
 
  // initialize the parkLED pin as an output:
  pinMode(blockLED, OUTPUT);
  // initialize the sensor pin as an input:
  pinMode(SENSORPIN, INPUT);
  digitalWrite(SENSORPIN, HIGH); // turn on the pullup
}

void loop()

{ long duration, distance;

  digitalWrite(trigPin, LOW);

  delayMicroseconds(2);

  digitalWrite(trigPin, HIGH);

  delayMicroseconds(5);

  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);

  distance = (duration / 2) / 29.1;

  if (distance > 3
 
  )

  { digitalWrite(parkled, HIGH);

  }

  else {

    digitalWrite(parkled, LOW);

  }

  Serial.print(distance);

  Serial.println(" cm");

  delay(200);

  // read the state of the pushbutton value:
  sensorState = digitalRead(SENSORPIN);

  // check if the sensor beam is broken
  // if it is, the sensorState is LOW:
  if (sensorState == LOW) {
    // turn blockLED on:
    digitalWrite(blockLED, HIGH);
  }
  else {
    // turn blockLED off:
    digitalWrite(blockLED, LOW);
  }

  if (sensorState && !lastState) {
    Serial.println("Unbroken");
  }
  if (!sensorState && lastState) {
    Serial.println("Broken");
  }
  lastState = sensorState;

  long duration2, distance2;

  digitalWrite(doortrig, LOW);

  delayMicroseconds(2);

  digitalWrite(doortrig, HIGH);

  delayMicroseconds(5);

  digitalWrite(doortrig, LOW);

  duration2 = pulseIn(doorecho, HIGH);

  distance2 = (duration2 / 2) / 29.1;

  if (distance2 < 10
  )

  { digitalWrite(doorled, LOW);

  }

  else {

    digitalWrite(doorled, HIGH);

  }

  Serial.print(distance2);

  Serial.println(" cm");

  delay(100);
}

PaulS

Quote
I have used a delay command
If it were YOUR job to sit in the garage, and monitor the sensors, that is NOT how you would do it, is it?

Hey, the door is open and car is inside. Let me take a nap, and then I'll close the door.

No, what you would do is note what time it was when the condition is satisfied that the car is inside with the door closed and with nothing in the door's way.

Periodically, you'd see if the condition was still true, and, if so, had it been true for some period of time.

So, why would you write the code any other way?
The art of getting good answers lies in asking good questions.

dougp

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!

craiginnes911

Thanks, I am very aware of this, hence why I am looking for assistance to what the correct command is. I 100% know and appreciate that the delay or wait command is not the correct one to use.

wildbill

Use millis. When you first notice that the door is open, record the value of millis. If it's still open when mills()-recordedValue > fiveMinutes, close the door.

KrisKasprzak

I have this same project and it's working. I have sensors in the garage that use ultrasonic distance sensors to monitor door position, then wireless sends that data to a base inside the house where a display shows each door position. There are buttons on the base the let me open/close each door. This process sends a signal to the doors sensor and a small relay on the sensor unit trips the door open/close button. My unit also has wifi so i can open / close via a web page or my phone (but only if connected to home internet).

My base sends a data request signal to the sensors to poll door position and report back. This is done in sequence to avoid the base getting wireless data simultaneously.

There's a ultrasonic distance sensor library I suggest you use. It will handle all the distance pinging and such. You just as the distance object for a distance and the library will report back. Then in your loop just request the distance (note. my sensor is near the opener motor and looks for the top of the door) when the door is open the distance is a foot or so, when the door is closed, it's 10 feet or so... hence i can easily detect position).

Hope this helps. PS Don't let some of the attitudes in this forum get you down.

Thanks,

Kris

Paul__B

OK, two things.

Firstly you, craiginnes911, need to go and read the forum instructions so that you can go back and modify your original post (not re-post it) - using the "More -> Modify" option below the right hand corner of your post - to mark up your code as such using the "</>" icon in the posting window.  Just highlight each section of code (or output if you need to post that) from the IDE and click the icon.

In fact, the IDE has a "copy for forum" link to put these markings on a highlighted block for you so you then just paste it here in a posting window.  But don't forget to use the "Auto-Format" (Ctrl-T) option first to make it easy to read.  If you do not post it as "code" it can easily be quite garbled and is always more difficult to read.

It is inappropriate to attach it as a ".ino" file unless it is clearly too long to include in the post proper.  People can usually see the mistakes directly and do not want to have to actually load it in their own IDE.  And that would also assume they are using a PC and have the IDE running on that PC.

Also tidy up your blank space.  Do use blank lines, but only between functional blocks.  Get rid of that double-spacing which makes it almost illegible



Now an interesting aside.  Our garage is used as the universal entry point to the house.  While it is annoying to find the garage door left open (and it is not visible from inside the house), it would be catastrophic if one walked out to put clothes on the line or such and the door closed itself when we had not brought keys!

dougp

While it is annoying to find the garage door left open (and it is not visible from inside the house), it would be catastrophic if one walked out to put clothes on the line or such and the door closed itself when we had not brought keys!
Ours has a door mounted remote on the outside.  As long as you can remember the code you're golden.
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!

dave-in-nj

#8
Apr 19, 2019, 02:28 pm Last Edit: Apr 19, 2019, 02:30 pm by dave-in-nj
lots of ways to get to the result.


if - the door is closed,
   check again


if the door is open
   if the car is in the garage
      
as you can see, this is true every moment the car is in the garage and the door is open.


if the door is open
   if the car is in the garage
      if the car  Has_Been  in the garage
         set Has_Been = 1  // sets a flag
         Then = millis()  // sets unsigned long to millis()


this would set the flag   Has_Been and start the timer.


if the door is closed
   Has_been = 0
if the door is open
   if the car is in the garage
      if Has_Been = 0  //  if the  car Has_Been  in the garage
            set Has_Been = 1  // sets the flag
            Then = millis()   // starts the timer


if (millis() - Then >= 5 minutes)
   close the door


this is very clunky but it shows how you can to set a flag to tell you  if the state has already existed
if it has existed, no need too do that again.
if it is the first time, then set the flag and the timer.

there are more elegant ways, but this is one of the general ways to do this.
and the concept of the flag should point out the obvious thing.


Go Up