[SOLVED] Particle photon crash when timer activates

lets see if i can get this right with out making anyone angry…
im using particle photon with relay shield. i was able to get mostly what i want out of code and the vPINS working together, my code might not be the cleanest or effective but its working so far. what im trying to do now is start a timer when ever one of my three buttons on the app is pressed activating a relay and than deactivate the relay and reset the switch on the app after the timer has reached 0. ive done as much looking to find my answer with out having to bother asking and im not looking for someone to code for me just trying to learn, but with the code that i have, it compiles fine. i only have a start timer on one button just for testing. if i don’t press that button there’s no issues, but once i press the button it clearly starts the timer but once the timer reaches 0 my photon starts flashing the red light on the board and is basically disconnected from the app. i have all the actions commented out of the timer right now trying to track down the issue.

thanks in advance for someone pointing out where i went wrong.

#define BLYNK_PRINT Serial  // Set serial output for debug prints
//#define BLYNK_DEBUG       // Uncomment this to see detailed prints

#include <blynk.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "AUTH CODE";

void relay_off()
{
    static int count = 0;
   
        Serial.println(count++);
        //digitalWrite(3, LOW);
        //Blynk.virtualWrite(V1, 0);
       
        //digitalWrite(4, LOW);
        //Blynk.virtualWrite(V2, 0);
       
        //digitalWrite(5, LOW);
        //Blynk.virtualWrite(V3, 0);
       
        //digitalWrite(6, LOW);
        //Blynk.virtualWrite(V4, 0);
}
       

Timer timer(5000, relay_off);

void setup()
{
    Serial.begin(9600);
    delay(5000); // Allow board to settle

    Blynk.begin(auth);
    pinMode (3,OUTPUT);  //define pin 3 as output for relay
    pinMode (4,OUTPUT);  //define pin 4 as output for relay 
    pinMode (5,OUTPUT);  //define pin 5 as output for relay
    pinMode (6,OUTPUT);  //define pin 6 as output for relay
    
}




BLYNK_WRITE(1)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V3 to a variable
 
  if (param.asInt()) {
      timer.start();
        
       digitalWrite(3, HIGH);       //turns relay 1 on
       
       digitalWrite(4, LOW);        //turns relay 2 off
       Blynk.virtualWrite(V2, 0);   //updates Blynk Button to off
       
       digitalWrite(5, LOW);        //turns relay 3 off
       Blynk.virtualWrite(V3, 0);   //updates Blynk Button to off
       
       digitalWrite(6, LOW);        //turns relay 4 off
       Blynk.virtualWrite(V4, 0);   //updates Blynk Button to off
       
   
    }
}

BLYNK_WRITE(2)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V3 to a variable
  
  if (param.asInt()) {
        //HIGH
       digitalWrite(4, HIGH);           //turns relay 2 on
       
       digitalWrite(3, LOW);            //turns relay 1 off
       Blynk.virtualWrite(V1, 0);      //updates Blynk Button to off
       
       digitalWrite(5, LOW);            //turns relay 3 off
       Blynk.virtualWrite(V3, 0);      //updates Blynk Button to off
       
       digitalWrite(6, LOW);            //turns relay 4 off
       Blynk.virtualWrite(V4, 0);      //updates Blynk Button to off
       
    }
}

BLYNK_WRITE(3)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V3 to a variable
 
  if (param.asInt()) {
        //HIGH
       digitalWrite(5, HIGH);           //turns relay 3 on
       
       digitalWrite(3, LOW);            //turns relay 1 off
       Blynk.virtualWrite(V1, 0);       //updates Blynk Button to off
       
       digitalWrite(4, LOW);            //turns relay 2 off
       Blynk.virtualWrite(V2, 0);       //updates Blynk Button to off
       
       digitalWrite(6, LOW);            //turns relay 4 off
       Blynk.virtualWrite(V4, 0);       //updates Blynk Button to off
       
   }
}

