Startup code runs more than once

I am developing a control system with numerouos buttons displayed on the monitor. Everything is working fine, however, I discovered that the startup code runs whenever I trigger a control. There is an empty draw sub - no code in it. If I remove that sub startup does run only once. The problem is that every time startup runs it resets everything to the initial configuration. It was creating some problems that I had a hard time tracking down. Is there some way to insure that startup runs once and only once?

This shouldn’t happen under normal operation. We would need to see your code to say why it is happening.

This code is just the eye candy to debug the interlocking logic. It’s still undergoing some revisions to get the logic right, but it demonstrates what I saw… With all the sub calls in setup and the draw statement present, everything runs. If you comment out the draw statement it works as I expected, it runs once and then nothing happens, mouse clicks do nothing. It really isn’t a problem as i can put all the startup code in an if statement that will only run once for variable initialization. It’s just that it doesn’t seem to agree with what I expected from the documentation…Once I start writing the logic code, probably in excess of 10,000 lines, debugging will be difficult enough without unexpected variable changes. My system is setup so the graphics are on my secondary monitor and code is on the primary.

(Attachment Experiment.pde is missing)

(Attachment Subs.pde is missing)

Here’s part of the code - I had to chop off some stuff to get it to fit in the character limitations

                      
// Lever Difinitions
final int roL01 = 0;
final int roL02 = 1;
final int roL03 = 2;
final int roL04 = 3;
final int roL05 = 4;
final int roL06 = 5;
final int roL07 = 6;
final int roL08 = 7;
final int roL09 = 8;
final int roL10 = 9;
final int roL11 = 10;
final int roL12 = 11;
final int roL13 = 12;
final int roL14 = 13;
final int roL15 = 14;
.  .  .

final int s208a = 124;
final int s309a = 125;
final int d314a = 126;
final int d311a = 127;
final int s314a = 128;
final int s314b = 129;
final int s315a = 130;
final int s218a = 131;

//board definitions
final int input_1   = 0; //ronceverte
final int input_2   = 1;
final int input_3   = 2;
final int input_4   = 3;
final int output_1  = 4;
final int output_2  = 5;
final int output_3  = 6;
final int output_4  = 7;
final int output_5  = 8;
final int output_6  = 9;
final int output_7  = 10;
final int output_8  = 11;
final int output_9  = 12;
final int output_10 = 13;

// Pin Values
final int p00 = 1;
.  .  .
final int p15 = 32768;

// signal aspects, base 0
//                                                rygryg
final int aspClear    = 36; // clear               100100
final int aspAppMed   = 17; // approach medium     010001
final int aspMedClr   = 33; // medium clear        100001
final int aspApproach = 20; // approach            010100
final int aspAdvApp   = 18; // advanced approach   010010
final int aspRestrict = 34; // restricting         100010
final int aspStop     = 36; // stop                100100
// dwarf aspects, base 0
final int dwfClear    =  1; // clear               001
final int dwfApproach =  6; // approach            110
final int dwfRestrict =  2; // restricting         010
final int dwfStop     =  4; // stop                100

// INPUT Boards

final int roSignal =  0; //roIN_01
final int roGetAsp =  0;
final int roUnLock =  1; //roIN_02
final int roSwitch =  2; //roIN_03
final int roSndAsp =  2;
final int roBlkOcc =  3; //roIN_04

final int ccSignal =  4; //ccIN_01
final int ccSwitch =  4;
final int ccBlkOcc =  4;
final int ccUnLock =  5; //ccIN_02
final int ccGetAsp =  5;

final int wsSignal =  6; //wsIN_01
final int wsUnLock =  7; //wsIN_02
final int wsSwitch =  8; //wsIN_03
final int wsBlkOcc =  9; //wsIN_04
final int wsGetAsp = 10; //wsIN_05

final int acIN_01 =  11; //acIN_01
final int acIN_02 =  12; //acIN_02

// OUTPUT Boards

final int roOUT_01 = 13; //signal locks
final int roOUT_02 = 14; // switch locks
final int roOUT_03 = 15;
final int roOUT_04 = 16;
final int roOUT_05 = 17;
final int roOUT_06 = 18;
final int roOUT_07 = 19;
final int roOUT_08 = 20;
final int roOUT_09 = 21;
final int roOUT_10 = 22;

final int ccOUT_01 = 23;
final int ccOUT_02 = 24;
final int ccOUT_03 = 25;
final int ccOUT_04 = 26; //locks / send aspects

