gandalf50:
Now two of the sensors work fine with the ethernet shield, using the pins above. Without the ethernet shield all 3 works.
I've looked at the 2560 pinout, but it didn't help figuring this out. Also tried several pin configs. I've also tried initializing the Ethernet shield (i.e. setting IP addr etc), but no luck (and tried with the 1.0 IDE as well).Only the 3 sensors are wired to the Mega (power via USB).
I'll try with an UNO with an Ethernet shield and an Arduino Ethernet.
/Magnus
I don't see any Ethernet code in that sketch. Does it fail just as is? What exactly happens?
The first thing you should ALWAYS try is to use different pins. Also, switch the sensors to see if the problem follows the sensors or the pins. My guess is that there's a conflict with the pins you are using. Using different pins is so simple that you should always try this first.
Try totally different pins and see if anything changes. If so, the conflict is with the Ethernet or SD library.
I'd also try to slow down the pings, change PING_INTERVAL 33 to PING_INTERVAL 250 and see if that does anything.
Finally, I'd try to not use a timer interrupt method of pinging as it could be just a simple timing issue with multiple libraries competing for CPU cycles. So instead of using ping_timer() just do a convention ping of each sensor. Here's a sketch that does just that to your 3 sensors:
#include <NewPing.h>
#define SONAR_NUM 3 // Number of sensors.
#define MAX_DISTANCE 200 // Maximum distance (in cm) to ping.
#define PING_INTERVAL 250 // Milliseconds between sensor pings (29ms is about the min to avoid cross-sensor echo).
NewPing sonar[SONAR_NUM] = { // Sensor object array.
NewPing(22, 23, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping.
NewPing(24, 25, MAX_DISTANCE),
NewPing(34, 35, MAX_DISTANCE)
};
void setup() {
Serial.begin(115200);
}
void loop() {
for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through all the sensors.
unsigned int cm = sonar[i].ping_cm();
Serial.print(i);
Serial.print("=");
Serial.print(cm);
Serial.print("cm ");
}
Serial.println();
delay(PING_INTERVAL);
}
Tim