servo push-button control

I've watched many tutorials but all of them are over complicated and do way more than what I'm looking to do.

What I want to do:

(2 buttons)
Push button 1: servo rotates to (x) degrees
Release button: servo returns to its initial starting point
Push button 2: servo rotates in opposite direction to (x) degrees
Release button: servo returns to its initial staring point

Thats all

Its for my heated camera enclosure. This function is to control the zoom rocker on my camera with a servo, with buttons from the outside.

I understand no one is looking to write the code for me, although that would be nice, I wouldnt learn anything from it.

I've read 3 books on arduino, browsed many forums and watched many tutorials. Although I do understand how arduino works I'm having trouble coding on my own and I'm looking for a little help.

If you think this is under-complicated enough to give me the backbone of the code and have me input the values I want and the pins I'll connect the buttons to, I think that would be a happy medium between doing it all for me and having me do it all on my own.

I'm trying to get this project out in the elements for winter filming soon, since its already snowing so please, anything helps. Guidance, resources, coding ideas, anything.

Thank you,
Jonas

What you want to do consists of properly wiring the switches (one leg to ground, one leg to a digital pin), setting the mode of the pin (INPUT_PULLUP), reading the state of the pin (digitalRead()), and moving the servo to some new position (servo.write(someValue)).

The key is going to be that the servo's default position is NOT going to be 0 degrees, so you must move the servo to that position when the Arduino resets AND it must be possible to do that, given the physical environment that the servo operates in.

Thank you, your reply has helped make some cognitive progress in what I need to do. Although I don't quite understand why I would have to physically move the servo at all, when in other sketches I've tried, the servo remains at whatever position it was at when the arduino is disconnected, in this case it would be the resting position when the button isn't pressed.

jonasjohnson:
when in other sketches I've tried, the servo remains at whatever position it was at when the arduino is disconnected, in this case it would be the resting position when the button isn't pressed.

You should take account of the possibility that the Arduino suffers a power outage while the servo is not at the normal resting position.

The usual way would be to send it to the rest position in setup()

...R

That is a great suggestion although a power outage is not something I'm particularly concerned about. This whole set up will be connected to a high capacity battery bank with a voltage monitor that will last 16 hours at full current rating, which is half of what I'm actually using. I do think that will be something I will add as a safety once I get the bare bones of the sketch written, so thank you for your suggestion.

It would probably help if I was more specific, lets say I have button 1 connected to pin 13, button 2 connected to pin 12 and servo connected to pin 9.

button 1 when pressed would rotate the servo to 15 degrees, when released it would go back to 0 degrees

button 2 when pressed would rotated the servo to 15 degrees in the opposite direction and when released it would go back to 0 degrees.

My issue with other peoples sketches, is that the servo remains at the position it was at when the button is released. In my case if I used that sketch, when the button is released, the camera would continue zooming in or zooming out depending on what button was pressed and released, when I would like the servo to return to 0 degrees when the button is released, for the camera to stay at its zoom position.

I know this probably seems like an easy sketch to write for someone who is well versed in the arduino world, but unfortunately I am not and have no coding skills whatsoever, only a basic understanding of how arduino operates. I am looking to advance my coding skills and hope this project will aid in my efforts to understand coding theory.

Then you need to have the zero degree command contained in the void loop, which will be sent continuously, and the other two commands placed in separate while statements, also in void loop. Look up while. The one while statement would be while button 1 is high, servo goes to 15 degrees, the other while statement would be while button 2 is high (or low, depending how you wire it), servo goes to the opposite. If 90 is the middle, then one button would drive the servo to 65 degrees, and the other to 105 degrees. While neither is pressed, the 90 degree command is continuously written to the servo, holding it at centre.

jonasjohnson:
is that the servo remains at the position it was at when the button is released.

Something like this ?

buttonAState = digitalRead(buttonApin);
buttonBState = digitalRead(buttonBpin);
if (buttonAState == LOW) {  // assumes LOW when pressed
   myServo.write(15);
}
else if (myButtonBState == LOW) {
   myServo.write(125);
}
else {
   myServo.write(70);
}

It would probably be wise only to repeat this about 5 times per second so as not to be writing servo values too often.

...R
EDIT ... 19 Nov 2018 to correct myButtonAState to myButtonBState - apologies to anyone who was confused

could I write something like:

if (pin 13, HIGH) button is pushed
{
myServo.write(15) servo rotates to 15 degrees
}
if (pin 13, LOW) button is not pushed
{
myServo.write(0) servo goes back to 0 degrees
}
and then the reverse for the other button:

if (pin 12, HIGH) button is pushed
{
myServo.write(-15) servo rotates 15 degrees in opposite direction
}
if (pin 12, LOW) button is not pushed
{
myServo.write(0) servo goes back to 0 degrees
}

feel free to correct my vocabulary and syntax, add or subtract any code.
I know theres no negative degrees but I don't know how else to write it to make the servo travel in the opposite direction rather than the same direction.

(deleted)

