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.
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.
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
}
}
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?
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 ?
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.