Converting if/else to Switch Case.

Hello,
Thanks in advance for being here! Running multiple servo’s on two Uno’s with two HC-12’s.
Had the code working with two servo’s and multiple on/off’s. However when I added the 3rd and
4th all servo’s loose there mind! Seems normal really. I assume the code is way messed up for
four servo’s. You guys will probably find somethings wrong with the two servo version lol

This is the first few lines of code that Does work with two servo’s:

//------------------------------------------------------HC-12 Receive

#include <Servo.h>
Servo MyServo1;
Servo MyServo2;

int Pin2 = 2;
int Pin3 = 3;
int Pin4 = 4;
int Pin5 = 5;

void setup()
{
  Serial.begin(9600);

  MyServo1.attach(10);
  MyServo2.attach(11);

  pinMode(Pin2, OUTPUT);
  pinMode(Pin3, OUTPUT);
  pinMode(Pin4, OUTPUT);
  pinMode(Pin5, OUTPUT);
}

void loop()
{
  int input = Serial.parseInt();
  if (input < 180)
  {
    MyServo1.write(input);
    delay(40);
  }
  else if (input < 360)
  {
    MyServo2.write(input - 180);
    delay(40);
  }
  //------------------------------------------------------------
  else if (input == 2222)
  {
    digitalWrite(Pin2, HIGH);
  }
  else if (input == 2221)
  {
    digitalWrite(Pin2, LOW);
  }
  //------------------------------------------------------------
  else if (input == 3333)
  {
    digitalWrite(Pin3, HIGH);
  }
  else if (input == 3331)
  {
    digitalWrite(Pin3, LOW);
  }
  //------------------------------------------------------------
  else if (input == 4444)
  {
    digitalWrite(Pin4, HIGH);
  }
  else if (input == 4441)
  {
    digitalWrite(Pin4, LOW);
  }
  //------------------------------------------------------------
  else if (input == 5555)
  {
    digitalWrite(Pin5, HIGH);
  }
  else if (input == 5551)
  {
    digitalWrite(Pin5, LOW);
  }
}

Here is what it looks like now with four servo’s. I did not worry about the on/off functions for troubleshooting purposes.

//------------------------------------------------------HC-12 Receive

#include <Servo.h>
Servo MyServo1;
Servo MyServo2;
Servo MyServo3;
Servo MyServo4;

 

void setup()
{
  Serial.begin(9600);

  MyServo1.attach(10);
  MyServo2.attach(11);
  MyServo3.attach(12);
  MyServo4.attach(13);
  

  
   
}

void loop()
{
  int input = Serial.parseInt();
  if (input < 180)
  {
    MyServo1.write(input);
    delay(40);
  }
  else if (input < 360 || input > 180)
  {
    MyServo2.write(input - 180);
    delay(40);
  }
  else if (input < 540 || input > 360)
  {
    MyServo2.write(input - 360);
    delay(40);
  }
  else if (input < 720 || input > 540)
  {
    MyServo2.write(input - 540);
    delay(40);
  }
  
}

To me this looked like it would work. After reading more and more I realize I really need to learn
how to punctuate switch case. I need someone to show me what this would look like. Something
I have been working on with other peoples code but im not quite getting it. Thanks!

input < 360 || input > 180

This returns true for any integer except between 180 and 360. Is it what you need?

input < 360 && input > 180

This returns true for an integer between 181 and 356 inclusively.

input >= 180 && input < 360

This returns true for an integer between 180 and 356 inclusively. We are used to see the lower bound first and then the higher bound

Jacques

input < 360 || input > 180

This returns true for any integer except between 180 and 360. Is it what you need?

I think it actually always returns true :wink:

Unless i am mistaken i need to abandon this style of code. its not the way its punctuated.

Also I have tried && instead of ll .

first input SHOULd only include digits within the range of 0-180

Second 181 --> 360 minus 180

Third 361 --> 540 minus 360

Fourth 541 --> 720.

all four servo's obviously cannot work on the same range of numerals.
This was how I went about solving that.
Only worked with 2 servo's (not 100% sure why not more. i FEEL its just to much for if/else. I NEED switch case do i not?).

Less then 360 and greater then 180. Why would that ALWaYs return true?

OHHHH also im aware now that it will need timing. millis(). Perhaps more important then the lack of switch case?

jgreene33:
Less then 360 and greater then 180. Why would that ALWaYs return true?

Because it's an OR ( || ), not an AND ( && )

got ya! I did try both.

Is this what you wanted?

void loop()
{
  int input = Serial.parseInt();
  if (input <= 180)
  {
    MyServo1.write(input);
    delay(40);
  }
  else if (input > 180  && input <= 360)
  {
    MyServo2.write(input - 180);
    delay(40);
  }
  else if (input > 360 && input <= 540)
  {
    MyServo3.write(input - 360);
    delay(40);
  }
  else if (input > 540 && input <= 720)
  {
    MyServo4.write(input - 540);
    delay(40);
  }
 
}

That is kind of what i already have. One person was kind enough to give an example of the switch case Thank you!! very much. Now I am working on how to time this sketch. Not going well so far.