Ultrasonic Sensor with Relay and Count of On of Relay

HI,

Can some one help me with my script. I am using 4 ultra sonic and 4 relays. And i also add the count function of Relay on. But this is not working. Can some one help me. Cont of relay on is not shown .

int RelayState = 0;   // counter for the number of button presses
int CurrentRelayState = 0;         // current state of the button
int LastRelayState = 0;     // previous state of the button

int Relay1 = 2;
int Relay2 = 3;
int Relay3 = 4;
int Relay4 = 5;

int trigPin1 = 6;
int echoPin1 = 7;

int trigPin2 = 8;
int echoPin2 = 9;

int trigPin3 = 10;
int echoPin3 = 11;

int trigPin4 = 12;
int echoPin4 = 13;

void setup() {
 Serial.begin (9600);
 pinMode(trigPin1, OUTPUT);
 pinMode(echoPin1, INPUT);
 
 pinMode(trigPin2, OUTPUT);
 pinMode(echoPin2, INPUT);
 
 pinMode(trigPin3, OUTPUT);
 pinMode(echoPin3, INPUT);

 pinMode(trigPin4, OUTPUT);
 pinMode(echoPin4, INPUT);
 
 pinMode(Relay1, OUTPUT);
 pinMode(Relay2, OUTPUT);
 pinMode(Relay3, OUTPUT);
 pinMode(Relay4, OUTPUT);
}

