#include <Servo.
h>
// Define motor control pins
int motor1CW = 8;
int motor1CCW = 9;
int motor2CW = 10;
int motor2CCW = 11;
// Define servo control pin
int servoPin = 12;
Servo gripperServo; // Create a servo object
void setup() {
// Set motor control pins as outputs
pinMode(motor1CW, OUTPUT);
pinMode(motor1CCW, OUTPUT);
pinMode(motor2CW, OUTPUT);
pinMode(motor2CCW, OUTPUT);
// Attach servo to pin
gripperServo.attach(servoPin);
// Initialize Serial communication
Serial.begin(9600);
}
void loop() {
// Check if data is available to read from Serial
if (Serial.available() > 0) {
// Read the incoming command
char command = Serial.read();
// Perform motor or servo control based on the received command
switch (command) {
case 'C':
// Move motor 1 clockwise
moveMotor1Clockwise();
break;
case 'A':
// Move motor 1 anticlockwise
moveMotor1AntiClockwise();
break;
case 'U':
// Move motor 2 clockwise
moveMotor2Clockwise();
break;
case 'D':
// Move motor 2 anticlockwise
moveMotor2AntiClockwise();
break;
case 'S':
// Stop motors
stopMotors();
break;
case 'G':
// Grip using servo (move 10 degrees clockwise)
grip();
break;
case 'N':
// Ungrip using servo (move 10 degrees anticlockwise)
ungrip();
break;
default:
// Stop motors and servo if an unrecognized command is received
stopMotors();
ungrip();
break;
}
}
}
// Function to move motor 1 clockwise
void moveMotor1Clockwise() {
digitalWrite(motor1CW, HIGH);
digitalWrite(motor1CCW, LOW);
delay(100);
}
// Function to move motor 1 anticlockwise
void moveMotor1AntiClockwise() {
digitalWrite(motor1CW, LOW);
digitalWrite(motor1CCW, HIGH);
delay(100);
}
// Function to move motor 2 clockwise
void moveMotor2Clockwise() {
digitalWrite(motor2CW, HIGH);
digitalWrite(motor2CCW, LOW);
delay(100);
}
// Function to move motor 2 anticlockwise
void moveMotor2AntiClockwise() {
digitalWrite(motor2CW, LOW);
digitalWrite(motor2CCW, HIGH);
delay(100);
}
// Function to stop both motors
void stopMotors() {
digitalWrite(motor1CW, LOW);
digitalWrite(motor1CCW, LOW);
digitalWrite(motor2CW, LOW);
digitalWrite(motor2CCW, LOW);
}
// Function to grip using servo (move 10 degrees clockwise)
void grip() {
int currentPos = gripperServo.read();
gripperServo.write(currentPos + 6); // Move 10 degrees clockwise
delay(100); // Delay to allow servo to move
}
// Function to ungrip using servo (move 10 degrees anticlockwise)
void ungrip() {
int currentPos = gripperServo.read();
gripperServo.write(currentPos - 6); // Move 10 degrees anticlockwise
delay(100); // Delay to allow servo to move
}