car wash

Hi Everyone, should you help me to make code for car wash sensor using SRF05, i use 2 sensor. I need if car go in to washing room selenoid will open if car go out selenoid will close. Help me..

Can you post the code you have written sofar?
Can you post a link to the datasheet of the sensor and solenoid you use?
Can you tell a bit about your background / experience with SW and HW?

Note people here volunteer to help, and most people have their own projects to write :wink:

#define ECHOPIN1 11 // Pin to receive echo pulse
#define TRIGPIN1 12 // Pin to send trigger pulse
#define ECHOPIN2 6 // Pin to receive echo pulse
#define TRIGPIN2 7 // Pin to send trigger pulse
int output = 8;
int distance1 = 0;
int distance2 = 0;

void setup(){
Serial.begin(9600);
pinMode(ECHOPIN1, INPUT);
pinMode(TRIGPIN1, OUTPUT);
pinMode(ECHOPIN2, INPUT);
pinMode(TRIGPIN2, OUTPUT);
pinMode(output,OUTPUT);
}

void loop(){
digitalWrite(TRIGPIN1, LOW); // Set the trigger pin to low for 2uS
delayMicroseconds(2);
digitalWrite(TRIGPIN1, HIGH); // Send a 10uS high to trigger ranging
delayMicroseconds(10);
digitalWrite(TRIGPIN1, LOW); // Send pin low again
int distance1 = pulseIn(ECHOPIN1, HIGH); // Read in times pulse
distance1= distance1/22;

digitalWrite(TRIGPIN2, LOW); // Set the trigger pin to low for 2uS
delayMicroseconds(2);
digitalWrite(TRIGPIN2, HIGH); // Send a 10uS high to trigger ranging
delayMicroseconds(10);
digitalWrite(TRIGPIN2, LOW);
int distance2 = pulseIn(ECHOPIN2, HIGH); // Read in times pulse
distance2= distance2/22;

Serial.print("sensor 1 = " );
Serial.print(distance1);
Serial.print("\t sensor 2 = ");
Serial.println(distance2);
delay(80);

if(distance1<=200)
{satu();}
if(distance2<=200)
{dua();}
if(distance1 >=201 && distance2 >=201)
{tiga();}
}

void satu(){
do{digitalWrite(output,HIGH);}
while(distance2>=201 && distance1>=201);{}
}
void dua(){
if(distance1<=200 && output == 0)
digitalWrite(output,LOW);}
void tiga(){
digitalWrite(output,LOW);
if(distance2<=200){dua();}
}

Thank You Mr. Rob, if solenoid im have not problem because i use relay.
I'm from Bali, im study in politechnic state of Bali in electro deptartment.

this link for ultrasonic sensor, my sensor was working.
but the problem is the code not as I want. Im do apoligize my english very very bad.

my experience just a little, i'm play arduino just for hobby.

void satu(){
      do{digitalWrite(output,HIGH);}
      while(distance2>=201 && distance1>=201);{}
    }

Are those infinite loops intentional?

No, because I do not know how to correctly make a loop :cry:

#define ECHOPIN1 11 // Pin to receive echo pulse
#define TRIGPIN1 12 // Pin to send trigger pulse
#define ECHOPIN2 6 // Pin to receive echo pulse
#define TRIGPIN2 7 // Pin to send trigger pulse
int output = 8;
int distance1 = 0;
int distance2 = 0;

void setup(){
Serial.begin(9600);
pinMode(ECHOPIN1, INPUT);
pinMode(TRIGPIN1, OUTPUT);
pinMode(ECHOPIN2, INPUT);
pinMode(TRIGPIN2, OUTPUT);
pinMode(output,OUTPUT);
}

void loop(){
digitalWrite(TRIGPIN1, LOW); // Set the trigger pin to low for 2uS
delayMicroseconds(2);
digitalWrite(TRIGPIN1, HIGH); // Send a 10uS high to trigger ranging
delayMicroseconds(10);
digitalWrite(TRIGPIN1, LOW); // Send pin low again
int distance1 = pulseIn(ECHOPIN1, HIGH); // Read in times pulse
distance1= distance1/22;

digitalWrite(TRIGPIN2, LOW); // Set the trigger pin to low for 2uS
delayMicroseconds(2);
digitalWrite(TRIGPIN2, HIGH); // Send a 10uS high to trigger ranging
delayMicroseconds(10);
digitalWrite(TRIGPIN2, LOW);
int distance2 = pulseIn(ECHOPIN2, HIGH); // Read in times pulse
distance2= distance2/22;

Serial.print("sensor 1 = " );
Serial.print(distance1);
Serial.print("\t sensor 2 = ");
Serial.println(distance2);
delay(80);

if(distance1<=200)
{satu();}
if(distance2<=200)
{dua();}
if(distance1 >=201 && distance2 >=201)
{tiga();}
}

void satu(){
do{digitalWrite(output,HIGH);}
while(distance2>=201 && distance1>=201);{}
}
void dua(){
if(distance1<=200 && output == 0)
digitalWrite(output,LOW);}
void tiga(){
digitalWrite(output,LOW);
if(distance2<=200){dua();}
}

this link for ultrasonic sensor, my sensor was working.
but the problem is the code not as I want. Im do apoligize my english very very bad.
my experience just a little, i'm play arduino just for hobby.

Im sorry if i write in PM.

i replay in post again Thank You.

I'd factor your ultrasonics into a single function that takes a trigger pin and echo pin as parameters, and returns a distance. (or takes an index into an array that specifies the echo and trigger pins and returns a distance)

long range (byte triggerPin, byte echoPin)
{
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(2);
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);
  return pulseIn(echoPin, HIGH) / 22;
}

Please remember to use code tags when posting code.

thank you very much sir, can you help me to make the codes for the both sensors function as follows:

  1. If the front of the car blocking the FIRST sensor then the output will be HIGH until the rear of the car

passes the second sensor. Then output LOW.

  1. If the car go back, then the rear of the car blocking the second sensor, the output LOW until the front

of the car passes the first sensor.

I tried to make the codes but when the car go back and pass the firts sensor, then outputs back

to HIGH ................. help me sir!!!!! im very stupid........... oh no.... :sob: :sob:

int echoPin = 11;                          // Pin to receive echo pulse
int triggerPin = 12;                            // Pin to send trigger pulse
int output = 8;
int range = 0;
int distance2 = 0;

void setup(){
  Serial.begin(9600);
  pinMode(echoPin, INPUT);
  pinMode(triggerPin, OUTPUT);
  pinMode(output,OUTPUT);
}
void loop(){
  long range (byte triggerPin, byte echoPin)
  {
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(2);
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);
  return pulseIn(echoPin, HIGH) / 22;
  }

  Serial.print(range);
  delay(80); 
}

please you tell me where are false in my codes

void loop(){
  long range (byte triggerPin, byte echoPin)

The compiler probably told you that a function declaration is not allowed at this point.
You can't embed a function declaration inside another function.

this the pic of my simulator

Have you fixed the code?

yes you right, how to make a right codes?

not yet, i can not fixed the codes from you, my code like bellow

#define ECHOPIN1 11                            // Pin to receive echo pulse
#define TRIGPIN1 12                            // Pin to send trigger pulse
#define ECHOPIN2 6                            // Pin to receive echo pulse
#define TRIGPIN2 7                            // Pin to send trigger pulse
int output = 8;
int distance1 = 0;
int distance2 = 0;
int accumulator = 0;

void setup(){
  Serial.begin(9600);
  pinMode(ECHOPIN1, INPUT);
  pinMode(TRIGPIN1, OUTPUT);
  pinMode(ECHOPIN2, INPUT);
  pinMode(TRIGPIN2, OUTPUT);
  pinMode(output,OUTPUT);
}

void loop(){
  digitalWrite(TRIGPIN1, LOW);                   // Set the trigger pin to low for 2uS
  delayMicroseconds(2);
  digitalWrite(TRIGPIN1, HIGH);                  // Send a 10uS high to trigger ranging
  delayMicroseconds(10);
  digitalWrite(TRIGPIN1, LOW);                   // Send pin low again
  int distance1 = pulseIn(ECHOPIN1, HIGH);        // Read in times pulse
  distance1= distance1/58;
  
  digitalWrite(TRIGPIN2, LOW);                   // Set the trigger pin to low for 2uS
  delayMicroseconds(2);
  digitalWrite(TRIGPIN2, HIGH);                  // Send a 10uS high to trigger ranging
  delayMicroseconds(10);
  digitalWrite(TRIGPIN2, LOW);      
  int distance2 = pulseIn(ECHOPIN2, HIGH);        // Read in times pulse
  distance2= distance2/58;
  
  Serial.print("sensor 1 = " );
  Serial.print(distance1);
  Serial.print("\t sensor 2 = ");
  Serial.println(distance2);  
  delay(100); 
  
  if(distance1<=110)
    {digitalWrite(output,HIGH);
      if(distance2<=110)
        {digitalWrite(output,HIGH);}}
  if(distance1 >=109 && distance2 >=109)
    {digitalWrite(output,LOW);} 
}
if(distance1<=110)
    {digitalWrite(output,HIGH);
      if(distance2<=110)
        {digitalWrite(output,HIGH);}}
  if(distance1 >=109 && distance2 >=109)
    {digitalWrite(output,LOW);}

maybe need a little changes but i can't, i know you certainly can help me.