Mick (Customer) asked a question.

having trouble with code recognizing switch closures and going to subroutines. serial monitor recognizes state changes with switches... thanks
  1. rev 1.00 08/20/2025 7:03 AM -initial release
  2.  
  3. */
  4. #include <P1AM.h>
  5. void setup() { // the setup routine runs once:
  6. Serial.begin(9600); //initialize serial communication at 115200 bits per second
  7. while (!P1.init()){
  8. ; //wait for Modules to sign on
  9. }
  10. }
  11. /*
  12. I/O assignments and Slot assignments
  13. * *********************************************************************************************
  14.  
  15. * *********************************************************************************************
  16. labels for outputs SLOT-1 P1-15TD2 OUTPUT MODULE 15 points of OUTPUT
  17. */
  18. //channelLabel N/A = {1, 1}; // SLOT-1 - CH-1: N/A
  19. channelLabel doorUPrelay = {1, 1}; // SLOT-1 - CH-1: doorUPrelay control relay 1=doorUP / 0=NOT-doorUP
  20. channelLabel doorDOWNrelay = {1, 2}; // SLOT-1 - CH-2: doorDOWNrelay control relay 1=doorDOWN / 0=NOT-doorDOWN
  21. channelLabel GREENlight = {1, 12}; // SLOT-1 - CH-12: green light control 1=on / 0= off
  22. channelLabel AMBERlight = {1, 13}; // SLOT-1 - CH-13: amber light control 1=on / 0= off
  23. channelLabel REDlight = {1, 14}; // SLOT-1 - CH-14: red light control 1=on / 0= off
  24. //channelLabel N/A = {1, 15}; // SLOT-1 - CH-15: N/A
  25. /* ********************************************************************************************
  26. labels for inputs SLOT-2 P2-16ND3 INPUT MODULE 16 points of INPUT
  27. */
  28. channelLabel SAFEdoorSWITCH = {2, 1}; // SLOT-2 - CH-1: safety door interlock switch 1=safe / 0= not-safe
  29. channelLabel STOPcommandSWITCH = {2, 2}; // SLOT-2 - CH-3: door motion stop switch 1=not-door-stop motion/ 0=stop door motion
  30. channelLabel UPcommandSWITCH = {2, 3}; // SLOT-2 - CH-2: UPcommandSWITCH door UP mode switch 1=NOT-door-UP / 0= door-UP
  31. channelLabel DOWNcommandSWITCH = {2, 4}; // SLOT-2 - CH-4: DOWNcommandSWITCH door DOWN mode switch 1=NOT-door-DOWN / 0= door-DOWN
  32.  
  33. /* ********************************************************************************************
  34. // * *********************************************************************************************
  35. /*
  36. /*
  37. labels for analog input SLOT-2 P1-04AD-2 ANALOG INPUT MODULE 4-CHANNEL of 0-10 VDC INPUT
  38. */
  39. // channelLabel N/A = {2, 1}; // SLOT-1 - CH-1: N/A
  40. // ********************************************************************************************
  41. // system variables / flags
  42. //
  43. //int UPcommandSWITCH = 1; // check if UPcommandSWITCH flag = 1 NOT-UPcommandSWITCH / 0=UPcommandSWITCH
  44. //int DOWNcommandSWITCH = 1; // check if DOWNcommandSWITCH flag = 1 NOT-DOWNcommandSWITCH / 0=DOWNcommandSWITCH
  45. //int STOPdoorSWITCH = 1; // check if STOPdoorSWITCH flag = 1 NOT-STOPdoorSWITCH / 0=STOPdoorSWITCH
  46. int UPDOORmode = 1; // check if UPDOORmode flag = 1 NOT- door up / 0= door up
  47. int DOWNDOORmode = 1; // check if DOWNDOORmode flag = 1 NOT- door down / 0= door down
  48. int STOPDOORmode = 1; // check if STOPDOORmode flag = 1 NOT- door stop / 0= door stop
  49. int FAULTfatal = 0; // set FAULTfatal flag = 0 not in FAULT / 1= FAULT "int" global variable
  50. int AMBERLIGHT = 0;
  51. int REDLIGHT = 0;
  52. int GREENLIGHT = 0;
  53. unsigned long x = 0; // the time the delay started
  54.  
  55. // ********************************************************************************
  56. /* *********************************************************************************************
  57. * scan UP/DOWN control switch zone
  58. */
  59. void loop() // the loop routine to scan UP/DOWN control switch zone
  60. { scanCONTROLbuttons();
  61. }
  62. void scanCONTROLbuttons()
  63. {
  64. Serial.println(UPDOORmode); // check status if x-axis has homed
  65. P1.writeDiscrete(HIGH, GREENlight); //green light control= ON
  66. delay(250); //wait .25 second
  67. //P1.writeDiscrete(HIGH,doorUPrelay); // doorUPrelay control relay 1=doorUP / 0=NOT-doorUP
  68. //P1.writeDiscrete(HIGH,doorDOWNrelay); // doorUPrelay control relay 1=doorDOWN / 0=NOT-doorDOWN
  69. //
  70. // check UP door Switch for status: = 1 NOT-UPcommandSWITCH / 0=UPcommandSWITCH
  71. UPDOORmode = P1.readDiscrete(UPcommandSWITCH); // check if UPcommandSWITCH = 1 NOT-UPcommandSWITCH / 0=UPcommandSWITCH
  72. if (UPDOORmode == 1) // check if UPDOORmode flag = 1 NOT- door up / 0= door up
  73. {
  74. // P1.writeDiscrete(HIGH,doorUPrelay); // doorUPrelay control relay 1=doorUP / 0=NOT-doorUP
  75. MOVEdoorUP; // gosub MOVEdoorUP subroutine
  76. }
  77. //
  78. // check DOWN door Switch for status: = 1 NOT-DOWNcommandSWITCH / 0=DOWNcommandSWITCH
  79. DOWNDOORmode = P1.readDiscrete(DOWNcommandSWITCH); // check if DOWNcommandSWITCH = 1 NOT-DOWNcommandSWITCH / 0=DOWNcommandSWITCH
  80. if (DOWNDOORmode == 1) // check if DOWNDOORmode flag = 1 NOT- door down / 0= door down
  81. {
  82. MOVEdoorDOWN; // gosub MOVEdoorDOWN subroutine
  83. }
  84. //
  85. // check STOP door Switch for status: = 1 NOT-STOPcommandSWITCH / 0=STOPcommandSWITCH
  86. STOPDOORmode = P1.readDiscrete(STOPcommandSWITCH); // check if STOPcommandSWITCH = 1 NOT-STOPommandSWITCH / 0=STOPcommandSWITCH
  87. if (STOPDOORmode == 1) // check if STOPDOORmode flag = 1 NOT- door stop / 0= door stop
  88. {
  89. STOPdoor; // gosub STOPdoor subroutine
  90. }
  91.  
  92. P1.writeDiscrete(LOW, GREENlight); //green ight control= OFF
  93. delay(250); //wait .5 second
  94. }
  95. // **********************************************************************************
  96. // **********************************************************************************
  97. // move door up - start door movement UP
  98. //
  99. void MOVEdoorUP() // use void because variables declared globaly
  100. {
  101. P1.writeDiscrete(HIGH, AMBERlight); //amber light control 1= on
  102. P1.writeDiscrete(LOW,doorDOWNrelay); // doorDOWNrelay control relay 1=doorDOWN / 0=NOT-doorDOWN
  103. delay(250); //wait .25 second
  104. P1.writeDiscrete(HIGH,doorUPrelay); // doorUPrelay control relay 1=doorUP / 0=NOT-doorUP
  105. }
  106. // **********************************************************************************
  107. // **********************************************************************************
  108. // move door down - start door movement DOWN
  109. //
  110. void MOVEdoorDOWN() // use void because variables declared globaly
  111. {
  112. P1.writeDiscrete(HIGH, AMBERlight); //green light control 1= on
  113. P1.writeDiscrete(LOW,doorUPrelay); // doorUPrelay control relay 1=doorUP / 0=NOT-doorUP
  114. delay(250); //wait .25 second
  115. P1.writeDiscrete(HIGH,doorDOWNrelay); // doorUPrelay control relay 1=doorDOWN / 0=NOT-doorDOWN
  116. }
  117. // **********************************************************************************
  118. // **********************************************************************************
  119. // stop door - stop door movement
  120. //
  121. void STOPdoor() // use void because variables declared globaly
  122. {
  123. P1.writeDiscrete(LOW,doorUPrelay); // doorUPrelay control relay 1=doorUP / 0=NOT-doorUP
  124. P1.writeDiscrete(LOW,doorDOWNrelay); // doorDOWNrelay control relay 1=doorDOWN / 0=NOT-doorDOWN
  125. P1.writeDiscrete(LOW, GREENlight); //green light control 0= oFF
  126. }
  127. // **********************************************************************************
  128. // **********************************************************************************
  129.  
  130.  
  131. // **********************************************************************************

 


  • Tinker (Customer)

    From: https://docs.arduino.cc/learn/programming/functions/ :

    "As you can see, even if a function does not have parameters and no returns is expected "(" and ")" brackets plus ";" must be given."

     

    Is the compiler giving any errors? I don't know off hand, what the compiler "thinks' of:

    1. {
    2. STOPdoor; // gosub STOPdoor subroutine
    3. }

    Note that in loop() you do call "scanCONTROLbuttons();" with the brackets

    Expand Post
  • FACTS_MikeSc (AutomationDirect)

    Yes without the () it's not a function call but an expression that refers to the function itself (its address). So it basically does nothing but it will compile.

     

    Just add the () to the function calls and you should be good.