# If..Else limits

I'm putting together a device that will drive two stepper motors and 2 DC motors with variable settings set prior to the "cycle start" button being pressed. Within the loop, I started writing the code in what I think would work but I'm not 100% sure of what is allowed within an "if else" tree. If I structured it like the following, would that work? Would it cause a bunch of problems (assuming the commands therein were correct)?

If (cycle start) If (stepper motor 1 direction switch) run in this direction run at speed run for this duration Else run in opposite direction run at speed run for this duration If (stepper motor 2) Else If (DC motor 1) Else If (DC motor 2) Else

That's the gist of the intent of the program. I'm sure there is a million ways to drive the intent but I'm still a bit confused with what is actually functional using If..Else

In essence there is no limit to what you can include in the code block associated with an if or an else (and don't forget else/if) as long as it is valid code and there is memory available. You can nest if/else within the code block for an if or else but the logic can become difficult to follow

My advice would be to put each { and } on its own line and to use them even if only one statement is execute as the result of a test. Auto Format the code (Ctrl +T) often and be liberal with the use of comments about why you are testing values. It should be obvious what you are testing from the code if you use sensible descriptive names for variables.

There are times where you would be tempted to use a series of "if(A==1) {} else if (A==2) {} else if (A==3) {}" to compare an integer variable (A) against a series integer constants (1, 2, 3...). The shortcut for that is "switch(A) {case 1: break; case 2: break; case 3: break;}" Can your series of decisions be broken down into a state variable and a series of actions based on the current state?

johnwasser: There are times where you would be tempted to use a series of "if(A==1) {} else if (A==2) {} else if (A==3) {}" to compare an integer variable (A) against a series integer constants (1, 2, 3...). The shortcut for that is "switch(A) {case 1: break; case 2: break; case 3: break;}" Can your series of decisions be broken down into a state variable and a series of actions based on the current state?

I don't think so. The program would drive all of the motors at a given speed for an overall cycle time. Each motor speed and direction is determined by an analog potentiometer map and an SPDT switch respectively prior to the cycle start. For X seconds (main "IF" statement), each motor channel moves in a direction dictated by its internal "IF" statement.

I have seen the "case" and "break" set up in the LCD tutorial in the Arduino starter book but I wasn't sure if that would work best for this application.

switch/case is excellent if you can reduce the case to a single integer as it is very easy to see which portion of code will be executed for each case value. This is particularly so if you use an enum to give the cases meaningful names. It is particularly good for implementing non blocking timing using millis()

Don't forget that you can nest switch/case or use simple if/else within a case and that you can have 2 or more switch/case constructs working independently if required. Be sure not to block execution of the code using delay(), while or large for loops

Getting an "else without a previous if" error when attempting this (segment)

`````` ////DC MOTOR 1 JOG////
{
digitalWrite(in1, HIGH);     // Run DC 1 in "Forward" direction
digitalWrite(in2, LOW);
analogWrite(enA, DCspeedOutput1);   //Read by Pot 4 map
}
else {
digitalWrite(in1, LOW);      // Run DC 2 in "Reverse" direction
digitalWrite(in2, HIGH);
analogWrite(enA, DCspeedOutput1);   //Read speed by Pot 4 map

else {
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
}
}
}
``````

I'm not sure what to do with an if statement within an if statement...or if I can even do that. I thought I had the curly brackets assigned write but apparently not.

Getting an "else without a previous if" error when attempting this

The reason is this semicolon

``````if (digitalRead(27) == HIGH);
``````

Got it. Thanks for the help!

Here is the full code. Finally compiles properly. Going to start wiring it up soon to test.

I’m not sure if the structure is the best but I’m open to suggestions.