From 31e3694e4f35f7209738e408dc2d6f398a911945 Mon Sep 17 00:00:00 2001 From: paul Date: Tue, 21 Jan 2025 13:44:25 -0500 Subject: [PATCH] main code to operate the garage door sensor --- garage_door_sensor.ino | 104 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 garage_door_sensor.ino diff --git a/garage_door_sensor.ino b/garage_door_sensor.ino new file mode 100644 index 0000000..bd8cea6 --- /dev/null +++ b/garage_door_sensor.ino @@ -0,0 +1,104 @@ +#include + +int laserPin = 4; +int doorsensorpin = 8; // limit switch NC pin +int doorsensorled = 7; // door open led +bool havemotion = false; +int checksaftersafe = 0; +#define echoPin 3 // attach pin D2 Arduino to pin Echo of HC-SR04 +#define trigPin 2 //attach pin D3 Arduino to pin Trig of HC-SR04 + +#define LED_PIN 5 +#define NUM_LEDS 7 +Adafruit_NeoPixel pixels(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800); + +// defines variables +long duration; // variable for the duration of sound wave travel +int distance; // variable for the distance measurement +int dooropen; // variable for the limit switch value + +void setup() { + // put your setup code here, to run once: + pinMode(laserPin, OUTPUT); + pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT + pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT + pinMode(doorsensorpin, INPUT_PULLUP); + pinMode(doorsensorled, OUTPUT); + pinMode(13, OUTPUT); + pixels.begin(); + //Serial.begin(9600); + digitalWrite(laserPin,LOW); + digitalWrite(doorsensorled,LOW); + digitalWrite(13,LOW); +} + +void loop() { + + pixels.clear(); + pixels.setBrightness(200); + + // put your main code here, to run repeatedly: + // Clears the trigPin condition + digitalWrite(trigPin, LOW); + delayMicroseconds(2); + // Sets the trigPin HIGH (ACTIVE) for 10 microseconds + digitalWrite(trigPin, HIGH); + delayMicroseconds(10); + digitalWrite(trigPin, LOW); + // Reads the echoPin, returns the sound wave travel time in microseconds + duration = pulseIn(echoPin, HIGH); + // reads is the door open limit switch + dooropen = digitalRead(doorsensorpin); + // Calculating the distance + distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back) + // Displays the distance on the Serial Monitor + Serial.print("Distance: "); + Serial.print(distance); + Serial.println(" cm"); + // distance from garage to sensor in most cases is: 46cm (21 inches minus 3 inches for box width) + // but we will go with 60... just in case, since initial tests seemed to be a bit off. + if (distance < 60) + { + havemotion = true; + checksaftersafe = 0; + digitalWrite(laserPin, HIGH); + for( int i = 0; i < NUM_LEDS; i++) { + pixels.setPixelColor(i, pixels.Color(255, 0, 0)); + } + } + else + { + // 20 times would be about 10 seconds of safety + if (havemotion && checksaftersafe < 20) + { + for( int i = 0; i < NUM_LEDS; i++) { + pixels.setPixelColor(i, pixels.Color(0, 255, 0)); + } + checksaftersafe++; + } + else + { + for( int i = 0; i < NUM_LEDS; i++) { + pixels.setPixelColor(i, pixels.Color(0, 0, 255)); + } + checksaftersafe = 0; + havemotion = false; + } + digitalWrite(laserPin, LOW); + } + + // now lets do the door open limit switch logic + if (dooropen == HIGH) + { + Serial.println("Door closed"); + digitalWrite(doorsensorled, LOW); + } + else + { + Serial.println("Door open"); + digitalWrite(doorsensorled, HIGH); + } + + pixels.show(); + delay(250); +} \ No newline at end of file