Simple Digital I/O Program and Optimization

Hey all.. long time reader, first time poster.

I searched the forums briefly, but didn't find anything relevant to my problem. I have a very simple program that turns an LED on when a HIGH signal is found on a separate input signal pin. Here is my code:

int ledPin = 13;                              /* LED on pin 13         */
int sigPin = 8;                                /* input on pin 8         */

void setup()
{
  pinMode(ledPin, OUTPUT);            /* digital pin as output */
  pinMode(sigPin, INPUT);               /* digital input pin       */
}

void loop()
{
  digitalWrite(ledPin, LOW);            /* turn off LED           */
  
  /* If we receive a (HIGH) signal, turn on the LED */
  while ( digitalRead(sigPin) == HIGH )
    digitalWrite(ledPin, HIGH);
    
}

This compiles and runs just fine, but the loop function seems to take a ridiculously long period of time. Is there something I'm not understanding, or that I should be doing differently? What am I missing?

I originally had a simple if statement in place of the while loop, but tested with the while just in case.

Any and all suggestions and criticisms are welcome :D

thanks!

The loop() function never exits. Its not supposed to. Unlike programming for a desktop, microcontroller applications are typically run as endless loops.

The only reason to use the while instead of the if statement is if you want the led to function as an indicator of how low the signal on pin 8 is active.

Actually, the loop() function is called over and over again forever, but it does actually exit and restart each time.

What do you mean when you say that this takes a long time to run? What behavior do you see? What do you have connected to the board?

Actually, the loop() function is called over and over again forever, but it does actually exit and restart each time.

yes.. this I understand.

This actually brings up a separate question: - would there be an increase in performance if the loop() function [as well as how the Arduino runs the loop() function] was replaced with a while(1) loop? You would [in effect] be inserting your general code [ that used to be in loop() ] into this while loop. I know this might not make sense to do..in the larger picture.. but would a while(1) loop reinitialize faster than exiting and recalling loop()?

What do you mean when you say that this takes a long time to run? What behavior do you see? What do you have connected to the board?

I apologize for not being more explicit..

This code is just a simple test that will be modified, expanded on, and used in a larger project.. so it is simply for testing purposes. My question is really separate from the example..

The code effectively checks for a HIGH signal on pin 8, turning on the LED connected to pin 13 if one is found. What I noticed is that the loop() function, despite having very little code, seems to take a while to execute/reinitialize. Let me break it down:

the loop() function: - turns off the LED - checks pin 8, if HIGH, LED --> on - restart..

So when I trigger pin 8 with a momentary 5v signal.. the LED lights up, and will stay on until turned off at the beginning of the next run through loop(). What I experienced is that the LED will stay on for 5 - 7 SECONDS before turning off..sometimes upwards of 8 - 12 seconds. Implying that the loop() function is either stuck somewhere, or is off doing something I don't understand.. This doesn't seem right. Note that this happens when using either a while loop or an if statement.

Thanks for the quick replies!!

When I was having some issues with speed I ran the following sketch

void loop()
{
   digitalWrite(pin3, HIGH);
   digitalWrite(pin3, LOW);
}

I checked the output on pin3 with and oscilliscope and determined that the digitalWrite function (in v7) took about 9us. Since the waveform was close to a 50% duty cycle it didn't appear that the overhead for the loop function was impacting performance much. If it was, in the above example, the low portion of the wave would have been longer then the high. On a tek 2225? analog scope I couldn't see much difference when the timebase was set to 2us (i think).

A similar test might help identify/quantify the issue/problem for you. Also if you are using the new v8 then it is possible that something about the new version might have altered the above behavior.

I think I remember someone mentioning that the digitalWrite function was going to be sped up. If this was the case it is certainly possible that the reduction in time for digitalWrite would mean that the loop overhead could have a greater impact percentage wise when dealing with such simple loops.

How do you have the switch connected? Are you using a pull-up/pull-down resistor? If not the pin may be floating.

With the code you posted, the LED will stay on as long as the input to pin 8 is high. What's connected to pin 8? If nothing, then it's reasonable for the LED to stay on for 5-7 or 8-12 seconds or longer as the input is "floating" and can return arbitrary values (e.g. HIGH for many seconds at a time).

How do you have the switch connected? Are you using a pull-up/pull-down resistor? If not the pin may be floating.

I just shorted the pin from the 5V power pin on the board itself during the tests.. so maybe that is my problem?

Most likely, here is a tutorial explaining the proper way to connect a switch.

Most likely, here is a tutorial explaining the proper way to connect a switch.

While the larger example won't be using a switch like this.. it does illustrate the problem at hand. Toying around with a resistor, and connecting the pin to either side shows this quite well.

Thanks!