Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

#include <Servo.

h>

const int button1Pin = 2; // Pin for button 1

const int button2Pin = 3; // Pin for button 2

const int switchPin = 4; // Pin for the switch

const int relayPin = 5; // Pin for the relay

const int servo1Pin = 6; // Pin for the first servo motor

const int servo2Pin = 7; // Pin for the second servo motor

Servo servo1;

Servo servo2;

void setup() {

pinMode(button1Pin, INPUT_PULLUP); // Use internal pull-up resistor

pinMode(button2Pin, INPUT_PULLUP); // Use internal pull-up resistor

pinMode(switchPin, INPUT_PULLUP); // Use internal pull-up resistor

pinMode(relayPin, OUTPUT); // Set relay pin as output

servo1.attach(servo1Pin);

servo2.attach(servo2Pin);

servo1.write(0); // Initial position of the first servo (0 degrees)

servo2.write(0); // Initial position of the second servo (0 degrees)

digitalWrite(relayPin, LOW); // Ensure relay is off initially

void loop() {

bool button1Pressed = digitalRead(button1Pin) == LOW; // Assuming LOW means button pressed

bool button2Pressed = digitalRead(button2Pin) == LOW; // Assuming LOW means button pressed

bool switchState = digitalRead(switchPin) == LOW; // Assuming LOW means switch is active


// Control relay based on switch state

if (switchState) {

digitalWrite(relayPin, HIGH); // Activate relay when switch is LOW

} else {

digitalWrite(relayPin, LOW); // Deactivate relay when switch is HIGH

// Control servo 1 based on button 1 press

if (button1Pressed) {

servo1.write(120); // Rotate clockwise to 120 degrees from 0

delay(3000); // Wait for 3 seconds

servo1.write(0); // Move back to original position

delay(500); // Allow time for servo to move back before next check

// Control servo 2 based on button 2 press

if (button2Pressed) {

servo2.write(120); // Rotate clockwise to 120 degrees from 0

delay(3000); // Wait for 3 seconds

servo2.write(0); // Move back to original position

delay(500); // Allow time for servo to move back before next check

You might also like