servo push-button control

Robin2:
Compare your code carefully with the code in my Reply #7. Note how I used ELSE

...R

I'm having a little bit of trouble understanding what exactly is going on in your sketch vs mine. Could you annotate your sketch more or explain the difference to me please?

PaulS:
Do you want to move the servo when the switch IS pressed? Or, when the switch BECOMES pressed? Big difference between the 2 conditions.

Also not aware of the difference between these two conditions, if you could explain it to me or direct me to a source where I can read up on the subject that would be great.

My sketch is working how I want it to but I'm definitely open to perfecting it.

Also not aware of the difference between these two conditions

If you do not understand the difference between IS pressed and BECOMES pressed, then no amount of code is going to be able to explain it.

PaulS:
If you do not understand the difference between IS pressed and BECOMES pressed, then no amount of code is going to be able to explain it.

Could you explain it in sentence form then? This is not my expertise by any means, I work in HVAC/R just to give you an idea of where I'm coming from. Arduino is like 1/1000 of my life so my understanding of a push button switch comes from door switches on furnaces or refrigerators. Either they're closed and allow current flow or they're open and break the path. I only understand switches as either open or closed. I am quite interested in the difference between IS and BECOMES though, so if you could give me some insight I would really appreciate it

Either they're closed and allow current flow or they're open and break the path.

Sure. But, at some point, the switch was not closed, and is now closed, or was closed and is now not closed.

There are times when you want to do something only when the switch changes state.

Maybe saying "is now pressed", or WAS pressed would be clearer. :slight_smile:

jonasjohnson:
I'm having a little bit of trouble understanding what exactly is going on in your sketch vs mine. Could you annotate your sketch more or explain the difference to me please?

In the code in Reply #7 I do one thing if buttonA is pressed, another thing if buttonB is pressed and a third thing if neither is pressed.

NOTE - I had a mistake (I had buttonA in two places) which I have now corrected - apologies if that confused you.

...R

so you're referring to a change of state rather than the state itself? I think I'm understanding, but how would this correlate to my current sketch?

Robin2:
In the code in Reply #7 I do one thing if buttonA is pressed, another thing if buttonB is pressed and a third thing if neither is pressed.

NOTE - I had a mistake (I had buttonA in two places) which I have now corrected - apologies if that confused you.

...R

Okay, that makes more sense. To my understanding, my sketch operates the same way but without "else" statements. What is the purpose and benefit of using "else" statements rather than all "if" statements?

jonasjohnson:
What is the purpose and benefit of using "else" statements rather than all "if" statements?

Think about it.

Using ELSE ensures that only one of the things can happen.

...R

I've thought about it and watched a few videos on if ,else if, and else statements but I'm not quite grasping what effect it would have on the performance of the buttons/servo. Maybe I should experiment and observe its effects, like if I accidentally pushed both buttons.

Maybe I should experiment and observe its effects, like if I accidentally pushed both buttons.

Now, that is the best idea you have had in this entire thread. Do it!

Don't think of it as "accidentally" pressing both buttons. Think more in terms of defensive programming. What should happen if the clueless doofus using this program does just that?

No, no, I didn't mean you. Really. It's just that that is the mindset you need to be a good programmer.

PaulS:
Really. It's just that that is the mindset you need to be a good programmer.

+1

...R

update: I made a focus control using a dc motor connected to the focus ring with a rubber hair tie as a belt. I'm using a motor shield (the one with designated pins for speed, direction, break etc.) both zoom and focus controls worked fine as separate sketches but when I combined the two, the servo control for the zoom didn't function properly anymore. Its gets sent and stuck at some unknown angle that doesn't seem to correlate to any values in my sketch, and the buttons meant to control the servo no longer have any affect. The dc motor control still works fine in the combined sketch. My shot in the dark is that my overuse of IF statements is causing this.

#include<Servo.h>

Servo myservo;   //create servo object to control servo
int pos = 78;    //set initial servo position to 78 deg

//zoom control i/o pins
int buttonApin = 6; 
int buttonBpin = 7;

//focus control i/o pins
int buttonCpin = 2;
int buttonDpin = 4;
int motorSpeed = 3;
int motorBreak = 9;
int motorDirection = 12;