final int wsOUT_01 = 27; //signal locks
final int wsOUT_02 = 28; //switch locks
final int wsOUT_03 = 29;
final int wsOUT_04 = 30;
final int wsOUT_05 = 31;
final int wsOUT_06 = 32;
final int wsOUT_07 = 33;
final int wsOUT_08 = 34;
final int wsOUT_09 = 35;
final int wsOUT_10 = 36;
final int wsOUT_11 = 37; //send aspects

final int acOUT_01 = 38;
final int acOUT_02 = 39;
final int acOUT_03 = 40; // locks / send aspects
//                            Pin 0    value
//                             x    y
final int ioBoard [][] = { { 265,  30, 00},  //  0 ro in
                           { 265,  65},  //  1
                           { 265, 100},  //  2
                           { 265, 135},  //  3
                           {1635, 530},  //  4  cc in
                           {1635, 656},  //  5
                           {1635, 600},  //  6
                           {1027, 515},  //  7  ws in
                           {1027, 550},  //  8
                           {1027, 585},  //  9
                           {1027, 620},  // 10
                           {1027, 655},  // 11
                           { 265, 635},  // 12  ac in
                           { 265, 670},  // 13
                           { 265, 170},  // 14  ro out
                           { 265, 205},  // 15
                           { 265, 240},  // 16
                           { 265, 275},  // 17
                           { 265, 310},  // 18
                           { 265, 345},  // 19
                           { 265, 380},  // 20
                           { 265, 415},  // 21
                           { 265, 450},  // 22
                           { 265, 485},  // 23
                           {1635, 600},  // 24  cc out
                           {1635, 635},  // 25
                           {1635, 670},  // 26
                           {1635, 705},  // 27
                           { 665, 440},  // 28  out_1  ws
                           { 665, 475},  // 29  out_2
                           { 665, 510},  // 30  out_3
                           { 665, 545},  // 31  out_4
                           { 665, 580},  // 32  out_5
                           { 665, 615},  // 33  out_6
                           { 665, 650},  // 34  out_7
                           { 665, 685},  // 35  out_8
                           { 665, 720},  // 36  out_9
                           { 665, 755},  // 37  out_10
                           { 665, 790},  // 38  out_11
                           { 265, 705},  // 39  ac  out
                           { 265, 740},  // 40
                           { 265, 775}   // 41
                            };  
final int frog1 = 9;
final int frog2 = 14;
     
final  int type = 0;
  final  int signalLever = 0;   //lever
  final  int switchLever = 1;     
  final  int blockButton = 2;
final  int position = 1;       //lever
  final  int normal = 1;
  final  int reverse = 0;
final  int xPos = 2;            //button location
final  int yPos = 3;
final  int lock = 4;            //lever
  final  int unlocked = 0;
         int locked = 1;        // actually any value > 0
 final  int text = 5;
 final int status = 1;          //block
 final int clear = 1;
 final int routed = 2;
 final int occupied = 3;
 
 //<<<<<<<<<<    Colors     >>>>>>>>>>
final  int hideColor = #656565;
final  int red = #FF0000;
final  int green = #00FF00;
final  int yellow = #FFFF00;
final  int lightGreen = #66FF66;
final  int white = #FFFFFF;
final  int black = #000000;
final  int ltGray = #989898;
       int strokeColor;
final int signal = 0;
final int turnout = 1;
final int block = 2;
final int dwarf = 3;


