How to abort an "if" loop process when conditions change before completing the task

I’m having a hard time finding help on this because I’m not even sure how to search for it, but here’s what I want to solve.

I have a photon that will be running in my truck to control a raspberry pi’s power on and off. Essentially when the ignition power is present, the Pi will power up, but when the ignition goes off, I want to signal the Pi to do a graceful shutdown. I’m doing this with a relay board I have connected to the Photon.

Here’s the scenario: ignition on isn’t a problem, all i’m doing is having the photon sense the ignition with a voltage divider on a digital pin set to detect high or low voltage. I then trigger another pin set as output to trigger a relay that sends 5v power to the Pi. When the ignition goes off I sense this with the photon (that has contant power) and then issue a signal on the other pin that is connected to one of the Pi’s GPIO pins where I have a python script to do a graceful shutdown.

My plan was to be able to have a certain delay of say 20 minutes to leave the Pi powered up in case I’m just quickly stopping somewhere so I don’t have to keep rebooting it all the time. The problem is the if…else loop I have has no way to interrupt the shutdown sequence when ignition power is restored. Should I be using a while loop or a do while or a switch setup? Which one of these control structures will interrupt the current case when conditions change?

At the end of your 20 minute spin-down, have the Photon Check the voltage divider again.
Then decide to Shutdown the Pi, or continue normal duty from the nested IF.

I tried putting in a nested if statement which tested the condition and then do a break, but it would not allow me to use a break because it said it needed to be inside a loop??? Didn’t make any sense to me.

Here’s the simple loop I have, it doesn’t like that break being in there, saying it needs to be inside a loop or switch.

void loop() {
  // Check for ignition on
  //digitalRead(ignition, HIGH);
  valpi = digitalRead(ignition);
  //digitalWrite(pipower, valpi);
  if (valpi == LOW)
    digitalWrite(pishutdown, HIGH);
    if (digitalRead(ignition) == HIGH) break;
    digitalWrite(pipower, LOW);
    digitalWrite(pishutdown, LOW);
    digitalWrite(pipower, HIGH);


I’m thinking I need to use a switch or a do while loop instead of an if else structure because the if else structure isn’t really a loop. What would be the best to use in this case? I have never used the switch structure, but maybe that’s the best way to go here.

I’m not sure if this will help you out, but I came across this product just yesterday (I ordered that company’s “Current Ranger” product).

1 Like

Hey, nice. I have a couple moteinos from low power labs. I have managed to get it to work with a switch case structure.