Can't print in the serial port in the startup ()

HI guys this is stupid question , but I am having a problem with the following code:

void setup() {
  // put your setup code here, to run once:
     char dumy [5];
     pinMode(13,OUTPUT);
     Serial.begin(9600);
     //Data to store.
     StartEPRmAddress = 0;
    // this is an idea to establish the serial connection with the board after each start up 
    Serial.flush();
    delay(1000); 
    BLINK(13,100,5);
     Serial.println("T");
    delay(10); 
    
     

}

I expect that when I unplug or plug the Arduino back it should print T in the serial but it doesn't.
If I restart the board , by pushing the button or unplug and plug I dont see the T is printed.
I am testing while the serial monitor open to view the changes.

thanks

Serial.flush() does nothing useful for you.

Move the Serial.print() so it comes immediately after Serial.begin()
And print a longer message such as Serial.println("Arduino Starting"); in case a single character on the screen is not noticed.

If that does not help then post your complete program.

...R

Try to wait until the serial port is ready:

Serial.begin(9600);
while (!Serial) delay(1);

I did that it didn't work,

that is all my code the other part is a function called BLINK which has nothing doto with it

Danois90:
Try to wait until the serial port is ready:

Serial.begin(9600);

while (!Serial) delay(1);

NO that didn't work either ,
it only prints once i upload the sketch , but if i unplugged the usb or pushed the reset button it doesn't work

bemin:
that is all my code the other part is a function called BLINK which has nothing doto with it

Bzzzzzt, wrong answer. What about loop()?

And a rule of thumb, emphasis on the rule part, is to always post all the code.

Since we can't test your code since it's incomplete and thus uncompileable, I compiled this:

void setup()
{
  Serial.begin(9600);
  Serial.println("T");
}

void loop()
{

}

It certainly prints a T when I press reset, but not when the usb is unplugged and re-plugged.

This:

I expect that when I unplug or plug the Arduino back it should print T in the serial but it doesn't.

..... might be a false expectation, but I don't know anything about the mechanics of the serial interface / monitor when the usb is plugged back in.

bemin:
I did that it didn't work,

that is all my code the other part is a function called BLINK which has nothing doto with it

What do you mean by "that",

And it is NOT all of your code because what you posted won't compile and cannot be tested.

...R

My crystal ball tells me that BLINK is a blocking function that never finishes.

wilfredmedlin:
Since we can't test your code since it's incomplete and thus uncompileable, I compiled this:

void setup()

{
 Serial.begin(9600);
 Serial.println("T");
}

void loop()
{

}




It certainly prints a T when I press reset, but not when the usb is unplugged and re-plugged. 


This:

..... might be a false expectation, but I don't know anything about the mechanics of the serial interface / monitor when the usb is plugged back in.

I tried your code it didn't work with me
what board are you using?
i am using Arduino MIcro

here is the full code, the led Blinks but nothing on the serial

int StartEPRmAddress = 0; 
void setup() {
  // put your setup code here, to run once:
     char dumy [5];
     pinMode(13,OUTPUT);
     Serial.begin(9600);
     //Data to store.
     StartEPRmAddress = 0;
    // this is an idea to establish the serial connection with the board after each start up 
    Serial.flush();
    delay(1000); 
    BLINK(13,100,5);
     Serial.println("T");
    delay(10); 
    
     

}

void loop()
{

}


void BLINK (int LED, int Interval_msec, int num_of_blink ){
   for (int i = 0; i < num_of_blink ; i++){
  digitalWrite(LED, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(Interval_msec);                       // wait for a second
  digitalWrite(LED, LOW);    // turn the LED off by making the voltage LOW
  delay(Interval_msec);                       // wait for a second
   }
}

bemin:
here is the full code, the led Blinks but nothing on the serial

You don't seem to have tried anything that has been suggested to you.

...R

For the Micro, replace Serial.flush() by while(!Serial)

The 32U4 based boards require that before the first print. If you omit it, Serial.print will print to nowhere.

Robin2:
You don't seem to have tried anything that has been suggested to you.

...R

YES I TRIED every thing

I tried While((!Serial()) it didn't work but it gave me another clue

When I push the restart button, the board will boat, but then I have to close Serial monitor and open it again inorder for the print to be excuted. It is almost like the startup() function it is not getting called until I actually close the Serial monitor and open it again ?
WHy is that? and is there any work around that?

I guarantee you that start up is running. It could be a Windows / USB COM Driver / Serial Monitor thing. Try:

void setup()
{
  Serial.begin(9600);
  delay(20000);
  Serial.println("T");
}

void loop()
{}

After plugging in (and before 20 second delay finishes) close and then re-open the Serial Monitor.

bemin:
YES I TRIED every thing

Well that was not obvious from the program you posted.

Post the program with the changes included.

...R

Okay Guys here is one more Clue,
I tried using Hyper Terminal to see what is going on.
Same thing happens, when I first connect everything works and serial.print("Arduino starting")
is printed. However If I pushed the reset button then nothing comes up right away, but I have to disconnect my hyper terminal and then reconnect it back , to see Arduino starting .

is sames to me that it stays at Setup() at the line (while!Serial() ) until I close and open hyper terminal again.

Any ideas what could be going on ?

Note: I have used Hyper terminal before, and usually if the connection is made and the machine is starting it starts right up and print the welcome massage with no need to reset the connection.

If there s no wasy around it ,How can I invoke the serial connection from arduino used code , if that makes sense ?

bemin:
Okay Guys here is one more Clue,

This is not an Agatha Christie novel and I have no particular interest in figuring it out.

Post the code with the changes that have already been suggested and tell us exactly what it does.

...R

I think this is just how it is. When you reset an Arduino board with an external USB-serial chip it's only the primary microcontroller that's reset. So the USB-serial chip keeps the serial port open. However, on the Micro and other ATmega32U4 based boards the ATmega32U4 also handles the serial port so when you reset those boards the connection is lost. You need to restart Serial Monitor to reconnect with the Micro's serial port.

bemin:
is sames to me that it stays at Setup() at the line (while!Serial() ) until I close and open hyper terminal again.

That's correct. That's the whole point of that line. The ATmega32U4 based boards such as your Micro work a bit differently in that they don't reset when you make a connection to the serial port. This means that normally if you print something when the program first starts you won't see it on the Serial Monitor because it was already printed before you could open the Serial Monitor. So while!Serial(); causes the board to wait until a serial connection is opened so you don't miss anything. This means if you have a program that prints some stuff (like debug messages) to the Serial Monitor but sometimes you want to run it without Serial Monitor open you would not want that line in your code since that will cause the program to hang instead of carrying on without the Serial Monitor being open.