"Hold" or "pause" command/statement. Many statements. Mixing "if", "&&" and "||"

(Had to break this up into two posts due to message length restrictions.)

superolav:
How to program a "hold" or "pause" command/statement.. or some workaround. Any ideas? If i "delay" i also delay the whole loop!

I think the concept you're looking for here is the "state machine". Given your pin reading example, you would do it like this:

void loop() {
    int sensorValue;
    int lastPinState = LOW;
    
    // Loop infinitely here:
    while (1) {
        sensorValue = analogRead(A0);
        
        if (sensorValue > 360) {
            if (lastPinState == LOW) {
                digitalWrite(4, HIGH);
                lastPinState = HIGH;
                
            // lastPinState == HIGH
            } else {
                digitalWrite(4, LOW);
                lastPinState = LOW;
            }
        }  // if (sensorValue > 360)
    } // while(1)
}

This is the logic that you described, but it's probably not going to do what you actually want because it's very likely you'll get a value over 360 with each loop iteration, meaning pin 4 is going to be twiddled back and forth at a super high speed. That might be what you're looking for, but probably not. Generally, you would want to only toggle the pin once the value at A0 goes over 360, then falls below 360, then goes back over 360 again. Or maybe you want it to toggle as long as A0 is over 360, but not more than once every 100ms. Or maybe you want the pin to go HIGH when A0 is over 360, and LOW again when A0 is over 630 (or whatever).

Let's assume you only want it to toggle each time it goes above 360, falls below, then goes back above 360 again:

void loop() {
    int sensorValue;
    int lastPinState = LOW;
    int lastSensorValue = 0;
    
    // Loop infinitely here:
    while (1) {
        sensorValue = analogRead(A0);
        
        if ( (sensorValue > 360) && (lastSensorValue < 360) ) {
            if (lastPinState == LOW) {
                digitalWrite(4, HIGH);
                lastPinState = HIGH;
            } else {
                digitalWrite(4, LOW);
                lastPinState = LOW;
            }
        }  // if (sensorValue > 360)
        
        lastSensorValue = sensorValue;
    } // while(1)
}

One more thing you might want to account for is called "hysteresis" -- which means, you might want there to be a "dead zone" to prevent the pin from toggling when the analog value hovers around, say, 358 to 362 for example. Technically, it fell below 360 when it's 359, so the code will toggle the pin. But the difference is negligible and you probably don't want it to change so close to the threshold value, since any amount of noise will set it off. And there WILL be noise.

You only need to change one line to implement this kind of buffer area:

        if ( (sensorValue > 360) && (lastSensorValue < 350) ) {

That will provide a 10-value cushion where the analog value has to drop below 350 before it's considered "below the threshold" again. You could tweak this value to be as large or small as you want.

There are a lot of ways this can be further optimized (not using ints when a single byte would work, or using the PINB register to toggle the pin instead of tracking its state, etc...) but this will get the job done.

Hope this short novel helps. :wink: