I am new to Arduino and programming. I am making a prosthetic hand with two fingers being acuated along an axis with a hobby servo, one for each finger (two servos). I was hoping someone could help me with the coding portion. One servo is connected to pin 9 and the other to pin 10, I am using an Arduino Uno as the board. I have a push button connected to pin 2, it is pulled down. I was looking for something along the lines of if i push the button than both servos will turn. (one clockwise, the other counter) but it has to stop once it reaches a pre determined position (I can mess around with this on my own) else they return to an open position. So basically if the button = HIGH then servo one will move to (x) and the other servo will move to (y) else they go back to resting position. Any help with this is appreciated.
Do you have any code so far? might want to start reading up on the arduino servo library.
This is what i tried, but I'm not sure if it's right. It won't compile I assume the syntax is wrong, I just don't know it enough it know what's wrong.
#include <Servo.h>
Servo myservo1;
Servo myservo2;
int pos = 0;
const int buttonPin1 = 1;
int buttonState1 = 0;
void setup() {
myservo1.attach(9);
myservo2.attach(10);
}
void loop() {
buttonState1 = digitalRead(1);
buttonState2 = digitalRead(2);
if (buttonState1 ==HIGH) {
myservo1.write(pos);
delay(15);
for (pos = 90);
}
}
else {
myservo1.write(pos);
delay(15);
for (pos = 0)
}
}
well the first thing people are going to teach you here is how to use the code tags
#include <Servo.h>
Servo myservo1;
Servo myservo2;
int pos = 0;
const int buttonPin1 = 1;
int buttonState1 = 0;
void setup() {
myservo1.attach(9);
myservo2.attach(10);
}
void loop() {
buttonState1 = digitalRead(1);
buttonState2 = digitalRead(2);
if (buttonState1 ==HIGH) {
myservo1.write(pos);
delay(15);
for (pos = 90);
}
}
else {
myservo1.write(pos);
delay(15);
for (pos = 0)
}
}
alright i made a few changes to get it to compile, read the comments I wrote in if you have any question let me know
#include <Servo.h>
Servo myservo1;
Servo myservo2;
int pos = 0;
const int buttonPin1 = 1;
int buttonState1 = 0;
int buttonState2 = 0;
void setup() {
myservo1.attach(9);
myservo2.attach(10);
}
void loop() {
buttonState1 = digitalRead(1);
buttonState2 = digitalRead(2);
if (buttonState1 ==HIGH) {
myservo1.write(pos);
delay(15);
if (pos = 90){
//changed to if statement probably don't want for loop
// maybe code here?
}
}
else {
myservo1.write(pos);
delay(15);
if (pos = 0){
// changed to if statment probably don't want
// for loop
//possibly some code here?
}
}
}
int pos = 0;
Why an int when a byte would suffice ? Note that pos is never changed in the code so will remain at zero
const int buttonPin1 = 1;
Why an int when a byte will suffice ? Probably better not to use pin 1 because it is used by the Serial interface which may be needed for debugging.
int buttonState1 = 0;
int buttonState2 = 0;
Why 2 button state variables ? Why ints ? Why not use HIGH/LOW ?
No pinMode()s in setup(). Not strictly necessary because pins default to being inputs but using pinMode() makes the purpose of the pins more obvious.
buttonState1 = digitalRead(1);
buttonState2 = digitalRead(2);
Why read the state of 2 pins when there is only 1 pushbutton ? Why not use the name of the pins instead of anonymous numbers ?
if (buttonState1 == HIGH) {
myservo1.write(pos);
delay(15);
Only writing to 1 servo not both.
if (pos = 90) {
}
An attempt (failed) to test the value of pos then do nothing anyway
else {
myservo1.write(pos);
delay(15);
Whether or not the button is pressed move the (one) servo to the same position
if (pos = 0) {
}
Another attempt (failed) to test the value of pos then do nothing anyway
Thank yo guys so much for the help, I edited the code that Zbay uploaded a little bit and now both of the servos are working the way I need them to.
#include <Servo.h>
Servo myservo1;
Servo myservo2;
int pos = 0;
const int buttonPin1 = 1;
int buttonState1 = 0;
int buttonState2 = 0;
void setup() {
myservo1.attach(9);
myservo2.attach(10);
}
void loop() {
buttonState1 = digitalRead(1);
if (buttonState1 ==HIGH) {
myservo1.write(170);
myservo2.write(100);
delay(15);
}
else {
myservo1.write(110);
myservo2.write(170);
delay(15);
}
}
Well done for getting it working.
Note my comments in post #4 as many of them are still relevant to your code.
So more like this?
#include <Servo.h>
Servo myservo1;
Servo myservo2;
const int buttonPin1 = 2;
int buttonState1 = 0;
void setup() {
myservo1.attach(9);
myservo2.attach(10);
}
void loop() {
buttonState1 = digitalRead(1);
if (buttonState1 ==HIGH) {
myservo1.write(165);
myservo2.write(100);
delay(15);
}
else {
myservo1.write(105);
myservo2.write(180);
delay(15);
}
}
Changed the input pin to 2
#include <Servo.h>
Servo myservo1;
Servo myservo2;
const int buttonPin1 = 2;
int buttonState1 = 0;
void setup() {
myservo1.attach(9);
myservo2.attach(10);
}
void loop() {
buttonState1 = digitalRead(2);
if (buttonState1 ==HIGH) {
myservo1.write(165);
myservo2.write(100);
delay(15);
}
else {
myservo1.write(105);
myservo2.write(180);
delay(15);
}
}
Changed the input pin to 2
But still not using the name you gave it and still using ints where bytes would do.
These comments will, I am sure, seem very picky, especially when the program works as it is. However, it is best to get using good practice for when you write larger programs. For instance, in order to change the input pin to 2 you had to do it in 2 places whereas if you had used its name in digitalRead() you would have only needed to change it where the variable is declared. Not too difficult in this program but imagine that it was sever hundred (or more) lines long.
Your code layout could do with some attention too. There are far too many blank lines and the indentation is not consistent
Here it is with some blank lines removed, Auto Format (Ctrl/T in the IDE) applied and in code tags for the forum to make the code easier to copy to an editor
#include <Servo.h>
Servo myservo1;
Servo myservo2;
const int buttonPin1 = 2;
int buttonState1 = 0;
void setup()
{
myservo1.attach(9);
myservo2.attach(10);
}
void loop()
{
buttonState1 = digitalRead(2);
if (buttonState1 == HIGH)
{
myservo1.write(165);
myservo2.write(100);
delay(15);
}
else
{
myservo1.write(105);
myservo2.write(180);
delay(15);
}
}
I have also put each brace on its own line so that the program blocks stand out more but that is a personal preference of mine.