//                         X,    Y
//                                   direction
//                                       diverging                
//                                             block
//                          0    1   2   3     4     5    6   7   8

                      
//                     Type: signal = 0, turnout = 1, block = 2, dwarf = 3
//                     |  Lever status; normal = 1, reversed = 0
//                     |  |    Block status: clear = 1, routed = 2, occupied = 3
//                     |  |              Lever Lock: unlocked = 0, locked => 0
//                     |  |              |   Timer Running
//                     |  |              |   |    Block         Signal/  Direction
//                     |  |    Button    |   |    Routed        Turnout  |   Diverging   Signal   Direction      
//                     |  |   Location   |   |    |   Occupied  location |   |           location  |   Diverging INPUT DATA*    OUTPUT DATA
//                     |  |     x     y  |   |    |   |    Text  x    y   |   |     Block x    y   |   | Block  Board     Pin  Board     Pin      Lever Device
//                     0  1     2     3  4   5    6   7    8     9   10  11  12    13    14   15  16  17    18  19        20   21        22
int     lever[][] = { {0, 1,  508,  290, 0,  0,   0,  0,   1,  650, 181, -1,  0,    0,    0,   0,  0,  0,    0, roSignal, p00, roOUT_03, 10},   //ro01  s300  0  Ronceverte       
                      {1, 1,  548,  290, 0,  0,   0,  0,   2,  681, 140,  1, -1, b201,    0,   0,  0,  0,    0, roSwitch, p00,       00, 00},   //ro02  1
                      {1, 1,  588,  290, 0,  0,   0,  0,   3,  681, 181,  1, -1, b301,  735, 140, -1,  1, b202, roSwitch, p01,       00, 00},   //ro03  2
                      {0, 1,  628,  290, 0,  0,   0,  0,   4,  752, 140,  1,  0,    0,    0,   0,  0,  0,    0, roSignal, p01, roOUT_03,  4},   //ro04  s201  3
                      {0, 1,  668,  290, 0,  0,   0,  0,   5, 1236, 140, -1,  0,    0,    0,   0,  0,  0,    0, roSignal, p02, roOUT_04, 10},   //ro05  s203  4
                      {0, 1,  708,  290, 0,  0,   0,  0,   6, 1236, 181, -1,  0,    0,    0,   0,  0,  0,    0, roSignal, p03, roOUT_04,  4},   //ro06  s303  5

                      .  .  .

                      {3, 1,    0,    0, 0,  0,   0,  0,   0, 1567, 263, -1,  0,    0,    0,   0,  0,  0,    0,       00,  00, roOUT_09,  1},   //s305a 122;
                      {3, 1,    0,    0, 0,  0,   0,  0,   0, 1621, 222, -1,  0,    0,    0,   0,  0,  0,    0,       00,  00, roOUT_10,  1},   //s305b 123;
                      {3, 1,    0,    0, 0,  0,   0,  0,   0, 1248, 352, -1,  0,    0,    0,   0,  0,  0,    0,       00,  00, ccOUT_02,  1},   //s208a 124;
                      {3, 1,    0,    0, 0,  0,   0,  0,   0, 1507, 474,  1,  0,    0,    0,   0,  0,  0,    0,       00,  00, ccOUT_03,  1},   //s309a 125;
                      {3, 1,    0,    0, 0,  0,   0,  0,   0, 1408, 818,  1,  0,    0,    0,   0,  0,  0,    0,       00,  00, wsOUT_07,  1},   //d314a 126;
                      {3, 1,    0,    0, 0,  0,   0,  0,   0, 1362, 818, -1,  0,    0,    0,   0,  0,  0,    0,       00,  00, wsOUT_04,  1},   //d311a 127;
                      {3, 1,    0,    0, 0,  0,   0,  0,   0, 1110, 818,  1,  0,    0,    0,   0,  0,  0,    0,       00,  00, wsOUT_09,  1},   //s314a 128;
                      {3, 1,    0,    0, 0,  0,   0,  0,   0, 1110, 777,  1,  0,    0,    0,   0,  0,  0,    0,       00,  00, wsOUT_06,  1},   //s314b 129;
                      {3, 1,    0,    0, 0,  0,   0,  0,   0,  751, 818,  1,  0,    0,    0,   0,  0,  0,    0,       00,  00, wsOUT_08,  1},   //s315a 130;
                      {3, 1,    0,    0, 0,  0,   0,  0,   0,  391, 940, -1,  0,    0,    0,   0,  0,  0,    0,       00,  00, acOUT_02,  1}    //s218a 131;
                    };                      
                      
// x1, y1, x2, y2 etc for line drawing
// 0 terminates the data entry
// switch points are NOT drawn

void setup ()
{  fullScreen();
   background(hideColor);
   rectMode(CENTER);
   strokeWeight(4);   
   int i;

   i = 0;
   for (i = 62; i <132; i++)
   {  drawBlock (i);
   }
   
   for (i = 0; i < 62; i++)
   {  drawLever (i);
   }
   
   for (i = 0; i < 105; i++)
   {  drawButton(i);
   }
   /*drawSwitch (ad01);
   drawSwitch (ad02);
   drawSwitch (ad03);
   drawSwitch (ad04);
   drawSwitch (M1);
   drawSwitch (M2);
   */
   for (i = 0; i < 132; i++)
   {  drawSignal(i);
   }
}