BLYNK_WRITE(4)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V3 to a variable
 
  if (param.asInt()) {
        // using 4th button to turn all relays off, when attempting to use else with any blynk write to turn the relay that was just turned on to off again i crash photon.
       digitalWrite(6, LOW);            //relay 4 off        
       
       digitalWrite(3, LOW);            //relay 1 off
       Blynk.virtualWrite(V1, 0);       //updates Blynk Button to off
       
       digitalWrite(4, LOW);            //relay 2 off
       Blynk.virtualWrite(V2, 0);       //updates Blynk Button to off
       
       digitalWrite(5, LOW);            //relay 3 off
       Blynk.virtualWrite(V3, 0);       //updates Blynk Button to off
       
       
   }
}


void loop()
{
    Blynk.run();
}

Maybe BlynkTimer would be ok as an alternative solution?

but, at least, you’ve got this printed?
Serial.println(count++);

or it crashes before printing this line?

Crashes right after 5 seconds after touching the button. I tried to get it to only reset the button (un clicked) but it won’t do that.

i have an update to the problem, im able to get the timer to work without crashing the board. ive narrowed it down to the board only crashes when i try to reset the button. i can activate all relays (one at a time) and it starts the timer and than turns that relay off, but as soon as i put the Blynk.virtualWrite(V1, 0); command in the board crashes. its like i cant send a blynk command within the photon timer, which i don’t understand. i added a 1 sec delay after the digital write inside the timer and i can watch the relay turn off after its commanded time and than the board will crash after the delay. ive tried to get the blynk timer to work but with no success. anyone have any ideas or ways around this to reset the button status after the timer ends?

@agemeny I took your concept and adapted it for my hardware (UNO on USB), then played around until I got it to work.

Each time I hit a button, it cancels any running timer, assuming it hasn’t already gone off, thus each button press starts a fresh 5 second timer before turning off all pins. I am using BlynkTimer.

Here is my code:

#define BLYNK_NO_BUILTIN   // Disable built-in analog & digital pin operations
#include <BlynkSimpleStream.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "daec5b95292c4f0387da61cf4c3e058b";  // Local Server
int CountDown; // Timer ID Name

BlynkTimer timer;

void setup()
{
  pinMode (5, OUTPUT);  // define pin 5 as output for relay
  pinMode (6, OUTPUT);  // define pin 6 as output for relay
  pinMode (7, OUTPUT);  // define pin 7 as output for relay
  pinMode (8, OUTPUT);  // define pin 8 as output for relay

  Serial.begin(9600);
  Blynk.begin(Serial, auth);
}



void relay_off()  // Resets all pins and buttons to OFF
{
  digitalWrite(5, LOW);
  Blynk.virtualWrite(V1, 0);

  digitalWrite(6, LOW);
  Blynk.virtualWrite(V2, 0);

  digitalWrite(7, LOW);
  Blynk.virtualWrite(V3, 0);

  digitalWrite(8, LOW);
  Blynk.virtualWrite(V4, 0);
}



BLYNK_WRITE(V1)
{
  int pinValue = param.asInt();  // Assigning incoming value from pin V1 to a variable

  if (param.asInt() == 1) {

    timer.disable(CountDown);  // Disable any currently running timer with CountDown ID
    CountDown = timer.setTimeout(5000L, relay_off);  // Start CountDown timer

    digitalWrite(5, HIGH);   // turns relay 1 on

    digitalWrite(6, LOW);  // turns relay 2 off
    Blynk.virtualWrite(V2, 0);  // updates Blynk Button to off

    digitalWrite(7, LOW);  // turns relay 3 off
    Blynk.virtualWrite(V3, 0);  // updates Blynk Button to off

    digitalWrite(8, LOW);  // turns relay 4 off
    Blynk.virtualWrite(V4, 0);  // updates Blynk Button to off
  }
}

