TSB (Customer) asked a question.

Watchdog Timer and subroutines ie functions

My code is simple

 P1.configWD(4000, TOGGLE); and P1.startWD();   in setup

start loop

some stuff happens

Call subroutine

Return from sub

 P1.petWD();

end of loop

 

Problem is if I have a fail and the program hangs in the sub the timer never expires.

Is this normal?

any input is appreciated

Thanks

Tim


  • FACTS_AdamC (AutomationDirect)

    Yes, every time that loop wraps around, it will call that P1 command which pets the watchdog.

    Selected as Best
  • FACTS_AdamC (AutomationDirect)

    Hey Tim,

     

    Is the code you're describing something like below? If the watchdog isn't being pet, it should expire and reset the P1AM SAMD21. If you're using the TOGGLE setting, this will allow the code to automatically start executing again It should also be noted that any P1AM library call will "pet" the watchdog

    1. #include "P1AM.h"
    2.  
    3. void setup() {
    4. Serial.begin(115200);
    5. while (!Serial);
    6. Serial.println("Starting program");
    7. P1.init();
    8. P1.configWD(4000, TOGGLE);
    9. P1.startWD();
    10. }
    11.  
    12. uint8_t cnt = 0;
    13. void loop() {
    14.  
    15. hang_on_3();
    16. P1.petWD();
    17. cnt++;
    18.  
    19. }
    20.  
    21. void hang_on_3() {
    22.  
    23. if (cnt == 3){
    24. while(1); // Hang here forever
    25. }
    26. Serial.println("Waiting");
    27. delay(1000); // otherwise wait 1 second
    28.  
    29. }

    Thanks

    Adam

    Expand Post
  • TSB (Customer)

    yes and it fails to fail ... Just hangs in the sub function Your code as posted works perfectly .

    Combing my code for the misplaced ; or (

    Thanks

     

    Tim

  • TSB (Customer)

    Ok I am stumped

    posting code here any help is appreciated

    1. /*****************************************************************************************************************
    2. PRogram to control flo the rapper
    3. Written by Timothy Bullard
    4. No Warranties expressed or implied
    5. This is beerware . IF you like this code use it. IF you want to pay for it buy the author a beer
    6. IF you can't find the author have a beer
    7. 19-Nov-2021
    8. */
    9.  
    10. #include <P1AM.h>;
    11.  
    12. const int inputs=1;//inputs
    13. const int outputs=2;
    14. const int input1=1;
    15. const int input2=2;
    16. const int output1=1;
    17. const int output2=2;
    18. const int output3=3;
    19. unsigned long start,finished,elapsed;
    20.  
    21.  
    22.  
    23.  
    24. void setup(){ // the setup routine runs once:
    25.  
    26. Serial.begin(115200); //initialize serial communication at 115200 bits per second
    27. while (!P1.init()){
    28. ; //Wait for Modules to Sign on
    29. }
    30. P1.configWD(4000, TOGGLE); //Pass in the timer value and TOGGLE will reset the CPU after 4000 milliseconds has passed.
    31. while (P1.readDiscrete(1,3)==1){}; //forces machine to be in manual or program will not start
    32. P1.startWD(); //start Watch Dog function. Timer starts at 0 and waits the previously passed in value (4000 ms).
    33. //if program takes more then 4 seconds to run cpu will reset
    34. }
    35.  
    36. void loop(){ //the loop routine runs over and over again forever:
    37.  
    38. while (P1.readDiscrete(1,3)==0){ //Read the value of channel 3 in slot 1 if value is 1 execute auto
    39.  
    40. if (P1.readDiscrete(1,4)==1){cyclepress();} // cycle the press once
    41. };// end of manual
    42.  
    43. P1.writeDiscrete(HIGH,outputs,output1); //Turn slot 2 channel 1 on = Start Button pressed
    44. delay(100); //wait 0.1 second
    45. P1.writeDiscrete(LOW,outputs,output1); //Turn slot 2 channel 1 off = Start Button lifted
    46. delay(500); //wait 0.5 second
    47. P1.writeDiscrete(HIGH,outputs,output2); //Turn slot 2 channel 2 on = Stop at end of cycle Button pressed
    48. delay(100); //wait 0.01 second
    49. P1.writeDiscrete(LOW,outputs,output2); //Turn slot 2 channel 2 off = Stop at end of cycle Button lifted
    50. delay(2000); //wait 2 second
    51. // completes conveyer control add code here to check for conveyer moving
    52. cyclepress(); //cycle the press
    53. P1.petWD(); //resets watchdog timer
    54. }// End of void loop
    55.  
    56. void cyclepress(){
    57. P1.writeDiscrete(HIGH,outputs,output3); //Turn slot 2 channel 3 on close press
    58. while (P1.readDiscrete(1,1)==0){}; //Read the value of channel 2 in slot 1 press closed
    59. P1.writeDiscrete(LOW,outputs,output3); //Turn slot 2 channel 3 off Press open
    60. while (P1.readDiscrete(1,2)==0){}; //Read the value of channel 2 in slot 1 press open
    61. } //end of cycle press

     

    Expand Post
  • TSB (Customer)

    ok after re reading your answer . My statement while (P1.readDiscrete(1,1)==0){}; likely pets the watchdog hence no fail???? while in that while loop. Let me know if this is the case.

    Thanks

    Tim

    • FACTS_AdamC (AutomationDirect)

      Yes, every time that loop wraps around, it will call that P1 command which pets the watchdog.

      Selected as Best
      • TSB (Customer)

        well that is inconvenient

        I guess I will get on that rewrite

        Thanks

        Tim

      • FACTS_AdamC (AutomationDirect)

        You could check the time between entry and the current millis() and break if it's longer than expected. If you want a function to be called instead of just breaking the loop, you can move the statement checking the time difference into the loop and use an if statement.

        1. uint32_t start = millis()
        2. uint32_t timeout = 4000
        3. void loop(){ //the loop routine runs over and over again forever:
        4. while (P1.readDiscrete(1,3)==0 && (millis()-start>=timeout)){ //Read the value of channel 3 in slot 1 if value is 1 execute auto
        5. //code
        6. }
        7. }

         

        Expand Post
      • TSB (Customer)

        Thanks

        I created this sub

        1. void timeout(){
        2. finished=millis();
        3. if (finished-start>=4000){NVIC_SystemReset();}
        4. }

        Then I do this

        1. start=millis();//sets the start time for timeout
        2. while (P1.readDiscrete(1,1)==0){
        3. timeout();
        4. }; //Read the value of channel 2 in slot 1 press closed

        seems to work

        Thanks for all the help

        Tim

        Expand Post