#include <Servo.h>
const int ultrasonicTrigPin = 2; // Ultrasonic sensor trigger pin
const int ultrasonicEchoPin = 3; // Ultrasonic sensor echo pin
const int servoPin = 5; // Servo motor control pin
const int buttonPin = 4; // Push button pin
const int ledPin = 6; // LED pin
Servo servo;
enum ButtonState {
Idle,
Pressed
};
ButtonState buttonState = Idle;
bool ledState = false;
unsigned long prevPressTime = 0;
const unsigned long ledOnDuration = 10000; // LED on duration in milliseconds
void setup() {
pinMode(ultrasonicTrigPin, OUTPUT);
pinMode(ultrasonicEchoPin, INPUT);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
servo.attach(servoPin);
servo.write(0); // Initialize servo position to 0 degrees
}
void loop() {
// Check for obstacle using ultrasonic sensor
long duration, distance;
digitalWrite(ultrasonicTrigPin, LOW);
delayMicroseconds(2);
digitalWrite(ultrasonicTrigPin, HIGH);
delayMicroseconds(100);
digitalWrite(ultrasonicTrigPin, LOW);
duration = pulseIn(ultrasonicEchoPin, HIGH);
distance = duration * 0.034 / 2;
if (distance <= 100) {
// Obstacle detected, rotate servo motor 180 degrees
servo.write(0);
delay(500); // Wait for 5 seconds
servo.write(360); // Reset servo position to 0 degrees
}
// Read button state
bool buttonPressed = digitalRead(buttonPin) == LOW;
switch (buttonState) {
case Idle:
if (buttonPressed) {
buttonState = Pressed;
prevPressTime = millis();
ledState = true;
digitalWrite(ledPin, HIGH);
}
break;
case Pressed:
if (buttonPressed) {
unsigned long currentTime = millis();
if (currentTime - prevPressTime >= ledOnDuration) {
// LED on duration has elapsed, turn off the LED
ledState = false;
digitalWrite(ledPin, LOW);
prevPressTime = currentTime;
}
} else {
buttonState = Idle;
}
break;
}
}
// #include <Servo.h>
// const int ultrasonicTrigPin = 2; // Ultrasonic sensor trigger pin
// const int ultrasonicEchoPin = 3; // Ultrasonic sensor echo pin
// const int servoPin = 5; // Servo motor control pin
// const int buttonPin = 4; // Push button pin
// const int ledPin = 6; // LED pin
// Servo servo;
// enum ButtonState {
// Idle,
// Pressed
// };
// ButtonState buttonState = Idle;
// bool ledState = false;
// unsigned long prevPressTime = 0;
// const unsigned long ledOnDuration = 10000; // LED on duration in milliseconds
// void setup() {
// pinMode(ultrasonicTrigPin, OUTPUT);
// pinMode(ultrasonicEchoPin, INPUT);
// pinMode(buttonPin, INPUT_PULLUP);
// pinMode(ledPin, OUTPUT);
// servo.attach(servoPin);
// servo.write(0); // Initialize servo position to 0 degrees
// }
// void loop() {
// // Check for obstacle using ultrasonic sensor
// long duration, distance;
// digitalWrite(ultrasonicTrigPin, LOW);
// delayMicroseconds(2);
// digitalWrite(ultrasonicTrigPin, HIGH);
// delayMicroseconds(100);
// digitalWrite(ultrasonicTrigPin, LOW);
// duration = pulseIn(ultrasonicEchoPin, HIGH);
// distance = duration * 0.034 / 2;
// if (distance <= 100) {
// // Obstacle detected, rotate servo motor 180 degrees
// servo.write(0);
// delay(300);
// servo.write(360); // Reset servo position to 0 degrees
// }
// // Read button state
// bool buttonPressed = digitalRead(buttonPin) == LOW;
// switch (buttonState) {
// case Idle:
// if (buttonPressed) {
// buttonState = Pressed;
// prevPressTime = millis();
// ledState = true;
// digitalWrite(ledPin, HIGH);
// }
// break;
// case Pressed:
// if (buttonPressed) {
// unsigned long currentTime = millis();
// if (currentTime - prevPressTime >= ledOnDuration) {
// // LED on duration has elapsed, turn off the LED
// ledState = false;
// digitalWrite(ledPin, LOW);
// prevPressTime = currentTime;
// }
// } else {
// buttonState = Idle;
// }
// break;
// }
// }