Gripper with push button


I’ve built an arm with a gripper and I’ve add a push button on one of the side of the gripper. When the gripper is closing, the push button is press down by the object. How would add this to my code to stop the gripper from crushing my object? Here is my current code for my gripper, it’s simple.

void Armextend()
    for(grab = 0; grab < 180; grab += 1){           // Open gripper                            
      delay(1000);  // more code follows after this line

Connect 1 side of the push button to ground. Connect the other side to an input pin. Enable the internal pullup resistor: in void setup() do this: pinMode(inputX, input); digitalWrite(inputX, HIGH);

In your loop, you would then read capture the state of the button and act on it:

state_inputX = digitalRead(inputX);

if(state_inputX == 1); normally open button? {} // do nothing, button is not pressed. else {//stop squeezing! }

Thank you for the response. I did a ruff sketch to understand it a bit better. I don’t know how I would stop the servo if the button is pressed. Can you see if my coding is correct and what would I add to my else statement.

#include <Servo.h>;
Servo G;    //Gripper
int button;  //Normally opened push button
int grab;

void setup (){
  pinMode(button, 1);
  digitalWrite(button, HIGH);

void loop(){
int state_button; 
G.attach(5); //Gripper
    state_button = digitalRead(button);

    if (state_button == 1)
        for(grab = 0; grab < 180; grab += 1){           // Open gripper                            
     else { }  //how would I write it to stop the servo?      

after this


Put these state_button = digitalRead(button); if (state_button == 0){grab=180;} // should kick you out of the for:next loop

Excellent my friend!!! It worked like a charm. One more question. Knowing that it stop quickly, how could I write it so that it squeezes a bit but than stop. I would like it to have a good grip on the object, some sort of the delay after its pressed.

change to grab=175 so it squeezes a few more times?