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