Parking Project! With Servo Motor
by 762267 in Circuits > Arduino
111 Views, 0 Favorites, 0 Comments
Parking Project! With Servo Motor
Parking project! With servo motor and a buzzer
Supplies
Your going to need.
Servo motor 1x
Red led 2x
Yellow led 1x
Green led 2x
Push button 1x
Buzzer 1x
wires 5x
breadboard 1x
4x 10k ohm resistors
Connect the Buzzer:
Position the buzzer component on the breadboard as shown below: It has two pins: a positive pin and a negative pin, though for most positive programs it will be marked with a ‘+’ or a longer pin.
Denote the powre rail (5V) and connect the positive pin of the buzzer to it.
Ensure that the negative pin of the buzzer is connected to the ground rail of the circuit (GND).
Connect the Push Button:
(Ensure that the push button is placed in such a way that it connects the middle gap of the breadboard.)
connect one end of the push button to the positive rail (5V).
connect the other end of the push button to an empty row of the breadboard.
Connect a resistor (10k ohms) between the empty row and the ground rail (GND).
Connect the LEDs:
Connect the Red LED to the breadboard as shown below Shape the Circuit as shown. The anode or a longer leg of the battery must be placed in a different row as compared to the cathode or a shorter one.
Connect a resistor (10k ohms) between the row of the anode and an unused row of the circuit board.
connect a wire from the resistor row to the positive terminal
The next step is to connect the cathode of the led to the ground rail or the negative power source as it is also referred to.
Perform the same operations for the Green and Yellow LEDs as has been accomplished for the Red LED above.
From the code, you can find that, one end of wire should connect with a certain number pin on Arduino, and the other end of wire should connect with certain place on breadboard which the component (e.g. LED or buzzer) get their power.
For instance:
If you linked the buzzer to module 10 on the Arduino board, the positive pole of the wire should be connected to the hole on the breadboard 10A while the negative pole of the wire should be connected to the hole that connect to the positive of the 5V pin of buzzer.
HERES THE CODE
#include <Servo.h>
int seconds = 0;
int carCount = 0; // number of cars
int totalSlots = 4; // total parking slots
int greenIn = 7; // green light for going in
int redIn = 6; // red light for going in
int greenOut = 1; // green light for going out
int redOut = 0; // red light for going out
int amber = 13; // orange/amber light
Servo barrierServo; // create servo object to control a servo
int incomingSensor = 8; // incoming sensor
int buzzer = 10; // buzzer pin
int incomingState, outgoingState;
void setup() {
barrierServo.attach(9); // attach servo to pin 9
barrierServo.write(0); // barrier down
pinMode(incomingSensor, INPUT); // incoming sensor
pinMode(buzzer, OUTPUT); // buzzer
pinMode(greenIn, OUTPUT);
pinMode(redIn, OUTPUT);
pinMode(greenOut, OUTPUT);
pinMode(redOut, OUTPUT);
pinMode(amber, OUTPUT);
}
void loop() {
// Set initial state of lights
digitalWrite(redIn, HIGH); // Red light for incoming cars
digitalWrite(greenIn, LOW); // Green light for incoming cars (initially off)
digitalWrite(greenOut, LOW); // Green light for outgoing cars (initially off)
digitalWrite(redOut, HIGH); // Red light for outgoing cars
digitalWrite(amber, LOW); // Amber light (initially off)
incomingState = digitalRead(incomingSensor); // Read incoming sensor state
// If incoming cars are detected and outgoing cars are not, process incoming cars
if (incomingState == HIGH && outgoingState == LOW) { // going in
if (carCount < totalSlots) { // If there are available parking slots
openBarrier(); // Open the barrier
buzz(500); // Buzz for 0.5 seconds
digitalWrite(redIn, LOW); // Turn off red light for incoming
digitalWrite(greenIn, HIGH); // Turn on green light for incoming
digitalWrite(greenOut, HIGH); // Turn on green light for outgoing (for visibility)
digitalWrite(redOut, LOW); // Turn off red light for outgoing
digitalWrite(amber, HIGH); // Turn on amber light
carCount++; // Increment car count
delay(4000); // Barrier up time is 4 seconds
closeBarrier(); // Close the barrier
}
}
// If outgoing cars are detected and incoming cars are not, process outgoing cars
if (incomingState == LOW && outgoingState == HIGH) { // going out
if (carCount > 0) { // If there are cars in the parking lot
openBarrier(); // Open the barrier
buzz(500); // Buzz for 0.5 seconds
digitalWrite(redIn, HIGH); // Turn on red light for incoming
digitalWrite(greenIn, LOW); // Turn off green light for incoming
digitalWrite(greenOut, LOW); // Turn off green light for outgoing
digitalWrite(redOut, HIGH); // Turn on red light for outgoing
digitalWrite(amber, HIGH); // Turn on amber light
carCount--; // Decrement car count
delay(2000); // Barrier up time is 2 seconds
closeBarrier(); // Close the barrier
incomingState = 0; // Reset sensors
outgoingState = 0;
}
}
// If both sensors are triggered simultaneously, close the barrier after a delay
else if (outgoingState == HIGH && incomingState == HIGH) { // both sensors triggered
closeBarrier(); // Close the barrier
delay(2000); // Delay for 2 seconds
}
}
// Function to open the barrier
void openBarrier() {
barrierServo.write(90); // Move servo to lift the barrier (90 degrees)
}
// Function to close the barrier
void closeBarrier() {
barrierServo.write(0); // Move servo to lower the barrier (0 degrees)
}
// Function to activate buzzer for a specified duration
void buzz(int duration) {
digitalWrite(buzzer, HIGH); // Turn on buzzer
delay(duration); // Wait for specified duration
digitalWrite(buzzer, LOW); // Turn off buzzer
}