You can write that if you like but if you want it to do anything you can't just invent your own language. Some of your ideas look like they may be fairly close. So feel free to put a bit of effort in and write it in the language that the Arduino uses.

To discover the correct format for the various commands try Resources/Reference up at the top of the page and/or look at some of the example programs in the IDE.

One hint, a normal servo is centred at approximately write(90) so 15 one way is write(90+15) and 15 the other way is write(90-15).

Steve

jonasjohnson:
feel free to correct my vocabulary and syntax, add or subtract any code.

Have you studied the code in Reply #7 ?

...R

button 1 when pressed would rotate the servo to 15 degrees, when released it would go back to 0 degrees

button 2 when pressed would rotated the servo to 15 degrees in the opposite direction and when released it would go back to 0 degrees.

You are assuming that you can rotate the servo to -15 degrees.

You can't. You can rotate from 15 to 30 and back to 15, then from 15 to 0 and back to 15. You can not rotate to -15 degrees.

All very good responses. I will study all suggested code and everyone's responses. I'll get to writing a sketch tonight, and post it in here to see if anyone wants to pick it apart some more. Bare with my mistakes and misunderstandings about the arduino language. I know what I want to do, I just dont know how to write it. We're getting there, though. Still on the shallow end of the learning curve.

Heres my first ever attempt at an arduino sketch. Pick it apart and tell me what will and wont work, and why not. I'm open to any suggestions.

#include<Servo.h>

Servo myservo // create servo object to control servo

const int myservoPin = 9; // const wont change
const int buttonAPin = 13;
const int buttonBPin = 12;

int pos = 90; // set initial servo position to 90
int button1State = 0; // variable for reading pushbutton status
int button2State = 0;

void setup() {
serial.begin(9600);
pinMode(servoPin, OUTPUT); // initialize the servo pin as an output
pinMode(buttonAPin, INPUT); // initialize the button A pin as an input
pinMode(buttonBPin, INPUT); // initialize the button B pin as an input

}

void loop() {
buttonAState = digitalRead(buttonApin) // read the state of button A value
buttonBstate = digitalRead(buttonBpin) // read the state of button B value

if (buttonAstate = = HIGH) { // check if button A is pressed
myServo.write(105); // if it is rotate servo to 105 degrees
}

if (buttonBstate = = HIGH) { // check if button B is pressed
myServo.write(75); // if it is rotate servo to 75 degrees
}

}

You didn't pay attention to reply #1, you have to have pulldown resistors (10k) from input pin to ground so the pins don't float anywhere between 0 and 5 volts when the button is NOT pressed.

Also -

You need a few more semicolons on the end of lines.
== has no space, it's not = =.
Serial isn't spelled serial.

You need to pay more attention to variable names. E.g. decide if the pin the servo is on is called myservoPin or servoPin and if you're going to use buttonAstate or button1State etc.

And, like the serial/Serial thing, the language is case sensitive so buttonApin and buttonAPin are not the same (there are lots of examples of this).

If you had tried to verify/compile your code the compiler would already have told you about most of these. There's really very little point posting code that you haven't even bothered trying to compile. The compiler is really good at finding the syntactic mistakes.

Steve

jonasjohnson:
Heres my first ever attempt at an arduino sketch. Pick it apart and tell me what will and wont work, and why not. I'm open to any suggestions.

Before you ask for help here you should try your program. Then you can tell us if you get compiler errors or if the program works, but not exactly as you want.

Arduinos are great for learning-by-doing

...R

I appreciate the bluntness, I'm learning new things with every response. I made a few changes and this checked out in the compiler, am I getting any closer?

#include<Servo.h>

Servo myservo; // create servo object to control servo

int buttonApin = 13;
int buttonBpin = 12;

int pos = 90; // set initial servo position to 90
int buttonAstate = 0; // variable for reading pushbutton status
int buttonBstate = 0;

void setup() {
myservo.attach(9);
Serial.begin(9600);
pinMode(9, OUTPUT); // initialize the servo pin as an output
pinMode(buttonApin, INPUT_PULLUP); // initialize the button A pin as an input
pinMode(buttonBpin, INPUT_PULLUP); // initialize the button B pin as an input

}

void loop() {
buttonAstate = digitalRead(buttonApin); // read the state of button A value
buttonBstate = digitalRead(buttonBpin); // read the state of button B value

if (buttonAstate == LOW) { // check if button A is pressed
myservo.write(105); // if it is rotate servo to 105 degrees
}

else{
myservo.write(90);
}

if (buttonBstate == LOW) { // check if button B is pressed
myservo.write(75); // if it is rotate servo to 75 degrees
}

else{
myservo.write(90);
}

}

jonasjohnson:
I appreciate the bluntness, I'm learning new things with every response. I made a few changes and this checked out in the compiler, am I getting any closer?

Please use the code button </> when posting code.

Did your program compile without errors? If not, then post the error message.

If it did compile without errors did you upload the program and try it? If so, did it do what you want? If not, what exactly did it do and what do you want it to do that is different?

...R