0% found this document useful (0 votes)
12 views2 pages

ARDUINO5

The document defines constants for button and servo pins. It initializes two servos and attaches them to pins. Pressing buttons moves the corresponding servo to 120 degrees for 3 seconds then back to 0, with debouncing.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

ARDUINO5

The document defines constants for button and servo pins. It initializes two servos and attaches them to pins. Pressing buttons moves the corresponding servo to 120 degrees for 3 seconds then back to 0, with debouncing.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <Servo.

h>

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

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

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

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

Servo servo1;

Servo servo2;

void setup() {

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

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

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

// Debounce button 1

if (button1Pressed) {

delay(50); // Debounce delay

if (digitalRead(button1Pin) == LOW) {

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

// Debounce button 2

if (button2Pressed) {

delay(50); // Debounce delay

if (digitalRead(button2Pin) == LOW) {

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