Using a switch to move a robotic hand up/down with a slight delay

// Finger 1
#include <Servo.h>
Servo fingerServo;
void setup() {
  fingerServo.attach(12);  // Attaches switchboard
}
Servo thumbfingerServo;  // create servo object to control the robotic finger
int clockwiseSwitch = 2;   // digital pin for the clockwise switch
int counterclockwiseSwitch = 3;   // digital pin for the counterclockwise switch
void setup() {
  thumbfingerServo.attach(22);  // attaches the servo on pin 22
  pinMode(clockwiseSwitch, INPUT); // set the clockwise switch pin as an input
  pinMode(counterclockwiseSwitch, INPUT); // set the counterclockwise switch pin as an input
}
// This loop will allow for the code to read the input from teh switches
void loop() {
  // Read the state of the clockwise switch (HIGH or LOW)
  int clockwiseState1 = digitalRead(clockwiseSwitch);
  // Read the state of the counterclockwise switch (HIGH or LOW)
  int counterclockwiseState1 = digitalRead(counterclockwiseSwitch);
  // Move the finger based on the switch states
  while (clockwiseState1 == HIGH && thumbfingerServo.read() < 180) {
    moveFingerClockwise();
    clockwiseState1 = digitalRead(clockwiseSwitch);
  }
  while (counterclockwiseState1 == HIGH && thumbfingerServo.read() > 0) {
    moveFingerCounterclockwise();
    counterclockwiseState1 = digitalRead(counterclockwiseSwitch);
  }
}
void moveFingerClockwise() {
  thumbfingerServo.write(thumbfingerServo.read() + 1);  // Increment the current position
  delay(50); // Add a delay for smoother movement
}
void moveFingerCounterclockwise() {
  thumbfingerServo.write(thumbfingerServo.read() - 1);  // Decrement the current position
  delay(50); // Add a delay for smoother movement
}

If you have any idea as to why the delay can take 5 second or up to a few minutes, please send recommendations. Is it something I forgot to add or is there a problem with what is written?

Post your sketch in text form.

  1. Edit your post
  2. Delete the graphic
  3. Copy your original sketch
  4. Click < CODE > in the message box
  5. Paste your code where you see '''type or paste code here'''

Do not use setup1() to attach the servo. You never call setup1(). Put it in setup() like thumbfingerServo.

Your problem is probably a power supply issue.

1 Like

Post an annotated schematic showing how this is wired. Be sure to include all power sources and links to technical information.

For the setup(), should I add numbers to the setup() if the same code is used for each finger

"setup()" is for configuring hardware (associating hardware to pins, setting pins).

The "same code" can be used for different servos by identifying the servo you want to configure or move.

You have "fingerServo.attach(12)" and "thumbfingerServo(22) which identify the pins used to command each servo.

To move a servo you write a value (angle for example) to the servo (fingerServo or thumbfingerServo).

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.