Servo operated by push button

It's not going to work if you keep reading button1 into press1 then checking press3 or 4.

And connecting both button4 and the servo to the same pin is a bit silly.

Also doing those digitalWrites to pins that the buttons aren't connected to won't help much either.

Then there's the fact that servo.write() only takes values from 0 to 180 and these are not real angles. 180 just tells the servo to go as far as it can. So if your servo's max angle is 270 then write(180) will send it to 270 and you'll need to work out the intermediate values from there.

Steve

I am sorry I do not understand code as a compleat beginner to this, so thought i will have a go. I do need a lot of help with this code. if any one can sort it out for me it would so good, then i can get a project finished.

Only time i have written code is on my very old VIC20 C64 about 35 years ago.
software to me is a black art. i am a hardware engineer, cant stone me for trying lol

But please can someone help me.
Mike

"But please can someone help me."

So what have you actually tried yourself? You only need two wires to touch together to simulate a switch. You don't even need a servo, just print the servo control numbers to the serial monitor. You might try using the forum search in the upper right of this page and searched for "servo button"?

MikeMCA:
I am sorry I do not understand code as a compleat beginner to this, so thought i will have a go. I do need a lot of help with this code. if any one can sort it out for me it would so good, then i can get a project finished.

The code you posted was nearly there. So which part of the detailed help I tried to give you are you having trouble with?

Ask questions and you will get answers but it's not very likely that anyone is going to want to just write all your code for you.

Steve

//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 = 90;

int button3 = 6; //button pin, connect to ground to move servo
int press3 = 100;

int button4 = 7; //button pin, connect to ground to move servo
int press4 = 180;

Servo servo1;

void setup()
{
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(button4, INPUT);

servo1.attach(7);

digitalWrite(1, HIGH); //enable pullups to make pin high
digitalWrite(2, HIGH); //enable pullups to make pin high
digitalWrite(3, HIGH); //enable pullups to make pin high
digitalWrite(4, HIGH); //enable pullups to make pin high
}

void loop()
{
press1 = digitalRead(button1);
if (press1 == LOW)
{
servo1.write(0);
}

press2 = digitalRead(button2);
if (press2 == LOW)
{
servo1.write(90);
}

press3 = digitalRead(button3);
if (press3 == LOW)
{
servo1.write(100);
}

press4 = digitalRead(button4);
if (press4 == LOW)
{
servo1.write(180);
}

}

press3 and press4 are still being set by reading button1.

Hi, Slipstick I have adjusted the code a bit.
can someone high light the errors maybe...
I realise that no one will write the code for me, I do not expect them too do it , I have to learn from somewhere.
bearing in mind it is only one servo and four switches.
these bits

And connecting both button 4 and the servo to the same pin is a bit silly.

Also doing those digital Writes to pins that the buttons aren't connected to won't help much either.

Mike

oh yes bugger thought i changed that
changed it now

Post your latest version. Does it function as you expect?

Repost the code, don't edit earlier posts so that people's comments now look stupid!

And you're still saying that both button4 and servo1 are on pin 7. What pins are they really connected to?

And you're still doing digitalWrites to pins 1,2,3 and 4. Your buttons are connected to pins 4,5,6 and 7 at the moment.

Steve

is this any better

de: [Select]//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 = 90;

int button3 = 6; //button pin, connect to ground to move servo
int press3 = 100;

int button4 = 7; //button pin, connect to ground to move servo
int press4 = 180;

Servo servo1;

void setup()
{
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(button4, INPUT);

servo1.attach(1);

digitalWrite(4, HIGH); //enable pullups to make pin high
digitalWrite(5, HIGH); //enable pullups to make pin high
digitalWrite(6, HIGH); //enable pullups to make pin high
digitalWrite(7, HIGH); //enable pullups to make pin high
}

void loop()
{
press1 = digitalRead(button1);
if (press1 == LOW)
{
servo1.write(0);
}

press2 = digitalRead(button2);
if (press2 == LOW)
{
servo1.write(90);
}

press3 = digitalRead(button3);
if (press3 == LOW)
{
servo1.write(100);
}

press4 = digitalRead(button4);
if (press4 == LOW)
{
servo1.write(180);
}

}

