I am trying to make a Vex claw(attachment) open and close with a gear attached to a continuous servo. The problem with continuous servos of course is that they are continuous. I want the motor to spin one direction while I am pressing the attached button and the other direction while I'm not pressing the button. I also want the motor to spin for a certain time while pressing the button so that when the claw opens up completely the motor doesn't keep trying to open the claw. Also when the claw is completely closed I don't want the motor trying to close it further while the claw is already closed. I know it's kind of confusing, but I don't know how else to explain it.
Here is the code:
#include <Servo.h>
const int button1 = 2;
Servo VexM;
int val;
void setup() {
VexM.attach(9);
pinMode(button1, INPUT);
Serial.begin(9600);
}
void loop() {
val = digitalRead(button1);
if (val == LOW) {
VexM.write(20);
}
if (val == HIGH) {
VexM.write(160);
}}
I also tried this, but it just made the problem worse:
#include <Servo.h>
const int button1 = 2;
Servo VexM;
int val;
void setup() {
VexM.attach(9);
pinMode(button1, INPUT);
Serial.begin(9600);
}
void loop() {
val = digitalRead(button1);
if (val == LOW) {
VexM.write(20);
delay(1000);
VexM.write(90);
}
if (val == HIGH) {
VexM.write(160);
delay(1000);
VexM.write(90);
}}