BLYNK_WRITE(V2)
{
  int pinValue = param.asInt();  // Assigning incoming value from pin V2 to a variable

  if (param.asInt() == 1) {

    timer.disable(CountDown);  // Disable any currently running timer with CountDown ID
    CountDown = timer.setTimeout(5000L, relay_off);  // Start CountDown timer

    digitalWrite(5, LOW);  // turns relay 1 off
    Blynk.virtualWrite(V1, 0);  // updates Blynk Button to off

    digitalWrite(6, HIGH);  // turns relay 2 ON

    digitalWrite(7, LOW);  // turns relay 3 off
    Blynk.virtualWrite(V3, 0);  // updates Blynk Button to off

    digitalWrite(8, LOW);  // turns relay 4 off
    Blynk.virtualWrite(V4, 0);  // updates Blynk Button to off
  }
}

BLYNK_WRITE(V3)
{
  int pinValue = param.asInt();  // Assigning incoming value from pin V3 to a variable

  if (param.asInt() == 1) {

    timer.disable(CountDown);  // Disable any currently running timer with CountDown ID
    CountDown = timer.setTimeout(5000L, relay_off);  // Start CountDown timer

    digitalWrite(5, LOW);  // turns relay 1 off
    Blynk.virtualWrite(V1, 0);  // updates Blynk Button to off

    digitalWrite(6, LOW);  // turns relay 2 off
    Blynk.virtualWrite(V2, 0);  // updates Blynk Button to off

    digitalWrite(7, HIGH);  // turns relay 3 ON

    digitalWrite(8, LOW);   // turns relay 4 off
    Blynk.virtualWrite(V4, 0);  // updates Blynk Button to off
  }
}

BLYNK_WRITE(V4)
{
  int pinValue = param.asInt();  // Assigning incoming value from pin V4 to a variable

  if (param.asInt() == 1) {

    timer.disable(CountDown);  // Disable any currently running timer with CountDown ID
    CountDown = timer.setTimeout(5000L, relay_off);  // Start CountDown timer

    digitalWrite(5, LOW);  // turns relay 1 off
    Blynk.virtualWrite(V1, 0);  // updates Blynk Button to off

    digitalWrite(6, LOW);  // turns relay 2 off
    Blynk.virtualWrite(V2, 0);  // updates Blynk Button to off

    digitalWrite(7, LOW);  //turns relay 3 off
    Blynk.virtualWrite(V3, 0);  // updates Blynk Button to off

    digitalWrite(8, HIGH);  // turns relay 4 ON
  }
}


void loop()
{
  Blynk.run();
  timer.run();
}

well @Gunner you’ve done it again. i was able to understand how to properly use the blynk timer with your code. it works fine on the photon now, any idea why the way i was attempting to do it with the other timer wouldn’t work?
im sure helping newbies all the time time with “simple” coding can get annoying but i appreciate your time and i really have learned a lot.

i guess this could be labeled as [solved] or removed but im not sure how to or if a higher user needs to do that.

1 Like

Glad I could help… it was a good coding lesson for me as well… always learning something new with timers.

Unfortunately I have no idea how Photon timers work, but I did have to setup the timer cancellation and of course add timer.run(); in the void loop(), but not sure if lack of those had any effect on your issue. I also eliminated the static int counter and print statements… not sure what it’s purpose was for, other than troubleshooting

@Gunner and @agemeny my understanding is / was that the Photon can’t use the SimpleTimer, now renamed as BlynkTimer. I thought I saw reference to making BlynkTimer work with a Photon but I wasn’t aware the modifications had been done.

Yeagh so my comment 3d ago just went unnoticed :wink:

I saw your comment and tried to get the blynk timer loaded but I couldn’t get everything ordered right or something haha. I’m still green :joy:

Is this now confirmed as working with the Photon?

It is. it was announced with one of the releases.

1 Like

I must have missed that. However the particle compiler when I verify my code since I changed over to the blynk time does say check code but doesn’t show anything in the raw but if I run the verify right after it’ll go through clear. Not sure if it was my code or because of blynk timer? Have you ever run into this?

It might be a webide glitch. They happen sometimes all over the place.