Coding a Servo as a lock

So first off my programming skills are weak, but I have my code somewhat working. My goal is to use my touch sensor to turn my servo and when its not pressed it would go back. The code right now when I press the touch sensor and lift my finger the servo turns for a millisecond or so. I want it to move when I push it, and I don't know how to code the turning. Can anyone help? Thank you so much!!! Also in my code I used pushbutton as my touch sensor

lock.ino (683 Bytes)

You have not created a servo object, attached the servo object to a pin or written the position to the servo. Why would you expect it to move ?

Have you looked at and tried the Servo examples ?

There are examples in the IDE
Try this:

#include <Servo.h>;
Servo myservo;  // create servo object to control a servo

int position = 0;
const byte pushButton = 2;

void setup()
{
  Serial.begin(9600);
  
  pinMode(pushButton, INPUT_PULLUP);
  myservo.attach(9);  // <-----<<<<< pin 9 geos to the servo input
}

void loop()
{
  int buttonState = digitalRead(pushButton);
  //Serial.println(buttonState);
  
  if (buttonState == HIGH && position < 180)
  {
    digitalWrite(6, HIGH);
    myservo.write(180);
  }
  else
  {
    digitalWrite(6, LOW);
    myservo.write(0);
  }
  
}

@larryd
I can see what you meant to do, but your program does not update the position variable so it will always be less than 180

I just typed some new code into the OP's sketch.

You are correct, I should have just had:

if (buttonState == HIGH)

.