# Trouble Combining Code

**URL:** <https://forum.arduino.cc/t/trouble-combining-code/956837>\
**Category:** Programming\
**Created:** [February 8, 2022, 7:08pm UTC](https://forum.arduino.cc/t/trouble-combining-code/956837 "2022-02-08T19:08:12Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![dstoddart](https://avatars.discourse-cdn.com/v4/letter/d/977dab/32.png) [@dstoddart](https://forum.arduino.cc/u/dstoddart)\
**Post date:** [February 8, 2022, 7:08pm UTC](https://forum.arduino.cc/t/trouble-combining-code/956837/1 "2022-02-08T19:08:12Z")

</div>

Hello,

I have built a machine that has a linear belt driven by a stepper motor, it has limit switches at both ends, and two buttons that drive an object on the belt towards or away from each of the limit switches. If written the following code to essentially disable the home or run buttons depending on if the limit switches are HIGH or LOW. This code works exactly as I would expect it to.

```cpp
/*
    Revision Notes
  1.2 - First working demonstration code
  1.3 - Added linear actuator aka finger to hardware
  1.4 - Added code to control finger
*/

#include <Stepper.h>

const int homeButtonPin = 5;
const int runButtonPin = 6;
const int homeLimitSwitch = 2;
const int endLimitSwitch = 3;
const int ledPin = 13;
const int stepsPerRevolution = 600;
const int fingerUp = 1; //rev 1.3
const int fingerDown = 0; //rev 1.3

Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

int homeButtonState = 0;
int runButtonState = 0;
int homeLimitSwitchState = 0;
int endLimitSwitchState = 0;
int lastCheck = 1;

void setup() {

  pinMode(ledPin, OUTPUT);
  pinMode(homeButtonPin, INPUT);
  pinMode(runButtonPin, INPUT);
  pinMode(homeLimitSwitch, INPUT);
  pinMode(endLimitSwitch, INPUT);
  pinMode(fingerUp, OUTPUT); //rev 1.3
  pinMode(fingerDown, OUTPUT); //rev 1.3

}

void loop() {
  // read the state of the pushbutton value:
  homeButtonState = digitalRead(homeButtonPin);
  runButtonState = digitalRead(runButtonPin);
  homeLimitSwitchState = digitalRead(homeLimitSwitch);
  endLimitSwitchState = digitalRead(endLimitSwitch);

  int speedPot = analogRead(A5); // Added 11:00 1/2/2022
  int speedSetting = map(speedPot, 0 , 1023, 100, 600); //rev 1.3 - 600 was 1000
  myStepper.setSpeed(speedSetting); // Added 11:00 1/2/2022

  if (homeButtonState == HIGH && homeLimitSwitchState == LOW)
  {
    myStepper.step(-stepsPerRevolution / 100); //1.4 reinstated /100
    digitalWrite(ledPin, HIGH);
  }

  if (homeLimitSwitchState == HIGH)
  {
    myStepper.step(0);
    digitalWrite(ledPin, HIGH);
  }

  if (((homeLimitSwitchState == HIGH || homeLimitSwitchState == LOW) && endLimitSwitchState == HIGH) && runButtonState == HIGH)
  {
    //This is where the code should be added
    myStepper.step(stepsPerRevolution / 100); //1.4 reinstated /100
  }

  if (endLimitSwitchState == HIGH && runButtonState == HIGH);
  {
    myStepper.step(0);
  }

}

```

I am adding another feature to the code that is driven by the same runButton. This code moves a linear actuator (aka finger) for a set amount of time, holds that position while runButton is held else it retracts the linear actuator. This code also works exactly like I would expect it to.

```cpp
int fingerDown = 0;
int fingerUp = 1;
int runButtonPin = 6;
int runButtonState = 0;
int lastCheck = 1;

void setup()
{
  pinMode(fingerDown, OUTPUT);
  pinMode(fingerUp, OUTPUT);
}

void loop()
{
  runButtonState = digitalRead(runButtonPin);
  if (runButtonState == HIGH)
  {
    if (lastCheck == 1)
    {
      analogWrite(fingerUp, 0);
      analogWrite(fingerDown, 255);
      delay(600); //Time the motor runs forward
      analogWrite(fingerUp, 0);
      analogWrite(fingerDown, 0);
    }
    lastCheck = 0;
  }
  else
  {
    lastCheck = 1;
    analogWrite(fingerDown, 0);
    analogWrite(fingerUp, 255);
  }
}

```

My challenge is this, when I try to add the "nested" IF/ELSE of the second code to the first code I get wildly unexpected results. Any help on how to have the second code work with the first would be greatly appreciated. The first code has a comment for where I would like to add it.

Please let me know what additional details I could provide to be more helpful.

---

<div class="post-metadata">

**Author:** ![ToddL1962](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/toddl1962/32/225060_2.png) [@ToddL1962](https://forum.arduino.cc/u/ToddL1962)\
**Post date:** [February 8, 2022, 7:41pm UTC](https://forum.arduino.cc/t/trouble-combining-code/956837/2 "2022-02-08T19:41:58Z")

</div>

I think your code would greatly benefit in readability if you use a state machine. Here is a link:

[State Machine](https://forum.arduino.cc/index.php?topic=604505.0)

Apart from that if you add the code where you indicated you get this:

```cpp
  if (((homeLimitSwitchState == HIGH || homeLimitSwitchState == LOW) && endLimitSwitchState == HIGH) && runButtonState == HIGH)
  {
    if (runButtonState == HIGH)
    {
      if (lastCheck == 1)
      {
        analogWrite(fingerUp, 0);
        analogWrite(fingerDown, 255);
        delay(600); //Time the motor runs forward
        analogWrite(fingerUp, 0);
        analogWrite(fingerDown, 0);
      }
      lastCheck = 0;
    }
    else
    {
      lastCheck = 1;
      analogWrite(fingerDown, 0);
      analogWrite(fingerUp, 255);
    }
    myStepper.step(stepsPerRevolution / 100); //1.4 reinstated /100
  }

```

Here are a couple of observations:

1. You will only enter the first **if** block if **runButtonState == HIGH**. Therefore, you will never execute the **else** statement of the **if (runButtonState == HIGH)** block!
2. There is no reason to check **homeLimitSwitchState == HIGH || homeLimitSwitchState == LOW** because it can't be anything other than those 2 values.

---

<div class="post-metadata">

**Author:** ![wildbill](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wildbill/32/1126438_2.png) [@wildbill](https://forum.arduino.cc/u/wildbill)\
**Post date:** [February 8, 2022, 8:24pm UTC](https://forum.arduino.cc/t/trouble-combining-code/956837/3 "2022-02-08T20:24:00Z")

</div>

Why pins 0 and 1 for the finger? You're depriving yourself of a fundamental debugging tool - Serial.print.

Why too are you using analogWrite when the only values you're using are are 0 and 255?

---

<div class="post-metadata">

**Author:** ![dstoddart](https://avatars.discourse-cdn.com/v4/letter/d/977dab/32.png) [@dstoddart](https://forum.arduino.cc/u/dstoddart)\
**Post date:** [February 8, 2022, 9:17pm UTC](https://forum.arduino.cc/t/trouble-combining-code/956837/4 "2022-02-08T21:17:32Z")

</div>

Thank you Todd,

I was/am unfamiliar with StateMachine but I can see why you recommend it and agree that it would be cleaner. I will try to make a transition to that code strategy and post it here.

I am not sure that I understand why I shouldn't check the limit switch states, I want to make sure that the stepper cannot drive past those limit switches.

---

<div class="post-metadata">

**Author:** ![dstoddart](https://avatars.discourse-cdn.com/v4/letter/d/977dab/32.png) [@dstoddart](https://forum.arduino.cc/u/dstoddart)\
**Post date:** [February 8, 2022, 9:20pm UTC](https://forum.arduino.cc/t/trouble-combining-code/956837/5 "2022-02-08T21:20:23Z")

</div>

Hello WildBill,

With regards to 0 and 1, they were open so I used them. I was not aware that doing so would prevent me from using serial.print, thanks for the tip. I can readily move to pins 4 and 7 and will do so.

I don't really have any better explanation on my choice to use analogWrite... I tried it and it worked. Is there any advantage to digitalWrite that might not be obvious to me?

Thank you for taking the time to help me out.

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [February 8, 2022, 9:21pm UTC](https://forum.arduino.cc/t/trouble-combining-code/956837/6 "2022-02-08T21:21:05Z")

</div>

> [@ToddL1962](#):
>
> `if (((homeLimitSwitchState == HIGH || homeLimitSwitchState == LOW) `

@ToddL1962 points out the the condition is always true.

Because _homeLimitSwitchState_ is true OR false.

a7

---

<div class="post-metadata">

**Author:** ![ToddL1962](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/toddl1962/32/225060_2.png) [@ToddL1962](https://forum.arduino.cc/u/ToddL1962)\
**Post date:** [February 8, 2022, 9:22pm UTC](https://forum.arduino.cc/t/trouble-combining-code/956837/7 "2022-02-08T21:22:30Z")

</div>

> [@dstoddart](#):
>
> I am not sure that I understand why I shouldn't check the limit switch states, I want to make sure that the stepper cannot drive past those limit switches.

You can and should check the states. However, look at this expression:

**homeLimitSwitchState == HIGH || homeLimitSwitchState == LOW**

That expression will always be true! **homeLimitSwitchState** can only be **HIGH** or **LOW**.

---

<div class="post-metadata">

**Author:** ![dstoddart](https://avatars.discourse-cdn.com/v4/letter/d/977dab/32.png) [@dstoddart](https://forum.arduino.cc/u/dstoddart)\
**Post date:** [February 8, 2022, 9:36pm UTC](https://forum.arduino.cc/t/trouble-combining-code/956837/8 "2022-02-08T21:36:41Z")

</div>

I completely understand your point.

I wrote code that made sense to me as a human trying to work things out... "do this thing regardless of homeLimitSwitch being HIGH or LOW" but the Arduino couldn't care less about that always true condition.

---

<div class="post-metadata">

**Author:** ![wildbill](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wildbill/32/1126438_2.png) [@wildbill](https://forum.arduino.cc/u/wildbill)\
**Post date:** [February 8, 2022, 9:39pm UTC](https://forum.arduino.cc/t/trouble-combining-code/956837/9 "2022-02-08T21:39:21Z")

</div>

> [@dstoddart](#):
>
> Is there any advantage to digitalWrite that might not be obvious to me?

Not that I can think of - in fact, I think if you use zero it does a digitalWrite behind the scenes anyway. Could be the same for 255 too.

It just looks odd and makes the reader wonder what's going on.

---

<div class="post-metadata">

**Author:** ![dstoddart](https://avatars.discourse-cdn.com/v4/letter/d/977dab/32.png) [@dstoddart](https://forum.arduino.cc/u/dstoddart)\
**Post date:** [February 8, 2022, 9:42pm UTC](https://forum.arduino.cc/t/trouble-combining-code/956837/10 "2022-02-08T21:42:45Z")

</div>

Thank you for your advice

---

<div class="post-metadata">

**Author:** ![dstoddart](https://avatars.discourse-cdn.com/v4/letter/d/977dab/32.png) [@dstoddart](https://forum.arduino.cc/u/dstoddart)\
**Post date:** [February 8, 2022, 10:27pm UTC](https://forum.arduino.cc/t/trouble-combining-code/956837/11 "2022-02-08T22:27:58Z")

</div>

Do either of you see a way for me to move the "fingerDown" code into the existing code by moving the else to a different location meaning that the figure should always be up unless the specific section in which it goes down is active?

---

<div class="post-metadata">

**Author:** ![wildbill](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wildbill/32/1126438_2.png) [@wildbill](https://forum.arduino.cc/u/wildbill)\
**Post date:** [February 8, 2022, 10:49pm UTC](https://forum.arduino.cc/t/trouble-combining-code/956837/12 "2022-02-08T22:49:56Z")

</div>

I'm not convinced that your second code needs to be nested in your existing ifs.

Try putting it in as the very last code in loop.

---

<div class="post-metadata">

**Author:** ![dstoddart](https://avatars.discourse-cdn.com/v4/letter/d/977dab/32.png) [@dstoddart](https://forum.arduino.cc/u/dstoddart)\
**Post date:** [February 8, 2022, 10:56pm UTC](https://forum.arduino.cc/t/trouble-combining-code/956837/13 "2022-02-08T22:56:34Z")

</div>

Wildbill has achieved hero status for the day! Thank you for your suggestion, It worked perfectly.

And thanks to Todd for suggesting I look into StateMachine.

I will clean up things based on your mutual suggestions.

---

<div class="post-metadata">

**Author:** ![dstoddart](https://avatars.discourse-cdn.com/v4/letter/d/977dab/32.png) [@dstoddart](https://forum.arduino.cc/u/dstoddart)\
**Post date:** [February 8, 2022, 11:14pm UTC](https://forum.arduino.cc/t/trouble-combining-code/956837/14 "2022-02-08T23:14:56Z")

</div>

Here is the current stage of the code prior to investigating the State Machine strategy. This code works exactly as I expected and needed.

```cpp
/*
    Revision Notes
  1.2 - First working demonstration code
  1.3 - Added linear actuator aka finger to hardware
  1.4 - Added code to control finger
*/

#include <Stepper.h>

const int homeButtonPin = 5;
const int runButtonPin = 6;
const int homeLimitSwitch = 2;
const int endLimitSwitch = 3;
const int ledPin = 13;
const int stepsPerRevolution = 600;
const int fingerUp = 7; //rev 1.4 changed pin
const int fingerDown = 4; //rev 1.4 changed pin

Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

int homeButtonState = 0;
int runButtonState = 0;
int homeLimitSwitchState = 0;
int endLimitSwitchState = 0;
int lastCheck = 1;

void setup() {

  pinMode(ledPin, OUTPUT);
  pinMode(homeButtonPin, INPUT);
  pinMode(runButtonPin, INPUT);
  pinMode(homeLimitSwitch, INPUT);
  pinMode(endLimitSwitch, INPUT);
  pinMode(fingerUp, OUTPUT); //rev 1.3
  pinMode(fingerDown, OUTPUT); //rev 1.3

}

void loop() {
  // read the state of the pushbutton value:
  homeButtonState = digitalRead(homeButtonPin);
  runButtonState = digitalRead(runButtonPin);
  homeLimitSwitchState = digitalRead(homeLimitSwitch);
  endLimitSwitchState = digitalRead(endLimitSwitch);

  int speedPot = analogRead(A5); // Added 11:00 1/2/2022
  int speedSetting = map(speedPot, 0 , 1023, 100, 600); //rev 1.3 - 600 was 1000
  myStepper.setSpeed(speedSetting); // Added 11:00 1/2/2022

  if (homeButtonState == HIGH && homeLimitSwitchState == LOW)
  {
    myStepper.step(-stepsPerRevolution / 100); //1.4 reinstated /100
    digitalWrite(ledPin, HIGH);
  }

  if (homeLimitSwitchState == HIGH)
  {
    myStepper.step(0);
    digitalWrite(ledPin, HIGH);
  }

  if (((homeLimitSwitchState == HIGH || homeLimitSwitchState == LOW) && endLimitSwitchState == HIGH) && runButtonState == HIGH)
  {
    //This is where the code should be added
    myStepper.step(stepsPerRevolution / 100); //1.4 reinstated /100
  }

  if (endLimitSwitchState == HIGH && runButtonState == HIGH);
  {
    myStepper.step(0);
  }

  //rev 1.4 starts here

  runButtonState = digitalRead(runButtonPin);
  if (runButtonState == HIGH)
  {
    if (lastCheck == 1)
    {
      digitalWrite(fingerUp, LOW);
      digitalWrite(fingerDown, HIGH);
      delay(300); //Time the motor runs forward
      digitalWrite(fingerUp, LOW);
      digitalWrite(fingerDown, LOW);
      delay(300); //Added to pause a moment before moving
    }
    lastCheck = 0;
  }
  else
  {
    lastCheck = 1;
    digitalWrite(fingerDown, LOW);
    digitalWrite(fingerUp, HIGH);
  }

  //rev 1.4 ands here

}

```

---

<div class="post-metadata">

**Author:** ![wildbill](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wildbill/32/1126438_2.png) [@wildbill](https://forum.arduino.cc/u/wildbill)\
**Post date:** [February 8, 2022, 11:24pm UTC](https://forum.arduino.cc/t/trouble-combining-code/956837/15 "2022-02-08T23:24:32Z")

</div>

This is odd:

```cpp
  if (endLimitSwitchState == HIGH && runButtonState == HIGH);
  {
    myStepper.step(0);
  }

```

I would have expected the limit switch to be LOW, but since that code really doesn't do anything (Do zero steps) it isn't visible.

---

<div class="post-metadata">

**Author:** ![dstoddart](https://avatars.discourse-cdn.com/v4/letter/d/977dab/32.png) [@dstoddart](https://forum.arduino.cc/u/dstoddart)\
**Post date:** [February 8, 2022, 11:29pm UTC](https://forum.arduino.cc/t/trouble-combining-code/956837/16 "2022-02-08T23:29:35Z")

</div>

You know what... I thought the same exact thing. I think there may be a backwards wire in that circuit but it is working for now so I'll add that to future refinements and improvements.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [August 7, 2022, 11:30pm UTC](https://forum.arduino.cc/t/trouble-combining-code/956837/17 "2022-08-07T23:30:35Z")

</div>

Closed on August 7, 2022, 11:30pm UTC

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