HOla! estamos creando una instalación interactiva llamada Viaje al interior de la semilla, donde las personas activan 2 videos y 2 audios poniendo las manos en la tierra, elagua , etc. Utilizamos 4 sensores ultrasónicos y la librería NewPing y Procesing, el problema es que después de uno o dos ciclos de lecctura, los videos y los audios se reinician aunque la mano siga en el sensor, no se si es debido al reset de arduino (utilizo un arduino MEGA) , o si no se están comunicando bien ARduino y Procesing? Cuáles son los requisitos mínimos de hardwre para esto que consideran necesario?
dejo el código para ver si ven algo que yo no veo, soy nueva en esto!! Quizás hay algún problema con la memoria?
#include <NewPing.h>
#define SONAR_NUM 4 // Number or sensors.
#define MAX_DISTANCE 200 // Maximum distance (in cm) to ping.
#define PING_INTERVAL 633 // Milliseconds between sensor pings (29ms is about the min to avoid cross-sensor echo).
unsigned long pingTimer[SONAR_NUM]; // Holds the times when the next ping should happen for each sensor.
byte cm[SONAR_NUM]; // Where the ping distances are stored.
uint8_t currentSensor = 0; // Keeps track of which sensor is active.
NewPing sonar[SONAR_NUM] = { // Sensor object array.
NewPing(11, 12, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping.
NewPing(10, 9, MAX_DISTANCE),
NewPing(8, 7, MAX_DISTANCE),
NewPing(14, 15, MAX_DISTANCE)
};
void setup() {
Serial.begin(9600);
pingTimer[0] = millis() + 75; // First ping starts at 75ms, gives time for the Arduino to chill before starting.
for (uint8_t i = 1; i < SONAR_NUM; i++) // Set the starting time for each sensor.
pingTimer[i] = pingTimer[i - 1] + PING_INTERVAL;
}
void loop() {
for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through all the sensors.
if (millis() >= pingTimer[i]) { // Is it this sensor's time to ping?
pingTimer[i] += PING_INTERVAL * SONAR_NUM; // Set next time this sensor will be pinged.
if (i == 0 && currentSensor == SONAR_NUM - 1) oneSensorCycle(); // Sensor ping cycle complete, do something with the results.
sonar[currentSensor].timer_stop(); // Make sure previous timer is canceled before starting a new ping (insurance).
currentSensor = i; // Sensor being accessed.
cm[currentSensor] = 0; // Make distance zero in case there's no ping echo for this sensor.
sonar[currentSensor].ping_timer(echoCheck); // Do the ping (processing continues, interrupt will call echoCheck to look for echo).
}
}
// The rest of your code would go here.
}
void echoCheck() { // If ping received, set the sensor distance to array.
if (sonar[currentSensor].check_timer())
cm[currentSensor] = sonar[currentSensor].ping_result / US_ROUNDTRIP_CM;
}
void oneSensorCycle() { // Sensor ping cycle complete, do something with the results.
Serial.write(cm, SONAR_NUM);
};