void firstsensor(){ // This function is for first sensor.
 int duration1, distance1;
 digitalWrite (trigPin1, HIGH);
 delayMicroseconds (100);
 digitalWrite (trigPin1, LOW);
 duration1 = pulseIn (echoPin1, HIGH);
 distance1 = (duration1/2) / 29.1;

     Serial.print("1st Sensor: ");
     Serial.print(distance1);  
     Serial.print("cm    ");

 if (distance1 < 30) {  // Change the number for long or short distances.
   digitalWrite (Relay1, HIGH);
 } else {
   digitalWrite (Relay1, LOW);
    // read the pushbutton input pin:
 CurrentRelayState = digitalRead(Relay1);

 // compare the CurrentRelayState to its previous state
 if (CurrentRelayState != LastRelayState) {
   // if the state has changed, increment the counter
   if (CurrentRelayState == HIGH) {
     // if the current state is HIGH then the button
     // wend from off to on:
     RelayState++;
     Serial.println("on");
     Serial.print("number of button pushes:  ");
     Serial.println(RelayState);
   } else {
     // if the current state is LOW then the button
     // wend from on to off:
     Serial.println("off");
   }
   // Delay a little bit to avoid bouncing
   delay(50);
 }
 // save the current state as the last state,
 //for next time through the loop
 LastRelayState = CurrentRelayState;
 }    
}
void secondsensor(){ // This function is for second sensor.
   int duration2, distance2;
   digitalWrite (trigPin2, HIGH);
   delayMicroseconds (100);
   digitalWrite (trigPin2, LOW);
   duration2 = pulseIn (echoPin2, HIGH);
   distance2 = (duration2/2) / 29.1;
 
     Serial.print("2nd Sensor: "); 
     Serial.print(distance2);  
     Serial.print("cm    ");
  
   if (distance2 < 20) {  // Change the number for long or short distances.
     digitalWrite (Relay2, HIGH);
   }
else {
     digitalWrite (Relay2, LOW);
   // read the pushbutton input pin:
 CurrentRelayState = digitalRead(Relay2);

 // compare the CurrentRelayState to its previous state
 if (CurrentRelayState != LastRelayState) {
   // if the state has changed, increment the counter
   if (CurrentRelayState == HIGH) {
     // if the current state is HIGH then the button
     // wend from off to on:
     RelayState++;
     Serial.println("on");
     Serial.print("number of button pushes:  ");
     Serial.println(RelayState);
   } else {
     // if the current state is LOW then the button
     // wend from on to off:
     Serial.println("off");
   }
   // Delay a little bit to avoid bouncing
   delay(50);
 }
 // save the current state as the last state,
 //for next time through the loop
 LastRelayState = CurrentRelayState;
   }    
}
void thirdsensor(){ // This function is for third sensor.
   int duration3, distance3;
   digitalWrite (trigPin3, HIGH);
   delayMicroseconds (100);
   digitalWrite (trigPin3, LOW);
   duration3 = pulseIn (echoPin3, HIGH);
   distance3 = (duration3/2) / 29.1;

     Serial.print("3rd Sensor: ");   
     Serial.print(distance3);  
     Serial.print("cm");
  
   if (distance3 < 10) {  // Change the number for long or short distances.
     digitalWrite (Relay3, HIGH);
   }
else {
     digitalWrite (Relay3, LOW);
   // read the pushbutton input pin:
 CurrentRelayState = digitalRead(Relay3);

 // compare the CurrentRelayState to its previous state
 if (CurrentRelayState != LastRelayState) {
   // if the state has changed, increment the counter
   if (CurrentRelayState == HIGH) {
     // if the current state is HIGH then the button
     // wend from off to on:
     RelayState++;
     Serial.println("on");
     Serial.print("number of button pushes:  ");
     Serial.println(RelayState);
   } else {
     // if the current state is LOW then the button
     // wend from on to off:
     Serial.println("off");
   }
   // Delay a little bit to avoid bouncing
   delay(50);
 }
 // save the current state as the last state,
 //for next time through the loop
 LastRelayState = CurrentRelayState;
   }   
}
void forthsensor(){ // This function is for forth sensor.
 int duration4, distance4;
 digitalWrite (trigPin4, HIGH);
 delayMicroseconds (100);
 digitalWrite (trigPin4, LOW);
 duration4 = pulseIn (echoPin4, HIGH);
 distance4 = (duration4/2) / 29.1;

     Serial.print("    4th Sensor: ");
     Serial.print(distance4);  
     Serial.print("cm    ");

 if (distance4 < 40) {  // Change the number for long or short distances.
   digitalWrite (Relay4, HIGH);
 } else {
   digitalWrite (Relay4, LOW);
   // read the pushbutton input pin:
 CurrentRelayState = digitalRead(Relay4);

 // compare the CurrentRelayState to its previous state
 if (CurrentRelayState != LastRelayState) {
   // if the state has changed, increment the counter
   if (CurrentRelayState == HIGH) {
     // if the current state is HIGH then the button
     // wend from off to on:
     RelayState++;
     Serial.println("on");
     Serial.print("number of button pushes:  ");
     Serial.println(RelayState);
   } else {
     // if the current state is LOW then the button
     // wend from on to off:
     Serial.println("off");
   }
   // Delay a little bit to avoid bouncing
   delay(50);
 }
 // save the current state as the last state,
 //for next time through the loop
 LastRelayState = CurrentRelayState;
 }    
}
void loop() {
Serial.println("\n");
firstsensor();
secondsensor();
thirdsensor();
forthsensor();
delay(10);
}

Is this spec-creep?

Your previous question (equally badly-posted) only mentioned three sensors.
You really, really need to get the hang of functions - that sketch should be about 40 lines long.

Sorry for the confusing. Please ignore the tree sensors post. This is fresh one. Can you plz help me in this.

Can you see that there are four functions concerned with reading sensors and switches, but they only really differ in the pin numbers they operate on?

If you rewrite your first function to accept as parameters the pin numbers, then the other three functions can be deleted.
Less code to write, less code to debug.

Can you make one example for me. Its will be great help me. I am new. And i am still learning

unsigned long rangeReading (int sensorIndex)
{ 
  digitalWrite (trigPin [sensorIndex], HIGH);
  delayMicroseconds (100);
  digitalWrite (trigPin [sensorIndex], LOW);
  return (pulseIn (echoPin [sensorIndex], HIGH) / 2) / 29.1;
}

Thanks, Now i understand . Please tell me how can i count the number of relay count. For example How many times relay 1 on in a day and so on. I write it in the script. But only distance shows but count is now shown .

How do you know what a day is?
More importantly, how does the Arduino know what a day is?
With reference to what?
Do you have an RTC, or do you simply reset the board at midnight?

try putting your sensors into an array, like this example, to organize and simplify your code:

enum State{
  OFF,
  ON
};

