Two switch servo control

Hey guys
still pretty new at this but I'm starting to try to come up with stuff on my own now to see if I understand the language. my goal with this one was to have a "yes/no" pointer servo with associated LEDs that would be controlled by two seperate buttons. this is the code I came up with. The yes function works perfectly, hit the button the green LED comes on the pointer moves to the left, waits two seconds then returns to the middle. However my no function doesn't work well, it moves to the no position then stays there and keeps the red LED on. I've checked the serial monitor and it registers the button pin as 1 for a moment then goes back to zero as I'd expect but the servo and LED don't return to their previous state. What am I missing here yall?

#include <Servo.h>
Servo myServo;

int const yesPin = 12;
int const noPin = 7;
int const yesLED = 4;
int const noLED = 2;

int const baseAngle = 90;
int const yesAngle = 45;
int const noAngle = 135;
int angle;
int yesState;
int noState;


void setup() {
 Serial.begin(9600);
 pinMode(12, INPUT);
 pinMode(10, INPUT);
 pinMode(4, OUTPUT);
 pinMode(2, OUTPUT);
 myServo.attach(9);
 myServo.write(baseAngle);
}

void loop() {
 yesState = digitalRead(yesPin);
 Serial.print("yesPin:");
 Serial.println(yesState);
 noState = digitalRead(noPin);
 Serial.print("\t noPin:");
 Serial.println(noState);

  while(yesState == HIGH){
  digitalWrite(yesLED, HIGH);
  digitalWrite(noLED, LOW);
  myServo.write(yesAngle);
  delay(2000);
  break;
 }

  while(noState == HIGH){
  digitalWrite(yesLED, LOW);
  digitalWrite(noLED, HIGH);
  myServo.write(noAngle);
  delay(2000);
  break;
 }

  while(yesState&noState == LOW){
  digitalWrite(yesLED, LOW);
  digitalWrite(noLED, LOW);
  myServo.write(baseAngle);
  break;
 }
}

while() ?

This is unnecessary "Mensa puzzle" code, make it readable, there is no justification for it, I suggest

  while(yesState == LOW && noState == LOW){

As implied by the previous post, abandon 'while...break', use 'if'.

You clearly don't.

I appreciate your edit suggestions they corrected the issue I was experiencing. Let me return the favor by suggesting you not make unnecessary comments like "you clearly don't" when it's pretty obvious I'm new to this. Look at that we both brought something to the table :smiling_face_with_three_hearts:

It is only a statement of fact. It seemed like finding your skill level was part of your experiment.

that I agree with you on

It's often helpful to compare your own code with that of other people, to see how language features are used.

You are lucky that INPUT is the default pin state:

You defined names for most of the pins but didn't use the names in setup().

My recommendations:

#include <Servo.h>
Servo myServo;

int const YesPin = 12;
int const NoPin = 7;
int const YesLED = 4;
int const NoLED = 2;
int const ServoPin = 9;

int const BaseAngle = 90;
int const YesAngle = 45;
int const NoAngle = 135;

void setup()
{
  Serial.begin(9600);
  pinMode(YesPin, INPUT);
  pinMode(NoPin, INPUT);
  pinMode(YesLED, OUTPUT);
  pinMode(NoLED, OUTPUT);
  myServo.attach(ServoPin);
  myServo.write(BaseAngle);
}

void loop()
{
  int YesState = digitalRead(YesPin);
  Serial.print("YesPin:");
  Serial.println(YesState);
  int NoState = digitalRead(NoPin);
  Serial.print("\t NoPin:");
  Serial.println(NoState);

  if (YesState == HIGH)
  {
    digitalWrite(YesLED, HIGH);
    myServo.write(YesAngle);
    delay(2000);
    myServo.write(BaseAngle);
    digitalWrite(YesLED, LOW);
  }

  if (NoState == HIGH)
  {
    digitalWrite(NoLED, HIGH);
    myServo.write(NoAngle);
    delay(2000);
    myServo.write(BaseAngle);
    digitalWrite(NoLED, LOW);
  }
}

here's the code I ended up with that worked great. thanks for the help yall!

#include <Servo.h>
Servo myServo;

int const yesPin = 12;
int const noPin = 7;
int const yesLED = 4;
int const noLED = 2;

int const baseAngle = 90;
int const yesAngle = 45;
int const noAngle = 135;
int angle;
int yesState;
int noState;


void setup() {
 Serial.begin(9600);
 pinMode(yesPin, INPUT);
 pinMode(noPin, INPUT);
 pinMode(yesLED, OUTPUT);
 pinMode(NoLED, OUTPUT);
 myServo.attach(9);
 myServo.write(baseAngle);
}

void loop() {
 yesState = digitalRead(yesPin);
 Serial.print("yesPin:");
 Serial.print(yesState);
 noState = digitalRead(noPin);
 Serial.print("\t noPin:");
 Serial.println(noState);
 
  if(yesState == HIGH){
  digitalWrite(yesLED, HIGH);
  digitalWrite(noLED, LOW);
  myServo.write(yesAngle);
  delay(2000);
 }

  if(noState == HIGH){
  digitalWrite(yesLED, LOW);
  digitalWrite(noLED, HIGH);
  myServo.write(noAngle);
  delay(2000);
 }

  if(yesState== LOW && noState == LOW){
  digitalWrite(yesLED, LOW);
  digitalWrite(noLED, LOW);
  myServo.write(baseAngle);
 }
}

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