When you program the Arduino the microcontroller on the board has to be reset to enter the bootloader so you can upload the new software. Arduino, in their infinite wisdom, decided that this should be done automatically whenever you connect to the serial port. There are much better ways of doing it, but I guess that they decided on this with the first Arduino board before they really knew what they were doing (do they even now?) and are now stuck with it. Personally I would have used a different USB->RS232 chip right from the start - one that can do more than just serial comms, such as the MCP2200 which gives you both a CDC and a HID device in one, and 8 GPIO lines that you could use to trigger events on the board, like resetting, programatically.
However, I digress. There are two ways around the issue that I am aware of - one which damages the board permanently, and one which is far simpler and nicer.
- The official way is a permanent mod to the board. I wouldn't recommend this way - there is a far nicer way (see below). Look for a little link on the front called RESET-EN and cut it with a very sharp knife. This will disconnect the serial chip from the reset pin and the board will never auto-reboot again. However, this will break the ability to program the board easily. From now on you will have to manually reset the board by pressing the reset button at the right time to program it.
- The method I use is far simpler and less destructive. Connect a 22uF capacitor between RESET and GND (on the POWER header). You will need to disconnect it to program the board, but it doesn't involve breaking the board at all. Just plug it in between those two connections in the header, making sure the - side of the capacitor goes to ground. This works by holding the reset line high enough to stop the chip from resetting even when driven low by the USB chip. The reset switch should still work, but you may need to hold it in for a bit longer than normal.
On a side note, the ChipKIT™ UNO32™ has the same problem - they had to keep that functionality to maintain compatability with other Arduino products and methodologies. However, they have been much more sensible about it. They still have the little link to cut, but instead of it being between the pads of an 0805 footprint, they are between the plated through holes of a 0.1" 2-pin header. This way, you can cut the link, and install a normal jumper header. From then on you can enable and disable the reset functionality by just installing or removing a jumper.
The delay()
function does not return until the requsted interval has expired. During that time it constantly reads the clock, watching for the end of the interval. This means that your program can't do anything else during that time.
An alternative is to for your program set a timer that will interrupt whatever the program is doing when the time expires by calling a function to (in this case) toggle the LED state. Meanwhile, once the timer is set, your program can continue on to do something more useful.
In a blinky program, where blinking an LED is the whole point, there isn't anything else needing doing, but in general, you'll want your programs to be able to maintain a display, react to buttons or a keyboard, and read a few sensors, for example, and appear to do them simultaneously.
Update:
A clock or timer is a hardware register the program can read, which is automatically counted up or down at a fixed rate, such as once every millisecond. This counting is done by the hardware without the program having to do anything. To delay for say, 2 seconds, the delay()
function will read the clock register, add 2000 to it (the number of milliseconds of the required delay) to find the ending time, then keep reading the clock register until it equals or exceeds that time.
In the second case, above, a timer register would be set to 2000 (again, the number of milliseconds to delay), but in this case the hardware decrements that register every millisecond, and will interrupt the program when the timer register gets to 0.
Best Answer
Your delay will be the length of the newdelay() function plus the time it takes to send the serial data.
You have:
Each of those steps takes time.
To get an exact 1000ms time you can either trigger the serial sending from a 1 second interrupt, or you can examine the millisecond count within your loop and send the serial data when the milliseconds loop through 1000:
(another way to stop it looping multiple times for a single millisecond would be to add a short delay inside the if(..) to ensure that the routine takes at least 1ms)
The Serial.println() function will take a varying amount of time depending on:
At 9600 baud you are sending 9600 symbols per second. With "8N1" format that's 10 symbols per byte. So for a string of 3 digits, plus the carriage return and line feed, that will be 10 symbols × 5 characters, which is 50 symbols.
9600 baud is 0.0001041670.000104167s per symbol (or 104µS per symbol), so 50 symbols will be 0.00520835s (or 5.20835ms).
That is the actual transmission time. You then also have to add on to that the time taken to actually do the formatting of the data and the calling of the serial routines. These all take an unspecified amount of time. To find this out you will need to know the assembly code the routine compiles into then find the number of clock cycles each instruction takes and total them up.