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.
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
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.
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
}
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.
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
}
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.
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.
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
}
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.
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.
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