//variables for reading pushbutton status          
int buttonAstate = 0;   
int buttonBstate = 0;
int buttonCstate = 0;
int buttonDstate = 0;

void setup() {

//zoom control setup
  myservo.attach(10);                //attach servo to pin 10
  pinMode(10, OUTPUT);               //initialize servo pin as an output
  pinMode(buttonApin, INPUT_PULLUP); //initialize button A pin as a pull up input
  pinMode(buttonBpin, INPUT_PULLUP); //initialize button B pin as a pull up input

//focus control setup
  pinMode(motorBreak, OUTPUT);       //initialize break pin as an output 
  pinMode(motorSpeed, OUTPUT);       //initialize speed pin as an output
  pinMode(motorDirection, OUTPUT);   //initialize direction pin as an output
  pinMode(buttonCpin, INPUT_PULLUP); //initialize button C pin as a pull up input
  pinMode(buttonDpin, INPUT_PULLUP); //initialize button D pin as a pull up input
}

void loop() {

//zoom control loop
  buttonAstate = digitalRead(buttonApin); //read state of button A 
  buttonBstate = digitalRead(buttonBpin); //read state of button B 
  
  
  if (buttonAstate == LOW) {  //check if button A is pressed 
    myservo.write(98);        //if it is rotate servo to 98 degrees                
  }
  
  if (buttonBstate == LOW) {  //check if button B is pressed
    myservo.write(58);        //if it is rotate servo to 58 degrees
  }
  
  if ((buttonAstate == HIGH) && (buttonBstate == HIGH)) { //check if buttons arent pressed
    myservo.write(78);                                    // if they arent rotate servo to 78 deg
  }

//focus control loop
  buttonCstate = digitalRead(buttonCpin); //read state of button C
  buttonDstate = digitalRead(buttonDpin); //read state of button D
 
 if(buttonCstate == LOW) {            //check if button C is pressed
  digitalWrite(motorDirection, HIGH); //if it is turn motor forward &
  digitalWrite(motorBreak, LOW);      //turn off break &
  analogWrite(motorSpeed, 225);       //set motor speed (PWM)
 }
 if(buttonDstate == LOW) {           //check if button D is pressed
  digitalWrite(motorDirection, LOW); //if it is turn motor backward &
  digitalWrite(motorBreak, LOW);     //turn break off &
  analogWrite(motorSpeed, 225);      //set motor speed (PWM)
 }
 if((buttonCstate == HIGH) && (buttonDstate == HIGH)) { //check if buttons arent pressed 
  digitalWrite(motorBreak, HIGH);                       //if they arent turn break on 
 }
 delay(100); //delay 100 ms
}

It looks like the servo is intended to return to 78° as soon as both buttons are released - is that correct?

Think about what will happen if you press BOTH buttons A and B. You should be using an ELSE to avoid that.

If those ideas don't help then I suspect the other motor is drawing too much power and upsetting the system. You have not given any details of how everything is powered. NEITHER the servo or the motor should be powered through the Arduino

...R

Oh right! the servo is a little baby one being powered by the arduino, the bipolar/unipolar dc motor is powered by a separate 5v supply. I figured power could be an issue for the servo, it worked fine being powered by the arduino before I combined the sketches and components. since I'm using a motor shield I think it would make sense to have the servo being operated by said shield and using that separate 5v source that the dc motor is using. I'll try that before messing with the IF statements of the code, as it works fine for testing, but if it still doesn't work then that'll narrow it down to a coding issue. The servo is intended to turn to 78 degrees as a home position for start up and when the buttons are released. The servo sketch worked exactly how I intended before I added the motor shield and code for the dc motor minus the lack of defensive programming for pressing both buttons at once.

I just tried using a power source separate from the arduino and the servo is still acting up. My only other ideas are that the repetitive IF statements are causing this or theres something wrong with my arduino.

I decided to use two boards because I'm impatient and want to get this project done. I'm using a genuine uno and motor shield to drive the dc motor, and I just got a genuine nano to control the servo. I have no issues uploading to the uno but when I try to upload to the nano it seems the programmer is not responding according to the error message. I made sure the nano is being recognized by my computer and I tried re-downloading the proper driver but still get the same message. what now?

Just so you know I am not ignoring you, I don't have any nanos so I have no experience of them.

...R

(deleted)