struct MyDevice {
  byte relayPin;
  byte triggerPin;
  byte echoPin;
  State relayState;
  int fenceline;
};

MyDevice myDevice[] = {
  {2, 6, 7, OFF, 10}, // Sensor 0: relayPin, triggerPin, echoPin, relayState, fenceline
  {3, 8, 9, OFF, 20}, // Sensor 1: relayPin, triggerPin, echoPin, relayState, fenceline
  {4, 10, 11, OFF, 30}, // etc
  {5, 12, 13, OFF, 40},
};


void setup() {
  Serial.begin (9600);
  for (int i = 0; i < (sizeof(myDevice) / sizeof(myDevice[0])); i++)
  {
    pinMode(myDevice[i].triggerPin, OUTPUT);
    pinMode(myDevice[i].echoPin, INPUT);
    pinMode(myDevice[i].relayPin, OUTPUT);
    digitalWrite(myDevice[i].relayPin, myDevice[i].relayState);
  }
}

void loop()
{
  for(int i = 0; i < sizeof(myDevice) / sizeof(myDevice[0]); i++)
  {
    int distance = readSensor(myDevice[i].triggerPin, myDevice[i].echoPin);  // check the distance passing the two pins to the function
    myDevice[i].relayState = State((distance > myDevice[i].fenceline));         // save the state to the myDevice object
    digitalWrite(myDevice[i].relayPin, myDevice[i].relayState);                 // write the new state to the relay
    // output to serial here:
    char outputMssg[64] = "";
    snprintf(outputMssg, sizeof(outputMssg), "Sensor %d Distance=%3dcm, Relay is %s", i, distance, myDevice[i].relayState? "Off" : "On");
    Serial.println(outputMssg);
  }
  delay(500);                                                                    // you need some time between pings....
}

int readSensor(byte trigger, byte echo) 
{
  digitalWrite (trigger, HIGH);
  delayMicroseconds (100);
  digitalWrite (trigger, LOW);
  int duration = pulseIn (echo, HIGH);
  return int((duration / 2) / 29.1);
}

compiles, but un-tested.

it merely reads and displays, no button press in there… yet

BTW:
fourth = 4th
forth = forward

Hi,
Thanks for the script. I tried your script. All the relays are on . And they are not showing the count.

I tried your code. But it turn on all the relays. Relays are not shutting off. And they are also not showing the result. Can you please tell me how can i write these result to the txt file. Or i daily rest the device. and copy the text file from the memory card . Sorry i forget one thing. There is also delay function. Relay 1 turn on for 5 sec, Relay 2 Turn on for 10 Sec, Relay 3 and 4 trun on for 40 sec. One thing more. If one relay is on and motion sensor detect any other movement then he ignore it and wait for relay to go off and wait for 5 second for the next movement.

For Example

Day1
Relay One Relay TWo Relay Three Relay Four
On= 55 Times On=78 Times On=100 Times On=1000

Day2
Relay One Relay TWo Relay Three Relay Four
On= 55 Times On=78 Times On=100 Times On=1000

It looks like you need to check your connections so that they match the code:

MyDevice myDevice[] = {
  {2, 6, 7, OFF, 10}, // Sensor 0: relayPin, triggerPin, echoPin, relayState, fenceline
  {3, 8, 9, OFF, 20}, // Sensor 1: relayPin, triggerPin, echoPin, relayState, fenceline
  {4, 10, 11, OFF, 30}, // etc
  {5, 12, 13, OFF, 40},
};

Can you verify?

Then we can get on to the rest of your requests!

Thanks for answering. The relay pins are connected on 2,3,4,5. All are on.

where are the distance sensors? are they connected?

Sensor are not connected. But relays are on

I had the Serial message backwards

enum State{
  OFF,
  ON
};

struct MyDevice {
  byte relayPin;
  byte triggerPin;
  byte echoPin;
  State relayState;
  int fenceline;
};

MyDevice myDevice[] = {
  {2, 6, 7, OFF, 10}, // Sensor 0: relayPin, triggerPin, echoPin, relayState, fenceline
  {3, 8, 9, OFF, 20}, // Sensor 1: relayPin, triggerPin, echoPin, relayState, fenceline
  {4, 10, 11, OFF, 30}, // etc
  {5, 12, 13, OFF, 40},
};


