Combining Button and Photocell Code

Hi forum! So I'm trying to run a servo with a bumper switch, and I like what the photocell code does with the servo it does what I want. But I want to be able to do that same thing with a bumper switch instead of running it on a photocell. Could someone help me combine those two?

//include the Servo library in your sketch
#include <Servo.h>

//create a servo object called myServo
Servo myServo;
//create calibration variable
int cal;

void setup() 
{
//take a calibration value right away 
cal = analogRead(A0); 
//attach myServo to pin 9  
myServo.attach(9);
//write 90 to myServo as a pre-set angle
}

void loop() 
{
 //store the sensor value on A0 to the variable val 
 int val = analogRead(A0);

 //if val is lress than the calibration value minus 50 for sensitivity write 160 to
 //myServo, else write 20 to myServo
 if(val < cal -50)
 {
  myServo.write(160);
 }
 else
 {
  myServo.write(20);
 }
}
//include the servo library in your sketch
#include <Servo.h>

//Create a servo object called myServo
Servo myServo;
//create a global boolean variable to toggle
//when button is pressed, set it to true.
int buttonState = 0;

void setup() 
{
  //pinModes for H-Bridge control pins
  myServo.attach(9);
  //set pin 6 to INPUT_PULLUP, no need for a pullup resistor
  pinMode(6,INPUT_PULLUP);
}

void loop() 
{
 
 
 //if buttonState is 0 (less than 1) toggle the state variable to 
 //the opposite of the current state (!state)
 if(digitalRead(6)==0)
 {
 		buttonState++;
 		delay(500);
 }
 
 
 //if state is false move myServo to 145, else 20.
 if(buttonState==1)
 {
  myServo.write(40);
 }
 else if(buttonState==2)
 {
  myServo.write(60);
 }
 else if(buttonState==3)
 {
 myServo.write(120);
 }
 else if(buttonState >= 4)
 {
 myServo.write(20);	
 buttonState=0;
 }
}
 //if buttonState is 0 (less than 1) toggle the state variable to 
 //the opposite of the current state (!state)

Your code does not do that, just keeps increasing the state value and perform 4 different actions

Note sure what you exactly want to reach as an outcome. Can you be more specific, like describe in plain English what's the start conditions, what needs to happen when a condition is met?

sorry! so what is happening with the photocell, is that when it detects no ambient light it's open, and when it detects ambient light it snaps shut. that is what I want the bumper switch to do, right now what it is doing is just opening and closing and stopping altogether with the triggering of the bumper switch. basically, I want the claw to stay open and then the bumper switch shut it.

So you just want to change in code 1 the analogRead and test against a threshold into reading your button and see if it is pressed, isn't it?

your second code shows you how to detect the button is pressed