For my job I am using an Arduino Mega 2560 + Ethernet Shield to build a datalogger for logging the voltage on all 16 analog pins.
I've got the timing and storage on microSD working, but now I'm running into some problems with the analogRead().
For those who want to test my problems: just write a program that reads all analog inputs one by one, and directly send the reading using Serial.print(); Run this loop without any delays.
Wire the analog pins to random voltages: GND, 3.3V, 5V, resistor divider, leave unconnected.
Sometimes the analogRead() function causes the Arduino to crash. Sometimes it is pin 0, sometimes 1, 7, no consistency here. Sometimes it hangs in the first loop, sometimes in the second, again no consistency. When I reset, it still hangs on the same pin in the same loop.
The only thing that seems to work is to change the voltage on the pin.
I really hate this, because this means that my datalogger isn't reliable and I will have to use MAXIM analog input to SPI chips.
Does anyone recognise my problems, and have a solution?
muddy:
For those who want to test my problems: just write a program that reads all analog inputs one by one, and directly send the reading using Serial.print(); Run this loop without any delays.
It's the most staightforward code ever. Using the MEGA btw.
If it crashes, it mostly happens right at the beginning or somewhere during the second readcycle.
It happens with inputs tied to 0V, 5V, 3,3V or floating.
This is exactly why Nick and I both asked you to post the actual code causing problems.
The example code you described: "just write a program that reads all analog inputs one by one, and directly send the reading using Serial.print(); Run this loop without any delays."
Then if I take your code (I expanded to use 5 analog inputs because 3 seemed to work for me and removed SD stuff because I don't have one attached) which uses "String":
That crashes after the first iteration every time I hit reset.
It is String causing your problems, not analogRead().
Also, you would do yourself a favor by wrapping the character strings used by your Serial.print()s in F(). e.g.:
Serial.println(F("Getting Started..."));
That will keep those string constants from using up RAM.
I didn't even consider that anything else could cause this problem.
Next week, I will apply the changes suggested and hopefully be a happy person!
Thanks
I didn't even consider that anything else could cause this problem.
When you think that thousands of other people read analogue pins every day without error, dear old William of Ockham's* favourite shaving implement suggests that whatever is causing your problem, it is not analogRead.
Good job he wasn't born just the other side of the A3 - he'd have been called Willy Wisley!
I didn't even consider that anything else could cause this problem.
Does this mean that you didn't follow your own advice?
For those who want to test my problems: just write a program that reads all analog inputs one by one, and directly send the reading using Serial.print(); Run this loop without any delays.