Skip to content

Instantly share code, notes, and snippets.

@kophyo54
Forked from haylau/clawexample.ino
Created June 10, 2026 21:47
Show Gist options
  • Select an option

  • Save kophyo54/0b18de36481643566ece9791b477915e to your computer and use it in GitHub Desktop.

Select an option

Save kophyo54/0b18de36481643566ece9791b477915e to your computer and use it in GitHub Desktop.
#include <Servo.h>
Servo middle, left, right, claw ; // Creates 4 "servo objects"
#define CLAW_OFF 0
#define CLAW_ON 90
volatile int clawPosition;
volatile bool clawOpen;
int middlePin = A1; // Middle Pot Analog Input Pin
int leftPin = A2; // Left Pot Analog Input Pin
int rightPin = A3; // Right Pot Analog Input Pin
int clawPin = A0; // Claw Pot Analog Input Pin
int middleValue = 0; // Middle Pot Value
int leftValue = 0; // Left Pot Value
int rightValue = 0; // Right Pot Value
int clawValue = 0; // Claw Pot Value
/*
void grab() {
clawOpen = false;
clawPosition = CLAW_OFF;
claw.write(clawPosition);
}
void release() {
clawOpen = true;
clawPosition = CLAW_ON;
claw.write(clawPosition);
}
*/
void activateClawInterrupt() {
if(clawOpen) {
clawOpen = false;
clawPosition = CLAW_ON;
claw.write(clawPosition);
}
else if(!clawOpen){
clawOpen = true;
clawPosition = CLAW_OFF;
claw.write(clawPosition);
}
}
void setup()
{
middle.attach(11); // attaches the servo on pin 11 to the middle object
left.attach(10); // attaches the servo on pin 10 to the left object
right.attach(9); // attaches the servo on pin 9 to the right object
claw.attach(6); // attaches the servo on pin 6 to the claw object
attachInterrupt(digitalPinToInterrupt(2), activateClawInterrupt, RISING);
Serial.begin(9600);
}
void loop()
{
// Read the Pot Values
middleValue = analogRead(middlePin); //A1 pin 11
leftValue = analogRead(leftPin); //A2 pin 10
rightValue = analogRead(rightPin); //A3 and pin 9
clawValue = analogRead(clawPin); // Interrupt
// Convert to values between 0 and 180 for the servos
middleValue = map(middleValue, 0, 1023, 0, 180) ;
leftValue = map(leftValue, 0, 1023, 0, 180) ;
rightValue = map(rightValue, 0, 1023, 0, 180) ;
//clawValue = map(clawValue, 0, 1023, 0, 180) ;
// Write values to servos
middle.write(middleValue); // Middle servo position
left.write(leftValue); // Left servo position
right.write(rightValue); // Right servo position
//claw.write(clawValue); // Claw servo position
Serial.print(clawOpen);Serial.print(leftValue); Serial.print(", "); Serial.print(middleValue); Serial.print(", ");
Serial.println(rightValue);
delay(200); // Short Delay
}
#include <Servo.h>
Servo middle, left, right, claw ; // creates 4 "servo objects"
void setup()
{
Serial.begin(9600);
middle.attach(11); // attaches the servo on pin 11 to the middle object
left.attach(10); // attaches the servo on pin 10 to the left object
right.attach(9); // attaches the servo on pin 9 to the right object
claw.attach(6); // attaches the servo on pin 6 to the claw object
//int pos = 0;
}
void loop()
{
// Callibration
#if 1
middle.write(90); // sets the servo position according to the value(degrees)
left.write(90); // does the same
right.write(90); // and again
claw.write(25); // yes you've guessed it
delay(300);
claw.write(150);
int pos = 0;
#endif
// Go through whole motion
#if 0
claw.write(25);
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
left.write(pos); // tell servo to go to position in variable 'pos'
right.write(pos);
middle.write(pos);
claw.write(pos);
delay(15); // waits 15ms for the servo to reach the position
}
claw.write(150);
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
left.write(pos); // tell servo to go to position in variable 'pos'
right.write(pos);
middle.write(pos);
claw.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
#endif
delay(300); // doesn't constantly update the servos which can fry them
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment