0% found this document useful (0 votes)
1 views

code implementation

jgdawdgr

Uploaded by

yohannestafete
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

code implementation

jgdawdgr

Uploaded by

yohannestafete
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Software design

The software design includes the following key components:


 Sensor Detection Code: The microcontroller is programmed to constantly check the
sensor’s state (whether motion is detected) and take action when needed.
 Actuator Control Code: The code for controlling the actuator, i.e., sending signals to
open or close the door.
 Remote Control Interface (Optional): The remote interface code, allowing the user to
monitor the door status and open/close it remotely.
Programming Languages and Tools:
 Programming Language:
o For Arduino: C++ (using Arduino IDE).
o For Raspberry Pi: Python (using libraries like GPIO Zero or RPi.GPIO).
 Development Tools:
o Arduino IDE (for coding, uploading, and debugging).
o Thonny IDE or VS Code (for Raspberry Pi development).

Algorithm for Door Control


The following steps outline the core algorithm:
1. Initialize the sensor, actuator, and any communication modules.
2. Continuously read sensor data.
3. If the sensor detects motion:
o Send a signal to the actuator to open the door.
o Start a delay timer.
4. If no motion is detected after the delay timer expires:
o Send a signal to the actuator to close the door.
5. Repeat the process.

Code Implementation
Example Code for Arduino
cpp
#include <Servo.h>
// Pin Definitions
#define PIR_PIN 2 // PIR sensor connected to pin 2
#define SERVO_PIN 9 // Servo motor connected to pin 9

// Global Variables
Servo doorServo; // Servo motor object
int sensorValue = 0; // Variable to store sensor reading
int doorOpenAngle = 90; // Angle to open the door
int doorCloseAngle = 0; // Angle to close the door
unsigned long openTime = 0; // Tracks when the door was opened
const unsigned long delayTime = 5000; // 5 seconds delay before closing

void setup() {
pinMode(PIR_PIN, INPUT);
doorServo.attach(SERVO_PIN);
doorServo.write(doorCloseAngle); // Start with door closed
Serial.begin(9600);
}

void loop() {
sensorValue = digitalRead(PIR_PIN);

if (sensorValue == HIGH) { // Motion detected


Serial.println("Motion Detected!");
doorServo.write(doorOpenAngle); // Open the door
openTime = millis(); // Record the time
}

// Close the door after delay if no motion


if ((millis() - openTime) > delayTime && sensorValue == LOW) {
Serial.println("Closing the door.");
doorServo.write(doorCloseAngle);
}
}
Optional: Bluetooth Integration (Using HC-05)
For remote control using a mobile app:
1. Establish a Bluetooth connection using the HC-05 module.
2. Add commands to open/close the door manually.
Example Addition to Code:
cpp
Copy code
#include <SoftwareSerial.h>

// Bluetooth Module Pins


SoftwareSerial BTSerial(10, 11); // RX, TX

void setup() {
BTSerial.begin(9600); // Start Bluetooth communication
...
}

void loop() {
// Check for Bluetooth command
if (BTSerial.available()) {
char command = BTSerial.read();
if (command == 'O') { // 'O' for Open
doorServo.write(doorOpenAngle);
} else if (command == 'C') { // 'C' for Close
doorServo.write(doorCloseAngle);
}
}
...
}

For Raspberry Pi (Python)


Here’s a similar implementation using Python:
python
Copy code
import RPi.GPIO as GPIO
import time

# Pin Definitions
PIR_PIN = 17
SERVO_PIN = 18

# Setup
GPIO.setmode(GPIO.BCM)
GPIO.setup(PIR_PIN, GPIO.IN)
GPIO.setup(SERVO_PIN, GPIO.OUT)

# Servo Setup
servo = GPIO.PWM(SERVO_PIN, 50) # 50 Hz PWM frequency
servo.start(0)

def set_servo_angle(angle):
duty = 2 + (angle / 18) # Convert angle to duty cycle
GPIO.output(SERVO_PIN, True)
servo.ChangeDutyCycle(duty)
time.sleep(1)
GPIO.output(SERVO_PIN, False)
servo.ChangeDutyCycle(0)
try:
while True:
if GPIO.input(PIR_PIN): # Motion detected
print("Motion Detected!")
set_servo_angle(90) # Open door
time.sleep(5) # Delay before closing
else:
print("No motion, closing door.")
set_servo_angle(0) # Close door
time.sleep(1)

except KeyboardInterrupt:
print("Exiting program.")
GPIO.cleanup()

You might also like