Blink doesn't work without serial
Moderators: adafruit_support_bill, adafruit

Blink doesn't work without serial

by arekuanubis on Thu May 05, 2011 9:29 pm

Ok... So I'm getting a really weird glitch here with my arduino uno, and I had hope that maybe someone had an idea as to what might be going on.

If I load the standard, basic, blink program from the Arduino IDE, the arduino pin 13 stays solidly lit. If I modify the program by putting Serial.begin(9600); in the setup() function, pin 13 blinks as the standard program should be doing.

If I take the standard blink program, without the Serial.begin line, and change the delays each to 1, the LED on pin 13 flashes breifly, then stays off.

I've checked the fuses vs my boards file, and I've even tried reflashing the boot loader... I'm not sure what's up here.

The information I got from my boards file:

Code: Select all | TOGGLE FULL SIZE
uno.bootloader.low_fuses=0xff
uno.bootloader.high_fuses=0xde
uno.bootloader.extended_fuses=0x05
uno.bootloader.path=optiboot
uno.bootloader.file=optiboot_atmega328.hex
arekuanubis
 
Posts: 16
Joined: Fri Mar 04, 2011 1:20 pm

Re: Blink doesn't work without serial

by adafruit_support_bill on Fri May 06, 2011 5:36 am

That is really strange. :? It sounds as if the processor is working - at least well enough to download and run sketches. And the LED does blink in some circumstances, so it is not some defect on the board.

It sounds almost as if Timer0 is not setup properly unless you do the Serial.begin(). Does PWM on pins 5 and 6 work?
User avatar
adafruit_support_bill
 
Posts: 29674
Joined: Sat Feb 07, 2009 9:11 am

Re: Blink doesn't work without serial

by arekuanubis on Wed Jun 01, 2011 9:13 pm

Sorry for taking so long to get back to you...

I now have two arduinos that both have this issue.. They're both Unos, but they were bought from different companies. This makes it even weirder....

Now here is the FULL story for uno number 2...

1. I opened the box, examined it, went "cool" and put it back in the box.. Didn't have time to play with it yet.
2. Waited 2-3 months.
3. Took it back out of the box to attempt to try and use an ethernet shield with it. Plugged in the ethernet shield, then the USB cable, observed the LED on pin 13 blinking happily.
4. Used arduino 0022 to upload the example WebServer program, it uploaded fine, tested it out, got a page full of gibberish.
5. Removed the USB cable and the ethernet shield (in that order)
6. Plugged ethernet shield back in, opened up example blink program, loaded it. Loaded fine.
7. Observed LED on pin 13.. Remains solid...
8. Loaded example program BlinkWithoutDelay, loaded fine, LED blinks as expected...


Now to answer your question.... I used the following code:
Code: Select all | TOGGLE FULL SIZE
#include <Servo.h>

Servo myservo;

void setup() {
  myservo.attach(5);
  pinMode(6, OUTPUT);
}
void loop() {
  digitalWrite(6,HIGH);
  myservo.write(90);
}


When measured with a volt meter, I got 5 volts on pin 6, 0 volts on pin 5.
arekuanubis
 
Posts: 16
Joined: Fri Mar 04, 2011 1:20 pm

Re: Blink doesn't work without serial

by westfw on Thu Jun 02, 2011 12:02 am

ethernet shield

Pin13 (where BLINK normally flashes the LED) is shared with the SPI interface used to communicate with the ethernet board. I don't think you can have SPI active and still expected pin13 to be usable as a normal digital output...

I'm not sure what will happen if the ethernet board is plugged in (and connected to the SPI pins) and you load a sketch that doesn't use SPI. (pin 13 is the SPI clock signal, so in theory it is NOT driven by the shield. But it still seems like a bad idea...)
User avatar
westfw
 
Posts: 1373
Joined: Fri Apr 27, 2007 12:01 pm
Location: SF Bay area

Re: Blink doesn't work without serial

by arekuanubis on Thu Jun 02, 2011 11:19 pm

While this is true, the blink problem occurs even on a board without the Ethernet shield.
arekuanubis
 
Posts: 16
Joined: Fri Mar 04, 2011 1:20 pm

Re: Blink doesn't work without serial

by adafruit_support_bill on Fri Jun 03, 2011 5:04 am

0 volts on pin 5.

Servo pulses are pretty short, so the expected value would be pretty low. Try doing analogWrite to pins 5 & 6. With an analogWrite(128), you should see close to 2.5v.
User avatar
adafruit_support_bill
 
Posts: 29674
Joined: Sat Feb 07, 2009 9:11 am

Re: Blink doesn't work without serial

by arekuanubis on Sat Jun 04, 2011 8:52 am

Code: Select all | TOGGLE FULL SIZE
void setup() {
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
}
void loop() {
  analogWrite(5,128);
  analogWrite(6,128);
}


Produced a constant 5 volts on both pins 5 and 6.When connected to an o-scope, there is still no wave form.

But.. similar to what I said in the original post, if I add in a Serial.begin() call, it works fine... The following code

Code: Select all | TOGGLE FULL SIZE
void setup() {
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
 Serial.begin(9600);
}
void loop() {
  analogWrite(5,128);
  analogWrite(6,128);
}


generates 2.5 volts on both pins 5 and 6, as well as a wave form when connected to my o-scope.
arekuanubis
 
Posts: 16
Joined: Fri Mar 04, 2011 1:20 pm

Re: Blink doesn't work without serial

by adafruit_support_bill on Sat Jun 04, 2011 9:46 am

