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 button3Pin = 9; // Pin for button 3

const int relayPin = 8; // Pin for 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 for button 1

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

pinMode(button3Pin, INPUT_PULLUP); // Use internal pull-up resistor for button 3

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)

void loop() {

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

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

bool button3Pressed = digitalRead(button3Pin) == LOW; // Check if button 3 is pressed (LOW)

// 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

// Activate relay for 5 seconds if button 3 is pressed

if (button3Pressed) {

digitalWrite(relayPin, HIGH); // Activate relay

delay(5000); // Hold relay for 5 seconds

digitalWrite(relayPin, LOW); // Deactivate relay

delay(500); // Allow time before next check

You might also like