#include <Servo.h>
const int buttonPin1 = 9; // Pin connected to the first button
const int buttonPin2 = 11; // Pin connected to the second button
const int ledPin = 13; // Pin connected to the LED
const int buzzerPin = 6; // Pin connected to the Buzzer
const int servoPin = 3; // Pin connected to the Servo
Servo myServo; // Create servo object to control a servo
int buttonState1 = 0; // Variable to store the state of button 1
int buttonState2 = 0; // Variable to store the state of button 2
bool systemActivated = false; // To track if the system has been activated
void setup() {
// Initialize the button pins as inputs
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
// Initialize the LED and Buzzer pins as outputs
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
// Attach the servo on pin 6 to the servo object
myServo.attach(servoPin);
// Set the servo to the initial position (0 degrees)
myServo.write(90);
// Initialize serial communication for debugging
Serial.begin(9600);
}
void loop() {
// Only check buttons if the system has not been activated
if (!systemActivated) {
// Read the state of the buttons
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
// If either button 1 OR button 2 is pressed, activate the system
if (buttonState1 == HIGH || buttonState2 == HIGH) {
activateSystem();
systemActivated = true; // Prevent further button presses from changing the state
}
}
// Keep the system running if it's activated
if (systemActivated) {
keepSystemActive();
}
delay(50); // Small delay for debouncing the buttons
}
// Function to activate the system
void activateSystem() {
// Turn on the LED
digitalWrite(ledPin, HIGH);
// Turn on the buzzer
digitalWrite(buzzerPin, HIGH);
// Move the servo to 90 degrees
myServo.write(0);
// Optional: Print to serial monitor for debugging
Serial.println("System activated: LED on, Buzzer on, Servo at 90 degrees");
}
// Function to keep the system components active
void keepSystemActive() {
// The system remains active (LED, buzzer, and servo remain in the same state)
digitalWrite(ledPin, HIGH);
digitalWrite(buzzerPin, HIGH);
myServo.write(0);
}