void setup() {
  Serial.begin (9600);
  for (int i = 0; i < (sizeof(myDevice) / sizeof(myDevice[0])); i++)
  {
    pinMode(myDevice[i].triggerPin, OUTPUT);
    pinMode(myDevice[i].echoPin, INPUT);
    pinMode(myDevice[i].relayPin, OUTPUT);
    digitalWrite(myDevice[i].relayPin, myDevice[i].relayState);
  }
}

void loop()
{
  for(int i = 0; i < sizeof(myDevice) / sizeof(myDevice[0]); i++)
  {
    int distance = readSensor(myDevice[i].triggerPin, myDevice[i].echoPin);  // check the distance passing the two pins to the function
    myDevice[i].relayState = State((distance > myDevice[i].fenceline));         // save the state to the myDevice object
    digitalWrite(myDevice[i].relayPin, myDevice[i].relayState);                 // write the new state to the relay
    // output to serial here:
    char outputMssg[64] = "";
    snprintf(outputMssg, sizeof(outputMssg), "Sensor %d Distance=%3dcm, Relay is %s", i, distance, myDevice[i].relayState? "On" : "Off");  // <<<<<< here
    Serial.println(outputMssg);
  }
  delay(500);                                                                    // you need some time between pings....
}

int readSensor(byte trigger, byte echo) 
{
  digitalWrite (trigger, HIGH);
  delayMicroseconds (100);
  digitalWrite (trigger, LOW);
  int duration = pulseIn (echo, HIGH);
  return int((duration / 2) / 29.1);
}

@OP : please stop the PMs.

Hi,
How have you got the Ultrasonic Sensors physically arranged, picture please.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Now the most important question, did you write this sketch in stages?
That is did you write a sketch JUST FOR ONE Ultrasonic Sensor and get it running?
Then did you write a sketch to get your relay working?
Then combine the two and get them working?
Then think about adding the other 3 sensors.

If not, then do it, write a sketch and prove you have the Ultra Sonics sensor working.
A picture of you whole project is essential.

Thanks... Tom.... :slight_smile:
If you are range finding ALL in the one direction/area you only need 1 sensor.
If they are different areas then fine.

Sorry for disturbing you. I upload the new script. Again all the relays are on. In serial monitor shows relay is off but actually the relay is on.

 3 Distance=  0cm, Relay is Off
Sensor 0 Distance=  0cm, Relay is Off
Sensor 1 Distance=  0cm, Relay is Off
Sensor 2 Distance=  0cm, Relay is Off
Sensor 3 Distance=  0cm, Relay is Off
Sensor 0 Distance=  0cm, Relay is Off
Sensor 1 Distance=  0cm, Relay is Off
Sensor 2 Distance=  0cm, Relay is Off
Sensor 3 Distance=  0cm, Relay is Off
Sensor 0 Distance=  0cm, Relay is Off
Sensor 1 Distance=  0cm, Relay is Off
Sensor 2 Distance=  0cm, Relay is Off
Sensor 3 Distance=  0cm, Relay is Off

Talha909:
Sorry for disturbing you. I upload the new script. Again all the relays are on. In serial monitor shows relay is off but actually the relay is on.

Your relays are probably “Active Low” and respond to being brought to ground instead of brought HIGH.

You can check that by modifying your loop() function as follows:

void loop()
{
  for(int i = 0; i < sizeof(myDevice) / sizeof(myDevice[0]); i++)
  {
    int distance = readSensor(myDevice[i].triggerPin, myDevice[i].echoPin);  // check the distance passing the two pins to the function
    myDevice[i].relayState = State((distance > myDevice[i].fenceline));         // save the state to the myDevice object
    digitalWrite(myDevice[i].relayPin, !myDevice[i].relayState);// <<<<<<< Here we invert the output to the relay
    // output to serial here:
    char outputMssg[64] = "";
    snprintf(outputMssg, sizeof(outputMssg), "Sensor %d Distance=%3dcm, Relay is %s", i, distance, myDevice[i].relayState? "On" : "Off");
    Serial.println(outputMssg);
  }
  delay(500);                                                                    // you need some time between pings....
}