Freeze in looping program, something wrong?

Hi all,

I just got a UNO and while familiarizing myself with it I noticed something strange;
8 LED with pot running this code

void setup() {
for(int i=2; i<10; i++) { pinMode(i, OUTPUT); }
}

void loop() {
for (int i=2; i<10; i++) { blink(i); }
for (int i=8; i>2; i--) { blink(i); }
}

void blink(int i) {
digitalWrite(i, HIGH);
delay(analogRead(A5));
digitalWrite(i, LOW);
}

Every 20-25 seconds or so the lightflow freezes (all LED go LOW), LED 13 on the board flashes for a second, then the flow starts from the beginning again.

Running on Win7 Home Premium, Arduino 0022.

Any ideas why this occurs?

Thanks,
markob

in

pinmode()

you need a comma, like:

pinmode(i, OUTPUT)

That could be causing something to hang.

Good spot, there is actually a comma there in my code, don't know why it didn't stick on my copy-paste :~
w/o a comma it's a syntax error and will not compile.

Any other ideas? Still curious about the interrupt in the program. Just like something would hang for a sec after 20-25 secs. Some memory that needs to be flushed? It's so basic code I'm really out of ideas...

Sounds like your board might be going through a reset cycle. How are you wiring your led's. Using series current limiting resistors with the leds?

Lefty

Yes... odd. your code looks fine otherwise. In the examples folder, there is a sketch very similar to yours (Open > 3.Analog > AnalogInput) Don't change anything and see if that one works.

One idea I do have:

There is something called an interrupt. I won't go into the details, but you can basically say that when an interrupt is trigger, function x is executed, no matter what the rest of the code is doing. Normally these interrupts are trigger exernally, with a button, etc. Very useful :slight_smile:

But there is also something called a timer interrupt. This does the same thing, but after a certain amount of time. Your uno could maybe have an accidental timer interrupt enabled. But as I'm typing this, I have another idea:

From wikipedia:

A watchdog timer (or computer operating properly (COP) timer) is a computer hardware or software timer that triggers a system reset or other corrective action if the main program, due to some fault condition, such as a hang, neglects to regularly service the watchdog (writing a "service pulse" to it, also referred to as "kicking the dog", “petting the dog”, "feeding the watchdog"[1] or "waking the watchdog"). The intention is to bring the system back from the unresponsive state into normal operation.

So if something is wrong with your uno (hang, bad power supply, etc.) The watchdog timer will be triggered, resetting your program. Try out a few other programs included in the examples folder, and see if the same thing happens.

Good Luck!

Lefty; Each LED wired as follow : DigitalPin -> LED -> R 420 Ohm -> GND.

Baum: interrupt was also crossing my mind, but where would that come from? Perhaps USB? I'll continue messing around with other testbuilds and see how it goes.

There are no known issues with continous looping I assume since the whole main() is a big loop in itself...

No. scratch the interrupts. Try other programs with infinite loops and see if they run fine. If they don't, the watchdog may be getting triggered.

markob:
Lefty; Each LED wired as follow : DigitalPin -> LED -> R 420 Ohm -> GND.

Good, that should not be causing a hardware reset due to overcurrent draw.

Baum: interrupt was also crossing my mind, but where would that come from? Perhaps USB? I'll continue messing around with other testbuilds and see how it goes.

The Arduino core code only enables timer0 interrupts to make millis() and delay() useable. No other interrupts are enabled, including watchdog timer, unless you enable them in your sketch or from some library code you use. This is barking up a rather skinny tree.

Why don't you change delay(analogRead(A5)); to delay(500); and see if your symptoms change.

There are no known issues with continous looping I assume since the whole main() is a big loop in itself...

The Arduino core code only enables timer0 interrupts to make millis() and delay() useable. No other interrupts are enabled, including watchdog timer, unless you enable them in your sketch or from some library code you use. This is barking up a rather skinny tree.

But he could have a defective Uno. And, assuming the worst, maybe he has a shorted PCB trace. or a dead component. Or something else. Why would analogRead hang?

Ok, so I tried another loop. Instead of a FOR loop I wrote a if/else statement in the loop (and got a chance to try my button as well);

#include <Button.h>

Button k = Button(8,PULLUP);

void setup() { pinMode(2, OUTPUT); }

void loop(){
if(k.isPressed()){
lightOn();
}else{
}
}
void lightOn() {
digitalWrite(2, LOW);
delay(500);
digitalWrite(2, HIGH);
}

Poor implementation and excuse my code, but this gives the same effect. After upload I press the button, LED on 2 goes HIGH, and 23 seconds after UNO start-up/sketch upload L13 flashes for a sec and LED on 2 goes LOW.

So something is reset..

Not that it matters, but can we se buttons.h?

Now try something. Run this program:

void setup() { 
pinMode(13, OUTPUT); 

digitalWrite(13, HIGH);

}

void loop() {}

See what that does.

Actually while staring at it 8) LED on 2 goes LOW, and then onboard L13 flashes 3-4 times rapidly and then a very faint flicker on L13 (all this L13 flashing well within a second).

Super verified by this advanced piece of code:

void setup() { pinMode(2, OUTPUT); }

void loop(){ digitalWrite(2, HIGH); }

Wow I type slow, but we thought about the same thing at least..

I think the arduino bootloader does something with LED13 while receiving a program. That should happen.

So your suggestion with LED 13 yields same result. Onboard LED is HIGH for ~23 seconds, flickers and goes LOW during less then a second, and then goes HIGH again.

I return to the watchdog. If your watchdog was accidentally enabled @ the factory, that could be it. Or it could be a hardware reset (maybe a short or something). Check your board for problems.

Any suggestion on what I should check?
Visual inspection shows no scratching of the PCB or other apparent sources of problems. Are there any sketches I can load to get into some debug mode or similar to see what's going on?

no sketches that I know of. Where did you get the Uno from? maybe you can return it and get a new one. Or, if your feeling adventurous, you can lookup avrdude and see if you can look at the watchdog timer...

I got the UNO from a local Arduino reseller/partner here in Sweden (Lawicel), I have only heard good things about this store so I am not concerned about a possible return of the UNO.
Only problem is that it's not a store very near to me so it will involve some shipping turn-around time, and here I was with a free weekend dedicated to coding and building. Oh, well, I guess the dog could use a long walk.

Thanks for all the help baum (and Lefty), much appreciated to get such quick help being a new user just storming in here with problems :slight_smile:

If you want you can email sparkfun technical service (even though you didn't buy it from them, they are very helpful). They may be able to help.... :slight_smile:

Good luck!