Can you please have a look at my program and send me in the right direction for making it work? The grippers works fine, but not the base.
Thank you
#include <Servo.h> // include the servo library
const int gripperclose = 4; // to close the gripper, connected to pin 4
const int gripperopen = 3; // to open the gripper, connected to pin 3
const int movebaseleft = 6; // move the base to the left, connected to pin 6
const int movebaseright = 7; // move the base to the right, connected to pin 7
int pos = 90; // the servo will start at the position 90 degrees
int valgripperopen = 0; // set the value to zero for the gripper opening button
int valgripperclose = 0; // set the value to zero for the gripper closing button
int valbaseleft = 0; //set the value to zero for the base to move left
int valbaseright = 0; // set the value to zero for the base to move right
Servo servoGrip; // enable the gripper servo
Servo servoBase; // enable the base servo
void setup(){
servoGrip.attach(12); // connect the gripper servo to pin 5
servoBase.attach(8); // connect the base servo to pin 8
pinMode(gripperopen,INPUT); // the open gripper button is set as an input
pinMode(gripperclose,INPUT); // the close gripper button is set as an input
pinMode(movebaseleft,INPUT); // the move the base left button is set as an input
pinMode(movebaseright, INPUT); // the move the base right button is set as an input
}
void loop(){
valgripperopen = digitalRead(gripperopen);
valgripperclose= digitalRead(gripperclose);
valbaseleft = digitalRead(movebaseleft);
valbaseright = digitalRead(movebaseright);
if(valgripperclose == HIGH){
servoGrip.write(pos -= 2); // if the gripper left button is press, the servo will open the gripper by increments of 2 degrees
delay(40); //
}else if(valgripperopen == HIGH){
servoGrip.write(pos += 2); // if the right button is press, the servo will close the gripper by increments of 2 degrees
delay(40);
if(valbaseleft == HIGH){
servoBase.write(pos -= 2); // if the base left button is press, the servo will move the base to the left by increments of 2 degrees
delay(40); //
}else if(valbaseright == HIGH){
servoBase.write(pos += 2); // if the base right button is press, the servo will move the base to the right by increments of 2 degrees
delay(40);
}
}
}
I'm building a robotic arm with a gripper. The arm will have 4 servos total. With the program above, the gripper is opening and closing as it's suppose too. The servo that controls the base is suppose to go at 90 degree and move to the right or to the left when the appropriate push buttons are pushed. The servo that controls the base us not responding.
I think @AWOL meant that you should put some Serial.println() lines into your code so that you can see whether the variables have the values you expect them to have.
You might want to lake the button pins high then pull them low to ground thru the buttons.
//zoomkat servo button test 12-29-2011
// Powering a servo from the arduino usually *DOES NOT WORK*.
#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
int button2 = 5; //button pin, connect to ground to move servo
int press2 = 0;
Servo servo1;
void setup()
{
pinMode(button1, INPUT);
pinMode(button2, INPUT);
servo1.attach(7);
digitalWrite(4, HIGH); //enable pullups to make pin high
digitalWrite(5, HIGH); //enable pullups to make pin high
}
void loop()
{
press1 = digitalRead(button1);
if (press1 == LOW)
{
servo1.write(170);
}
press2 = digitalRead(button2);
if (press2 == LOW)
{
servo1.write(10);
}
}
I can’t remember if the below worked, but it does print to the serial monitor for debugging issues.
//zoomkat servo button sweep test 12-23-2013
// Powering a servo from the arduino usually *DOES NOT WORK*.
#include <Servo.h>
int button1 = 5; //button pin, connect to ground to move servo
int press1 = 0;
int button2 = 6; //button pin, connect to ground to move servo
int press2 = 0;
Servo servo1;
int pos = 90; // variable to store and set the servo position
void setup()
{
Serial.begin(9600);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
servo1.attach(7);
servo1.write(pos); //starting position
digitalWrite(5, HIGH); //enable pullups to make pin high
digitalWrite(6, HIGH); //enable pullups to make pin high
Serial.println("servo button sweep test 12-23-2013");
}
void loop()
{
press1 = digitalRead(button1);
if (press1 == LOW)
{
pos=(pos+1);
if(pos>180) pos=180; //limit upper value
Serial.println(pos); //for serial monitor debug
servo1.write(pos); // tell servo to go to position in variable 'pos'
delay(150); // waits 150ms to slow servo movement
}
press2 = digitalRead(button2);
if (press2 == LOW)
{
pos=(pos-1);
if(pos<0) pos=0; //limit lower value
Serial.println(pos); //for serial monitor debug
servo1.write(pos); // tell servo to go to position in variable 'pos'
delay(150); // waits 150ms to slow servo movement
}
}