You have the right idea but I wouldn't connect the servo to pin 1. Pins 0 and 1 are used for program loading and PC communication. Try a different pin like maybe pin 3.

So the real question is have you put all the parts together? How is the servo powered (check the comment at the top of your code)? And does it work? If it works then it counts as being pretty good.

Steve

I suggest you add some serial monitor feedback in your code similar to the below so you might see what you are doing.

void setup() {
  Serial.begin(9600);
  Serial.println("Simple serial monitor feedback"); 
}


////////////////

 press2 = digitalRead(button2);
 if (press2 == LOW)
 {
   servo1.write(90);
   Serial.println("90 sent to servo from press2")
 }

Hi slipstick, thanks for the answers it is being powered from another supply, I haven't built any thing yet just trying to get the code ok, then i will build the gambol mech.

Zoomkat i will add the code and see what happens eek. lol
can I add buttons to this code and see the results. where would you put it.... at the beginning i think?

As i said I am a complete newbie at this, but I can begin to see how this works.

Mike

de: [Select]//zoomkat servo button test 12-29-2011
// Powering servo from an other supply.

#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 = 90;

int button3 = 6; //button pin, connect to ground to move servo
int press3 = 100;

int button4 = 7; //button pin, connect to ground to move servo
int press4 = 180;

Servo servo3;

void setup()
{
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(button4, INPUT);

servo1.attach(1);

digitalWrite(4, HIGH); //enable pullups to make pin high
digitalWrite(5, HIGH); //enable pullups to make pin high
digitalWrite(6, HIGH); //enable pullups to make pin high
digitalWrite(7, HIGH); //enable pullups to make pin high
}

void loop()
{
press1 = digitalRead(button1);
if (press1 == LOW)
{
servo3.write(0);
}

press2 = digitalRead(button2);
if (press2 == LOW)
{
servo3.write(90);
}

press3 = digitalRead(button3);
if (press3 == LOW)
{
servo3.write(100);
}

press4 = digitalRead(button4);
if (press4 == LOW)
{
servo3.write(180);
}

}

Wrong 3. That's just the servo NAME and you can call a servo anything like. It's the pin number in the attach() that's important.

And please at least try to compile your code. You're just wasting a lot of time if you keep posting stuff that won't even compile like that last effort. If it won't compile you know for a fact that it won't work.

Steve

OK Steve I am very sorry for it seems wasting your time, thank you for any ... help.
i will now quit this forum, i dont know where to go from here, it has made me feel like an idiot, seems like I have done every thing wrong even posting. how can I compile a code if I dont even have it to compile.

Just to get back to basics, to test the below code you only need a piece of wire to touch between pin 4 and ground. The serial monitor will show the result.

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
Servo servo1;

void setup()
{
  pinMode(button1, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
  Serial.begin(9600);  
  Serial.println("Booting up for the Big Adventure!");
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(160);
    Serial.println("Servo going to 160!");
    delay(2000);
    servo1.write(20);
    Serial.println("And back to 20");
  }
}

Zoomkat thank you for that, i did some messing with the code and got it to compile, no errors. I will upload to tomorrow and see if it works, sorry if I come over a bit thick but I have not played with this coding before, and just bleating out instructions is not a way forward that is OK if you know what to look for, if you know what I mean.

Mike

It's a very common mistake among beginners to try and write the whole program out and then test it. As you've seen, it's very easy to create bugs in what appears to be a trivial piece of code.

Once you have Zoomcat's example working, I suggest that you return to your code and get rid of as much of it as you can. Just get one button working - the servo will take a default position on startup, have a single button that moves it somewhere else. When that works, add a button etc. etc.

That isn't a beginner specific technique. It's what experienced folks tend to do - they know that they won't get it right the first time and a small change is easier to debug. Eventually, hopefully, you'll develop a feeling of unease when you have added twenty lines of code but not tested it :wink: