Three conditions with servo, tried everything!

...everything I could think of. Now I need to ask Arduino world.

Hi all, i have a servo project that should respond to three conditions. Here is a picture of my circuit:


and code:

//This program establishes 3 servo position for 3 input conditions.
//1. 45deg when switch 1 is on and switch 2 is off.
//2. 180deg when switch 1 is off and switch 2 is on.
//3. 90deg when switch 1 and switch 2 are off.

#include <Servo.h>  //call up servo function from Arduino library
Servo motorSlave;   //define servo motor
int servoPin = 9;   //define servo signal pin
int switch1 = 8;    //switch 1 in pin 8
int switch2 = 7;    //switch 2 in pin 7
int switch1_value;  //define input 1 condition
int switch2_value;  //define input 2 condition
int servoPos1 = 0;  //define servo output
int servoPos2 = 180;
int servoPos3 = 90;

void setup() {
  pinMode(switch1, INPUT);                 //switch 1 is an input
  pinMode(switch2, INPUT);                 //switch 2 is an input
  motorSlave.attach(servoPin, 645, 1575);  //servo attached through servoPin
}

void loop() {
  switch1_value = digitalRead(switch1);  //read switch 1
  switch2_value = digitalRead(switch2);  //read switch 2

  if (switch1_value == HIGH && switch2_value == LOW) {  //condition 1
    motorSlave.write(servoPos1);
  }
  if (switch1_value == LOW && switch2_value == HIGH) {  //condition 2
    motorSlave.write(servoPos2);
  }
  if (switch1_value == LOW && switch2_value == LOW) {  //condition 3
    motorSlave.write(servoPos3);
  }
}

The servo violently gyrates in condition three. I really have tried hundreds of possible solutions with this thing. The only thing that will cause the servo to go to 90 is connecting the ground to my servo control line.
Alternatively the command 'while' works for the third condition but it wont recognize the other two conditions after words.
image

Can anyone tell what is happening here? Thank you!!

  • Pin 8 is floating

  • Use pinMode(mySwitches, INPUT_PULLUP);

  • Your Arduino board 5 volt supply is not adequate to power your project. If you continue to use it as such, your Arduino may be damaged. Also, never connect an inductive load to the Arduino 5v pin.

  • In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.
    Use the < CODE / > icon from the ‘posting menu’ to attach the copied sketch.
    No images of code.

FYI

3 Likes

in order for any condition to persist, the switch causing the condition must be held. If you want to press the button and have the condition persist after the button is released, look into the state change detection method.

Larry D, How exactly do you mean pin 8 is floating? How exactly is pin 8 floating in some sense that pin 7 is not?

None of your solutions worked btw. It was as simple as adding a ground to the switches that didn't have a hot lead.

Im a little disappointed in the response from this forum. Nothing helpful except overly didactic ego-driven dribble which did nothing but take my attention away from the actual solution.

larry D your response was particularly egregious. Do the world a favor and just be silent when you have nothing to offer.

You may need to find another forum that suits your needs.

Remember that this forum is supported by volunteer members and does not offer programming or system design services for free.

Have a nice day and enjoy coding in C++.

It is not connected to anything except a wire, which acts as an antenna to pick up electrical noise.

With that attitude, best of luck with the Arduino learning experience!

There are a total of four different combinations of high/low on the two inputs.
You need to consider all four, not just three.

What will happen if both switches are high?

Thus preventing it from floating.

@cnknoblauch is enjoying a short break from the forum

2 Likes

buttons are typically connected between the pin and ground, the pin configured as INPUT_PULLUP to use the internal pullup resistor which pulls the pin HIGH and when pressed, the button pulls the pin LOW.

without either a pull-up/down the pin floats

You cannot drive main servo power via the Uno regulator, or for that matter, successfully via a breadboard.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.