Arduino Function Interference?

Discussion in 'Programmer's Corner' started by CoachKalk, Jul 28, 2012.

  1. CoachKalk

    Thread Starter Member

    Sep 20, 2011
    I have been adding features to the laser maze with great results. My sketch now has Sudden Death and Speed Round choices. The Sudden Death portion ends the course when any laser is blocked while the Speed Round lets the kids finish, but penalty time is added.

    I quickly realized the kids were avoiding the more challenging areas of the course, so I added an illuminated PB in a strategic location to increases the difficulty. The PB blinks at the start of the run and it MUST be pressed or the Stop button will not stop their time.

    But, I have noticed an issue and I am not sure what/why it is happening. I have inserted the function I wrote to control the illuminated PB (target). The entire sketch is quite large, so I have only included the portions specific to the target1 function.

    What I Am Seeing:
    When the target1Control function is placed within the Sudden Death or Speed Round functions, the sketch works, but the actual blink rate does not correspond to the sketch blink rate. I am not sure how, but something seems to be interfering with the sketch.

    If I place the target1Control function at the beginning of the loop function, the actual blink rate matches the sketch blink rate. When I press/select the type of game I want (Sudden Death or Speed Round), the blinking is still correct.

    But, when I press the difficulty level (easy, medium or ninja), the blink rate slows considerably. If I then press Start and run the course, the sketch works as planned - I must press the target PB or the Stop button will not work - even though the PB blinking is whacked.

    Note: The lasers turn on when the difficulty level is selected.

    I think I may have a power supply problem/interference, but I wanted to post the function for your review as well.

    My entire project has 4 different power supplies.
    Lasers: Dedicated 5V Supply
    Illuminated PB's and Siren: 12V Supply
    Timer Circuits and Arduino: 9V Supply
    Large 5" Displays: 20V Supply

    Even though the lasers are the only components using the 5V supply, when the laser power on there is some sort of influence over the arduino/sketch/12V supply? Any ideas?

    Code ( (Unknown Language)):
    2.  const int target1 = 18;  //PB in course that must be pushed
    3.  const int lightTarget1 = 17;   //Controls bulb for illuminated Target1
    4.  int target1Mode = 0;
    5.  int target1State;
    6.  int target1V1;
    7.  int target1V2;
    9.  int lightTarget1State = LOW;           //target1State used to blink bulb
    10.  long previousTarget1Millis = 0;   //will store last time target1 was updated
    11.  long target1Interval = 250;       // interval at which to blink target1 bulb - .5 sec total .25 on .25 off
    13. void setup(){
    14.  pinMode(target1, INPUT_PULLUP);
    15.  pinMode(lightTarget1, OUTPUT);
    16.  target1State = digitalRead(target1);
    17. }
    19. void loop(){
    21.    target1V1 = digitalRead(target1);
    22.    delay(2);
    23.    target1V2 = digitalRead(target1);
    24.      if (target1V1 != target1State || target1V2 != target1State){
    25.         target1Mode = 1;
    26.        }
    27.  }
    29. void target1Control(){
    30.      if (target1Mode == 0){
    31.      //start the .5 sec toggle after game starts
    32.      unsigned long currentTarget1Millis = millis();
    33.            if(currentTarget1Millis - previousTarget1Millis > target1Interval) {
    34.            // save the last time you changed the bulb power
    35.               previousTarget1Millis = currentTarget1Millis;  
    36.              // if the bulb is off turn it on and vice-versa:
    37.                 if (lightTarget1State == LOW)
    38.                     lightTarget1State = HIGH;
    39.                     else
    40.                       lightTarget1State = LOW;
    42.              // set the bulb with the target1State of the variable:
    43.            digitalWrite(lightTarget1, lightTarget1State);
    44.            }
    45.          }
    46.       if (target1Mode == 1){
    47.          digitalWrite(lightTarget1, HIGH);
    48.       }    
    49.  }
  2. BillB3857

    Senior Member

    Feb 28, 2009
    You say you are using a dedicated supply for the lasers. Is it truly a dedicated (separate) supply or are you using a multiple output supply with a dedicated 5V ouput and other outputs supplying everyting else? My thought is that the on many multiple output supplies only one of the outputs is actually in control of the regulation. The others simply go along for the ride and may be close, but vary with load on the controlling supply.