#include <Servo.h>
Servo myServo; // Create a first servo object
Servo myServo2; // Create a second servo object
int button1State = 0; // Variable to store button 1 state
int button2State = 0; // Variable to store button 2 state
int button1Pin = 7; // Pin for button 1 (clockwise)
int button2Pin = 8; // Pin for button 2 (counterclockwise)
int servoPin = 3; // Pin for 1st servo motor
int servoPin2 = 5; // Pin for 2nd servo motor
void setup() {
pinMode(button1Pin, INPUT_PULLUP);
pinMode(button2Pin, INPUT_PULLUP);
myServo.attach(servoPin); // Attach the 1st servo to the pin
myServo2.attach(servoPin2); // Attach the 2nd servo to the pin
// Initially open claws
myServo.write(180); // Rotate 1st servo counterclockwise
myServo2.write(0); // Rotate 2nd servo clockwise
}
void loop() {
button1State = digitalRead(button1Pin);
button2State = digitalRead(button2Pin);
// Close claws
if (button1State == LOW && button2State == HIGH) { // Reversed logic when using pullup mode (internal resistor on Arduino NANO)
myServo.write(0); // Rotate 1st servo clockwise
myServo2.write(180); // Rotate 2nd servo counterclockwise
}
// Open claws
if (button2State == LOW && button1State == HIGH)
{
myServo.write(180); // Rotate 1st servo counterclockwise
myServo2.write(0); // Rotate 2nd servo clockwise
}
delay(50); // Add a small delay to debounce buttons
}
Connect battery to
VIN/GND pins on Arduino.
Connect buttons to GND/Digital pins on Arduino.
Open claws.
Close claws.