user20615: So I'm running a program that involves both Arduino and a Raspberry PI and they are communicating between the Serial port. As you can see below when my an emergency stop door, involving a magnetic switch, is opened then the PI sends a message to the Arduino. When the door is closed the PI sends a different value telling the Arduino to continue with the program. The Arduino is receiving and reading the messages correctly as I have ran a few tests with the serial monitor. 
When the door is open the PI is printing '1' which shuts down the Arduino correctly. My problem is that when I close the doors nothing happens when in theory it should break the loop and continue to run the main Arduino code. The raspberry PI is correctly printing '2' and the Arduino is correctly reading this value as it prints "Logoff" in the serial monitor.
If anyone can help or lead me to correctly breaking this loop and returning to the main code that would be great. 
Thanks.
Below is the emergency part of the code that continues to run. If the full code is needed I will gladly edit it into this post. Also if the PI code is needed I will post that too, I don't believe is necessary because there are no problems on that side.
void Estop() {
    if (Serial.available()) {
        int d = Serial.read() - '0';

        while (d == 1) {
            digitalWrite(green, HIGH);
            digitalWrite(red, LOW);
            digitalWrite(blue, HIGH);
            digitalWrite(estopout, HIGH);
            digitalWrite(piston, LOW);
            digitalWrite(belt, HIGH);
            Serial.read() - '0';
            Serial.print(d);
            delay(2000);
            break;

            if (d == 0) {
                Serial.print("Logoff");
                delay(2000);
            } else {
                break;
            }
        }
    }
}

`

user5090: You never reassigned the variable d anywhere within the loop. The while breaks after a 2 seconds delay after printing what is the value of d. That means that your if (d == 0) [...] never gets hit. I presume that you expect to read 'logoff' on your code?
void Estop() {
    if (Serial.available()) {
        int d = Serial.read() - '0';

        while (d == 1) {
            digitalWrite(green, HIGH);
            digitalWrite(red, LOW);
            digitalWrite(blue, HIGH);
            digitalWrite(estopout, HIGH);
            digitalWrite(piston, LOW);
            digitalWrite(belt, HIGH);
            d = Serial.read() - '0';
            Serial.print(d);
            delay(2000);
            #break;

            if (d == 0) {
                Serial.print("Logoff");
                delay(2000);
            } else {
                break;
            }
        }
    }
}


20615: <p>So I'm running a program that involves both Arduino and a Raspberry PI and they are communicating between the Serial port. As you can see below when my an emergency stop door, involving a magnetic switch, is opened then the PI sends a message to the Arduino. When the door is closed the PI sends a different value telling the Arduino to continue with the program. The Arduino is receiving and reading the messages correctly as I have ran a few tests with the serial monitor. </p>

<p>When the door is open the PI is printing '1' which shuts down the Arduino correctly. My problem is that when I close the doors nothing happens when in theory it should break the loop and continue to run the main Arduino code. The raspberry PI is correctly printing '2' and the Arduino is correctly reading this value as it prints "Logoff" in the serial monitor.</p>

<p>If anyone can help or lead me to correctly breaking this loop and returning to the main code that would be great. </p>

<p>Thanks.</p>

<p>Below is the emergency part of the code that continues to run. If the full code is needed I will gladly edit it into this post. Also if the PI code is needed I will post that too, I don't believe is necessary because there are no problems on that side.</p>

<pre><code>void Estop() {
    if (Serial.available()) {
        int d = Serial.read() - '0';

        while (d == 1) {
            digitalWrite(green, HIGH);
            digitalWrite(red, LOW);
            digitalWrite(blue, HIGH);
            digitalWrite(estopout, HIGH);
            digitalWrite(piston, LOW);
            digitalWrite(belt, HIGH);
            Serial.read() - '0';
            Serial.print(d);
            delay(2000);
            break;

            if (d == 0) {
                Serial.print("Logoff");
                delay(2000);
            } else {
                break;
            }
        }
    }
}
</code></pre>

<p>`</p>
 5090: <p>You never reassigned the variable d anywhere within the loop. The <code>while</code> breaks after a 2 seconds delay after printing what is the value of d. That means that your <code>if (d == 0) [...]</code> never gets hit. I presume that you expect to read 'logoff' on your code?</p>

<pre><code>void Estop() {
    if (Serial.available()) {
        int d = Serial.read() - '0';

        while (d == 1) {
            digitalWrite(green, HIGH);
            digitalWrite(red, LOW);
            digitalWrite(blue, HIGH);
            digitalWrite(estopout, HIGH);
            digitalWrite(piston, LOW);
            digitalWrite(belt, HIGH);
            d = Serial.read() - '0';
            Serial.print(d);
            delay(2000);
            #break;

            if (d == 0) {
                Serial.print("Logoff");
                delay(2000);
            } else {
                break;
            }
        }
    }
}
</code></pre>
