Garbage values in serial monitor using mega 2560

Hi,

I am new to the forum, but have been using different Arduinos for a while now. I recently ran into a fairly weird problem and I don't know what the cause is. My code should simply output a bunch of float values. Although, after a couple of seconds it works perfectly, the initial few values will have some kind of garbage.

This is my code

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
  for (int i =1;i<5;i++)
{  Serial.println(float(i)+2.0);
delay(1);
  // put your main code here, to run repeatedly:
}
}

This is the output I am getting

4.00

5.00

6.00

3.00

4.00

5.‚j

6.°3.00

4.00

5.00

6.00

Since I need to use this data elsewhere, I really need to make sure everything is right. I did a lot of digging on why this could be so and found this post. I was wondering if an updated firmware on the mega2560 will solve my problem. If so, where can I find this updated firmware? Should I just follow what Arduino recommends for updating firmware or is there something else I should keep in mind?

Thank you

It couldn't hurt to refresh the firmware on the USB-to-Serial processor.

A 1 millisec delay means there is a lot of data being sent. I would try a 100msec delay and see if that makes any difference.

I would also print something from setup() to show when the program starts - for example
Serial.println("name of my program");

...R

At 9600 Baud your data will take about 5 milliseconds to send, ignoring any overheads in the code.
You could try increasing the Baud rate.

Hey,

So I finally got some time to go through all the suggestions. I updated the firmware using FLIP (Although I am not sure how I can verify the driver has been updated). I also added a Serial.println() to the setup.

My code now is:

float a=2;

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
delay(100);
Serial.println("Hello");
}

void loop() {
  float val;
  for (int i = 1;i<5;i++)
  {
    val = i + 2.0;
    Serial.println(val);
    delay(5);
  }
}

and the output is still pretty much the same (See output.png). This output is in no means consistent. The 'Hello' can appear later on as well

I still don't know what's wrong :expressionless:

output.png

void loop() {
  float val;
  for (int i = 1;i<5;i++)
  {
    val = i + 2.0;

What do you think the initial value of "val" is?

"This output is in no means consistent. The 'Hello' can appear later on as well"

It appears from your "output.png" that the output is consistent, based on your script.

What did you expect the output to display?

If the "Hello" appears later, then that indicated that the arduino is being reset. If you turn your laptop monitor off and on, It could be resetting the arduino. If "hello" appears again, and you did not do anything, then you need to figure out why the arduino is getting reset.

AWOL:

void loop() {

float val;
  for (int i = 1;i<5;i++)
  {
    val = i + 2.0;



What do you think the initial value of "val" is?

It doesn't matter. It's not used before a value is assigned to it.

Oops - I saw a "+=".

Must get closer to the screen.

LOL, yea, it's 5 o'clock somewhere. I'm ready to open a bottle of Octoberfest that I started brewing 4 weeks ago. If I am incoherent for the rest of the night, you will know why.. lol

I'm already a good way through a rather splendid bottle of Westerhall Grenadian rum.

@AWOL, it being 5:30 here, I would guess it would be about 10:30 there. Have a great rest of the day.

After I compile and upload the code to the board, wouldn't arduino automatically reset and start printing? Then shouldn't it print 'hello' first followed by the numbers? I don't understand why there's some garbage before the 'hello'

mathewsmjohn1992:
After I compile and upload the code to the board, wouldn't arduino automatically reset and start printing? Then shouldn't it print 'hello' first followed by the numbers? I don't understand why there's some garbage before the 'hello'

When you start the serial monitor it resets the Arduino. During the reset it may send spurious characters (garbage) to the serial monitor, probably due to some internal 'handshaking' procedure. I don't think there's a way to stop that. Can't you just ignore them? Or print a few line feeds before your "hello", so the garbage quickly scrolls off the top of the serial monitor.

Hold the reset button on the Mega.
Open the serial monitor.
Release the reset button.
No gobbledygook.
?
Leo..

Hi,
Have you tried higher serial speeds?

Tom..... :slight_smile:

I guess I'll just ignore everything until my 'Hello' and then read all data from there. Guess that's the only solution :\

TomGeorge:
Hi,
Have you tried higher serial speeds?

Tom..... :slight_smile:

I tried pretty much every baud rate. There's always some random output before the 'Hello'. Guess it's something internal. I'll just ignore it. :slight_smile:

If the only problem you now have is the garbage before your setup text, I have intermittently the same problem with a number of my projects/experiments on the serial monitor.
I just take it as the controller resetting and the serial output having a little epileptic fit.

Tom.... :slight_smile:

TomGeorge:
I just take it as the controller resetting and the serial output having a little epileptic fit.

Tom.... :slight_smile:

Haha :smiley: .. I really thought there was a fix. But since the workaround is working I am happy :P.

Is this still happening? or is the 'Hello' only showing when "you" reset the arduino?