Sounds like what I originally thought - a Timer 0 problem. Timer 0 controls the PWM on pins 5 & 6 too.
I've never heard of that problem before. I'll have to dig out one of my Uno's and give it a try.
User avatar
adafruit_support_bill
 
Posts: 29674
Joined: Sat Feb 07, 2009 9:11 am

Re: Blink doesn't work without serial

by arekuanubis on Sun Jun 05, 2011 9:56 am

Correct me if I'm wrong here, but... If it really is a timer0 problem, and since the Serial.begin() call fixes it.... That seems to indicate that the Serial.begin function is running setup code that is not running otherwise, even though it should be... This then sounds like it might be a problem with the libraries shipped with my copy of the arduino program, don't you think?

For what it's worth... I'm using arduino-0022 on a 64bit linux OS. (I tend to think that the OS has little to do with this error though)
arekuanubis
 
Posts: 16
Joined: Fri Mar 04, 2011 1:20 pm

Re: Blink doesn't work without serial

by adafruit_support_bill on Sun Jun 05, 2011 10:23 am

This then sounds like it might be a problem with the libraries shipped with my copy of the arduino program, don't you think?

I think that is probably the case.
I just tried the sample code you posted and get 2.5v on both 5 & 6 with and without the Serial.begin. I'm using Arduino 22 on a 64 bit Windows-7 machine.
User avatar
adafruit_support_bill
 
Posts: 29674
Joined: Sat Feb 07, 2009 9:11 am

Re: Blink doesn't work without serial

by westfw on Sun Jun 05, 2011 5:52 pm

I'm using arduino-0022 on a 64bit linux OS.

Wasn't there another problem reported with millis() not working correctly on some linux installs?
(The problem is that with a linux arduino build, the compiler and basic C libraries and include files (NOT the arduino libraries) are installed separately from the Arduino IDE, so it's possible for a linux Arduino build to be using a buggy avr-gcc compiler even though the windows and Mac installs of the same Arduino version work fine.)

I'll see if I can dig it up...
(edit. Here? http://arduino.cc/forum/index.php/topic ... #msg363688 )
User avatar
westfw
 
Posts: 1373
Joined: Fri Apr 27, 2007 12:01 pm
Location: SF Bay area

Re: Blink doesn't work without serial

by arekuanubis on Wed Jun 08, 2011 9:38 pm

Looks like you all were dead on. I tossed a global variable into the file and added 1 to it in the setup function, which made the blink program work again. Very much a weird error...

The specific version of linux I'm using is arch, which is mentioned in the other thread you posted and, when checking my binutils-avr package, it's the exact same version which has the reported problem.

That's kind of annoying, but at least you were able to point me to a functional work around!

Thanks! :)
arekuanubis
 
Posts: 16
Joined: Fri Mar 04, 2011 1:20 pm

Re: Blink doesn't work without serial

by synack on Sat Jun 11, 2011 10:01 pm

I'm seeing this error on 64bit Fedora 14 Linux as well. Same problem behaviour and the fixes mentioned do work. yum still installs 0021 but, based on what I'm seeing here, it doesn't sound like 0022 would resolve the issue. Some thoughts on the workaround, though:

Anyone having this issue with programs that already include global variables may try to be clever and just add an instruction into setup() that doesn't actually change the value of the global from whatever it's initially instantiated to. For example, you declare "boolean done = false;" and then simply add "done = false;" to setup(). The compiler includes some pretty sophisticated optimization routines to try and get the code as tight as possible (which is a good thing!) so cheating like this won't resolve the issue the same way arekuanubis' fix would.

Likewise, not providing an initial value at all and then trying to set it in setup() appears to cause the compiler to simply relocate the set instruction up into the initial instantiation. Again, you end up sending the board a broken sketch.

A cheat that does appear to work, though, is instantiating the global to something *other than* what you actually want it initialized to and then setting it properly in setup(). This provides a definite state going in and another definite, nonequal state going back out so the compiler cannot optimize. For example:

boolean done = true;
setup() {
done = false;
}
User avatar
synack
 
Posts: 1
Joined: Sat Jun 11, 2011 9:30 pm

Re: Blink doesn't work without serial

by mtbf0 on Sun Jun 12, 2011 8:49 am

first of all, i haven't had this problem. this may be why...

last year when i installed opensuse 11.2, the avr-gcc packages were really buggy, so i built avr-binutils, avr-gcc, avr-libc and avrdude from sources. it's not hard. seems like i've done it a lot. there are good instructions in the avr-libc documentation, here.

the problem last time was that release versions avr-gcc and avr-libc were out of sync and i had to use a slightly older version of libc to get it to build.

if you build avr-gcc, you'll need headers for a couple of multi-precision math libraries, gmp and mfpr. when i've done builds on debian and suse i've always had to go find development packages to get the compiler to compile so, if config won't configure or make won't make, that may be your problem.

not for everyone, but makes for an educational afternoon.
"i want to lead a dissipate existence, play scratchy records and enjoy my decline" - iggy pop, i need more
User avatar
mtbf0
 
Posts: 1645
Joined: Fri Nov 09, 2007 11:59 pm
Location: oakland ca

Re: Blink doesn't work without serial

by westfw on Sun Jun 12, 2011 4:31 pm

I've had all sorts of problems recently trying to build gcc on my Mac. It seems that some of the packages default to installing 32bit libraries, and some default to installing 64bit libraries, and gcc decides it can't find the right versions :-(
User avatar
westfw
 
Posts: 1373
Joined: Fri Apr 27, 2007 12:01 pm
Location: SF Bay area