Someone can tell me why this LED doesn't blink a regular time ?

With this code and USB monitor actived the LED doesn't respect the delay of 1000ms for OFF condition:

/*
File: Blink_with_LED_always_ON_and_little_time_OFF_when_serial_is_act.ino

Why not it turns on an LED on for one second, then off for one second, repeatedly when serial is actived ? This get ON everytime except for more little time is OFF

This example code is in the public domain.
*/

// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;
int T1;

// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication with computer:
Serial.begin(9600);
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
T1=T1+1;
Serial.print("T1=");
Serial.println(T1);
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}

Someone can explain me the reason ?

If I remove the red part the led blink regular ON=OFF=1000ms

Best regards,
Enrico

Works fine HERE, with the Serial code in.

See #7: http://forum.arduino.cc/index.php/topic,148850.0.html

T1=T1+1;

What is the value of T1 when you first add 1 to it, and then what is it's value after adding 1 each time? At what point do you give it an initial value, or 'initialise' it?

You can also write the addition easier as

T1++;

It is also good practice to use lowercase or camelCase for variables and UPPERCASE for constants.

tack:

T1=T1+1;

What is the value of T1 when you first add 1 to it, and then what is it's value after adding 1 each time? At what point do you give it an initial value, or 'initialise' it?

In the sketch shown, T1 is defined outside of a function, and therefore it is a global variable. All static and global variables that are not otherwise initialized, are set to 0 before the sketch runs.

Works fine on my machine.

Hi to all,
now I had disclosed why the current sketch with output pin #1 to a LED doesn't produce a regular ON/OFF timing ...

Because I'm a novice :slight_smile:

In my first sketch I have wrote the pin #13 thinking that it was the same thing for #1 where I was really connected with an external LED outside the board with 330ohm resistor to GND. But...infact this is not exactly the same result that you will have obtained...
This because if you try with the following sketch:

/*
  File: Blink_with_LED_always_ON_and_little_time_OFF_when_serial_is_act.ino
  
  Why not it turns on an LED on for one second, then off for one second, repeatedly when serial is actived ? This get ON everytime except for more little time is OFF
 
  This example code is in the public domain.
 */
 
// Pin 1 has an LED connected on Digital connector pin #1.
// give it a name:
int led = 1;
int T1=0;

// the setup routine runs once when you press reset:
void setup() {                
  // initialize serial communication with computer:
  Serial.begin(9600);                   
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);     
}

// the loop routine runs over and over again forever:
void loop() {
  T1++;
  Serial.print("T1=");
  Serial.println(T1);  
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}

You will can see the LED always ON and a little time OFF (an instant).
Now for my knowledge I had tried the same on each pin the same code with an external LED and from pin #0 to #13 of Arduino UNO, so I'd see that all pins from #2 to #13 the timings ON/OFF works properly, while in pin #0 it's stayed always ON and #1 always ON with a little OFF time.

Sorry me for the long and detailed explanation, but this maybe help other novices like me...so the conclusion is that not all digital outputs are really usable from pin #0 to #13 but only from #2 to #13, because pin #0 and #1 are in common with the serial RX and TX with TTL signals internally connected to the chip ATmega8U2 that is the converter TTL-->USB, present in the PCB of Arduino UNO rev.3. This mean that a that pins #0 and #1 are present data communication packets that are quite the same coming from USB cable connected between the PC and Arduino Uno...So if want use it I must be sure to don't use the USB cable connected and/or it's better keep free because in other words if I want upload a new firmware I could have more problems with other things connected here.

Maybe it's possible to use these pin normally but not for me at this stage...

Thank you for the support about initialization of variables and short notation to increase the counter in other way.

Bye

MichaelMeissner:

tack:

T1=T1+1;

What is the value of T1 when you first add 1 to it, and then what is it's value after adding 1 each time? At what point do you give it an initial value, or 'initialise' it?

In the sketch shown, T1 is defined outside of a function, and therefore it is a global variable. All static and global variables that are not otherwise initialized, are set to 0 before the sketch runs.

Always good practice to explicitly initialise a variable, or assign a value, prior to first operations though, would you agree?

Try this and than you can figure it out.

Serial.print("T1= ****************************************************************************************************************************************************************************************************************************************************************************************************************************");

Or add another 1 second delay AFTER the last Serial print.

Say away from pins 0 and 1 as they are the pins used by serial!.

Mark

What happens when you make this change (add the LED with 220R resistor pin D2)?

int led = 2;

Thank to all,
maybe it's not well written, but I'd resolved and understand by myself... :slight_smile:

Always good practice to explicitly initialise a variable, or assign a value, prior to first operations though, would you agree?

It's up to you. I don't because I know they are zero (only statics/globals, auto variables are not set to 0). But if you feel safer doing the intiialization, that's ok. The one exception is if you hit the wall and have run out of flash memory, those extra stores in the beginning of the problem might get you under the threshold. But that is fairly rare.

MichaelMeissner:

Always good practice to explicitly initialise a variable, or assign a value, prior to first operations though, would you agree?

It's up to you. I don't because I know they are zero (only statics/globals, auto variables are not set to 0). But if you feel safer doing the intiialization, that's ok. The one exception is if you hit the wall and have run out of flash memory, those extra stores in the beginning of the problem might get you under the threshold. But that is fairly rare.

Really?

Vaclav:

MichaelMeissner:
It's up to you. I don't because I know they are zero (only statics/globals, auto variables are not set to 0). But if you feel safer doing the intiialization, that's ok. The one exception is if you hit the wall and have run out of flash memory, those extra stores in the beginning of the problem might get you under the threshold. But that is fairly rare.

Really?

What is it you are asking about with Really? The various ISO C/C++ standards require that all static and global variables that are not initialized to other values are set to zero. The Arduino library does this before control reaches the static constructors that are run before the setup function is called.

Auto variables are on the stack frame or instantiated in machine registers and they are not cleared when the function is called. So uninitialized variables have whatever values the memory/register had at the time of the call.

Now, for statics/globals if the variables are initialized to 0 by default are placed in a different section by the compiler (bss instead of data). On many embedded systems, the data section is copied from an area in flash memory at program startup, and then the bss is set to 0. So, you might be able to save a little flash memory by using implicit initialization instead of explicitly assigning 0 statically. I haven't looked under the covers in the Arduino to see if that is the case for Arduino libraries.