void draw()
{ 
}

//*******************************  subroutines   ********************************


void drawSignal(int dsi)
{  strokeWeight(3);
   stroke(black);
   {  switch (lever[dsi][0])
      {  case signal:
            fill (black);
            line(lever[dsi][9], lever[dsi][10], lever[dsi][9], lever[dsi][10] - 28 * lever[dsi][11]);
            circle (lever[dsi][9] + 8 * lever[dsi][11], lever[dsi][10] - 28 * lever[dsi][11], 12); //upper head
            circle (lever[dsi][9] + 8 * lever[dsi][11], lever[dsi][10] - 14 * lever[dsi][11], 12); //lower head
         break;
         case dwarf:
            fill (black);
            line (lever[dsi][9], lever[dsi][10], lever[dsi][9], lever[dsi][10] - 14 * lever[dsi][11]);
            circle (lever[dsi][9] + 6 * lever[dsi][11], lever[dsi][10] - 14 * lever[dsi][11], 7); //upper lamp
            circle (lever[dsi][9] + 6 * lever[dsi][11], lever[dsi][10] - 7 * lever[dsi][11], 7); //lower lamp for Approach only        
         break;
         default:
         break;
      }
   }   
}

void mousePressed()
{  int mi; 
   int mx;                        // mouseX value
   int my;                        // mouseY value
   mx = mouseX;
   my = mouseY;
   for (mi = 0; mi < 105; mi++)    
   {  if ((mx < lever[mi][2] + 15) && (mx > lever[mi][2] - 15) && (my < lever[mi][3] + 10) && (my > lever[mi][3] - 10))
      {  switch (lever[mi][0])
         {  case signal:
               if (lever[mi][1] == 1) 
               {   lever[mi][1] = 0;
                   drawLever(mi);           
                   drawBlock(lever[mi][13]);
                   if (lever[mi][18] > 0)
                   {   drawBlock(lever[mi][18]);
                   }
               }  
               else
               {   lever[mi][1] = 1;
                   drawLever(mi);           
                   drawBlock(lever[mi][13]);
                   if (lever[mi][18] > 0)
                   {   drawBlock(lever[mi][18]);
                   }
               }
            break;
            case turnout:
               if (lever[mi][1] == 1) 
               {   lever[mi][1] = 0;
                   drawLever(mi);           
                   drawBlock(lever[mi][13]);
                   if (lever[mi][18] > 0)
                   {   drawBlock(lever[mi][18]);
                   }
               }  
               else
               {   lever[mi][1] = 1;
                   drawLever(mi);           
                   drawBlock(lever[mi][13]);
                   if (lever[mi][18] > 0)
                   {   drawBlock(lever[mi][18]);
                   }
               }
            break;
            case block:
               if (lever[mi][7] == 1) 
               {   lever[mi][7] = 0;
                   drawBlock(mi);
               }  
               else
               {   lever[mi][7] = 1;
                   drawBlock(mi);
               }
            break;
            default:
            break;   
         }
      }  
   }  
}   

void drawSwitch (int leverPosition, int x, int y, int direct, int diverg, int trackColor)
{  if (leverPosition == normal) 
   {  stroke (ltGray);
      line (x, y, x + 23 * direct, y + 17 * diverg);
      stroke (trackColor);
      line (x, y, x + 23 * direct, y);
   }
   else  //lever reversed
   {  stroke (ltGray);
      line (x, y, x + 23 * direct, y); 
      stroke (trackColor); 
      line (x, y, x + 23 * direct, y + 17 * diverg);
   }  
}

void drawLever(int li)
{ if (lever[li][0] == 0)
  {  stroke(red);
  }
  else
  {  stroke(black);
  }
  if (lever[li][1] == 1)
  {  line (lever[li][2], lever[li][3], lever[li][2], lever[li][3] - 40);
     strokeWeight(6);
     stroke(hideColor);
     line (lever[li][2], lever[li][3], lever[li][2], lever[li][3] + 40);
     strokeWeight(4);
  }
  else
  {  line (lever[li][2], lever[li][3], lever[li][2], lever[li][3] + 40);
     strokeWeight(6);
     stroke(hideColor);
     line (lever[li][2], lever[li][3], lever[li][2], lever[li][3] - 40);
     strokeWeight(4);
  }
  drawButton (li);
}

