Estou Desenvolvendo um projeto com o Arduino mega gostaria de programa-lo para poder acender spots em uma escada para isso estou utilizando um shield rele com 8 reles e 2 sensores ultrassonicos a programação não está funcionando como eu queria, por que assim que eu ligo o arduino ele já inicia o shield rele, sem que necessite do sinal de um dos sensores, gostaria de saber como posso fazer para que as saidas, iniciem somente quando um dos sensores mandar o sinal,gostaria também de saber como faço para que qualquer um dos sensores possa ligar e também possam desligar o shield rele por que não estou conseguindo fazer isso a programação que foi feita apenas faz com que um sensor ligue e o outro desligue o shield rele.
#include <NewPing.h>
#define SONAR_NUM 2 // Number of sensors.
#define MAX_DISTANCE 10 // Maximum distance (in cm) to ping.
#define PING_INTERVAL 33 // Milliseconds between sensor pings (29ms is about the min to avoid cross-sensor echo).
#define Pinorele1 22
#define Pinorele2 23
#define Pinorele3 24
#define Pinorele4 25
#define Pinorele5 26
#define Pinorele6 27
#define Pinorele7 28
#define Pinorele8 29
unsigned long pingTimer[SONAR_NUM]; // Holds the times when the next ping should happen for each sensor.
unsigned int 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(2,3, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping.
NewPing(50,51, MAX_DISTANCE),
};
void setup() {
Serial.begin(115200);
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;
pinMode (Pinorele1,OUTPUT);
pinMode (Pinorele2,OUTPUT);
pinMode (Pinorele3,OUTPUT);
pinMode (Pinorele4,OUTPUT);
pinMode (Pinorele5,OUTPUT);
pinMode (Pinorele6,OUTPUT);
pinMode (Pinorele7,OUTPUT);
pinMode (Pinorele8,OUTPUT);
}
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).
}
}
if ((cm[1] > 1 && cm[1] < 10)&&(currentSensor!=LOW))
{
digitalWrite( Pinorele1,HIGH);
digitalWrite( Pinorele2,HIGH);
digitalWrite( Pinorele3,HIGH);
digitalWrite( Pinorele4,HIGH);
digitalWrite( Pinorele5,HIGH);
digitalWrite( Pinorele6,HIGH);
digitalWrite( Pinorele7,HIGH);
digitalWrite( Pinorele8,HIGH);
}
if (cm[0]>1)
{
digitalWrite( Pinorele1,LOW);
digitalWrite( Pinorele2,LOW);
digitalWrite( Pinorele3,LOW);
digitalWrite( Pinorele4,LOW);
digitalWrite( Pinorele5,LOW);
digitalWrite( Pinorele6,LOW);
digitalWrite( Pinorele7,LOW);
digitalWrite( Pinorele8,LOW);
}
}
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.
// The following code would be replaced with your code that does something with the ping results.
}