Projeto Escada Inteligente com Arduino Mega e 2 Shields Ultrassonicos

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.
   

}

Consertei um problema eu consegui fazer o shield rele não iniciar ligado,o unico problema até agora está sendo fazer com que os 2 sensores possam acionar e desacionar os reles por que o que está acontecendo é que um sensor só pode acionar e o outro só pode desacionar , uma coisa que eu notei é que o primeiro sensor que foi colocado os pinos de trigger e eco e responsavel por acionar já o outro é responsavel por desacionar o shield rele, se alguém puder me ajudar agradeço.

#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);
digitalWrite (Pinorele1,HIGH);
pinMode (Pinorele2,OUTPUT);
digitalWrite (Pinorele2,HIGH);
pinMode (Pinorele3,OUTPUT);
digitalWrite (Pinorele3,HIGH);
pinMode (Pinorele4,OUTPUT);
digitalWrite (Pinorele4,HIGH);
pinMode (Pinorele5,OUTPUT);
digitalWrite (Pinorele5,HIGH);
pinMode (Pinorele6,OUTPUT);
digitalWrite (Pinorele6,HIGH);
pinMode (Pinorele7,OUTPUT);
digitalWrite (Pinorele7,HIGH);
pinMode (Pinorele8,OUTPUT);
digitalWrite (Pinorele8,HIGH);

}


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.
   

}

Consegui terminar vou disponibilizar o codigo aqui no site

// PROJETO ESCADA INTELIGENTE COM ARDUINO MEGA E SHIELD RELE COM 8 RELES                                                                                                                                                       |
// DESENVOLVIDO POR LUCAS SANDES TEIXEIRA                                                                                                                          

#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);
digitalWrite (Pinorele1,HIGH);
pinMode (Pinorele2,OUTPUT);
digitalWrite (Pinorele2,HIGH);
pinMode (Pinorele3,OUTPUT);
digitalWrite (Pinorele3,HIGH);
pinMode (Pinorele4,OUTPUT);
digitalWrite (Pinorele4,HIGH);
pinMode (Pinorele5,OUTPUT);
digitalWrite (Pinorele5,HIGH);
pinMode (Pinorele6,OUTPUT);
digitalWrite (Pinorele6,HIGH);
pinMode (Pinorele7,OUTPUT);
digitalWrite (Pinorele7,HIGH);
pinMode (Pinorele8,OUTPUT);
digitalWrite (Pinorele8,HIGH);

}

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).
    }
  }
 
}
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() 
{ 
if ((cm[1] > 1 && cm[0] < 10)|(cm[0]>1 && cm[1] < 10)&&(currentSensor==SONAR_NUM-1))
    
  {
digitalWrite( Pinorele1,LOW);
delay(100);
digitalWrite( Pinorele2,LOW);
delay(100);
digitalWrite( Pinorele3,LOW);
delay(100);
digitalWrite( Pinorele4,LOW);
delay(100);
digitalWrite( Pinorele5,LOW);
delay(100);
digitalWrite( Pinorele6,LOW);
delay(100);
digitalWrite( Pinorele7,LOW);
delay(100);
digitalWrite( Pinorele8,LOW);
delay(100);
   }
    
  
 if ((cm[1] > 1 && cm[0] < 10)|(cm[0]>1 && cm[1] < 10)&&(currentSensor==SONAR_NUM-1))
    {
digitalWrite( Pinorele1,HIGH);
delay(200);
digitalWrite( Pinorele2,HIGH);
delay(200);
digitalWrite( Pinorele3,HIGH);
delay(200);
digitalWrite( Pinorele4,HIGH);
delay(200);
digitalWrite( Pinorele5,HIGH);
delay(200);
digitalWrite( Pinorele6,HIGH);
delay(200);
digitalWrite( Pinorele7,HIGH);
delay(200);
digitalWrite( Pinorele8,HIGH);
delay(200);      
  }
  

}