void drawButton(int bi)
{
  rectMode(CENTER);
  noStroke();
  switch (lever[bi][0])
  {  case signalLever:
        if (lever[bi][4] == 0)  //lever not locked
        {  fill(green);
        }
        else
        {  fill(red);
        }
        rect(lever[bi][2], lever[bi][3], 31, 21);
        textAlign(CENTER);
        if (lever[bi][4] == 0)
        {  fill(black);
        }
        else
        {  fill(white);
        }
        text(str(lever[bi][8]),lever[bi][2], lever[bi][3] + 5);
     break;
     case switchLever:
        if (lever[bi][4] == 0)
        {  fill(green);
        }
        else
        {  fill(red);
        }
        rect(lever[bi][2], lever[bi][3], 31, 21);
        textAlign(CENTER);
        if (lever[bi][4] == 0)
        {  fill(black);
        }
        else
        {  fill(white);
        }
        text(str(lever[bi][8]),lever[bi][2], lever[bi][3] + 5);
     break;
     case blockButton:
        fill(yellow);
        rect(lever[bi][2], lever[bi][3], 31, 21);
        textAlign(CENTER);
        fill(black);
        text(str(lever[bi][8]),lever[bi][2], lever[bi][3] + 5);
     break;
  }
  
}

void drawBlock (int bli)
{  if(lever[bli][7]== 1)
   {   strokeColor = red;
   }
   else
   {  if (lever[bli][6] == 1)
      {  strokeColor = green;
      }
      else
      {  strokeColor = black;
      }
   }
     
   strokeWeight(4); 
   stroke(strokeColor);
   switch (bli)
   {  case b200:                    //Ronceverte
         line (505, 140,  639, 140);
         break;
      case b300:
         line(505, 181,  639, 181);
         break;
      case b201:
         line (650, 140,  681, 140);
         drawSwitch (lever[roL02][1], 681, 140,  1, -1, strokeColor);
         break;
      case b301:
         line (650, 181,  681, 181);
         drawSwitch (lever[roL03][1], 681, 181,  1, -1, strokeColor);
         break;
      case b102:
         line (712, 117,  735, 100);
         line (735, 100,  834, 100);
         line (857, 100, 1024, 100);
         drawSwitch (lever[roL07][1], 1024, 100,  1,  1, strokeColor);
         drawSwitch (lever[M2][1], 1056, 818,  1, -1, strokeColor);
         break;
      case b202:
         line (735, 140, 1014, 140);
         drawSwitch (lever[roL03][1], 735, 140, -1,  1, strokeColor);
         break;
     case b302:
        line (712, 181, 1014, 181);
        break;
     .  .  .  

      case b217:
        line (375, 899,  403, 899);
        drawSwitch (lever[acL03][1],  375, 899, -1, -1, strokeColor);
        drawSwitch (lever[acL04][1],  403, 899,  1, -1, strokeColor);
        break;
      case b317:
        line (311, 859,  321, 859);
        line (344, 859,  434, 859);
        line (457, 859,  470, 859);
        break;
      case b218:
        line (240, 899,  344, 899);
        break;
      case b318:
        line (240, 859,  321, 859);
        drawSwitch (lever[acL03][1],  321, 859,  1,  1, strokeColor);
        break;
      case dk1:                      //START UP ONLY
        line (865,  76,  888,  59);
        line (888,  59,  942,  59);
        break;
      case dk2:
        line (1354, 204, 1377, 222);
        line (1401, 222, 1629, 222);
        line (1676, 222, 1812, 222);
       break;
     case dk3:
       line (1401, 239, 1432, 263);
       line (1455, 263, 1575, 263);
       line (1598, 263, 1621, 245);
       break;
     case dk4:
       line (1432, 303, 1598, 303);
       line (1455, 280, 1486, 303); 
       line (1543, 303, 1574, 280);
       break;
     case dk5:
       line (1248, 352, 1271, 370);
       line (1148, 352, 1248, 352);
       break;
     case dk6:
       line (1487, 459, 1507, 474);
       line (1507, 474, 1570, 474);
       break;
     case dk7:
       line (728, 835,  755, 818);
       line (755, 818,  819, 818);
       break;
     case dk8:
       line (345, 940,  403, 940);
       line (403, 940,  426, 923);
       break;
     case dk9:
       line (1087, 795,  1110, 777);
       line (1110, 777,  1175, 777);
       break;
     default:
       break;
  }
}

It seems you’re using the wrong tag. This is processing code, not p5.js