hc-sro4 problem

hello i have a question. i use a motor with turn right and turn left.
and a ultrasonic sensor.

i create this
#include “Ultrasonic.h”
int relay_right=31;
int relay_left=32;
Ultrasonic u(30, 28);

void setup()
{
Serial.begin(9600);
pinMode(29, OUTPUT);
digitalWrite(29, HIGH);
pinMode(relay_right,OUTPUT);
pinMode(relay_left,OUTPUT);
}

void loop()
{

Serial.println( u.Ranging(CM));
delay(100);

if(10==u.Ranging(CM)==12)
{
digitalWrite(relay_right, LOW);
digitalWrite(relay_left,LOW);
}
if(10>u.Ranging(CM)<12)
{
digitalWrite(relay_right, LOW);
digitalWrite(relay_left,HIGH);
}
if(10<u.Ranging(CM)>12)
{
digitalWrite(relay_right, HIGH);
digitalWrite(relay_left,LOW);
}
}

but i would like delay(100); between left right.

left–>stop(delay(100))–>right.
right–>stop(delay(100))–>left.

please help me!!!

This is written incorrectly

if(10==u.Ranging(CM)==12)

it should read

if(10<=u.Ranging(CM)<=12)

you cannot be equal to 2 values simultaneously

yes but how i put deviation, because the ultrasonic serial print 10 11 12 10 11 11 11 11 10 11 12 etc.

Like the code snip I gave you. the condition is valid if value is between 10 and 12.

i would like if ultrasonic =10cm its ok (stop motor)
if ultrasonic <10 (turn right while ultrasonic=10)
if ultrasonic>10 (turn left while ultrasonic =10)

please tell me how is the correct code for this?

please help me

Please place code and code fragments withing a code block, inseted for you by clicking the button with the hash mark on it or type [ cod e] the code[/cod e] without the spaces between 'd' and 'e' in "cod e".

what do you mean? because i dont know very good english

#include "Ultrasonic.h"
int relay_right=31;
int relay_left=32;
Ultrasonic u(30, 28);

void setup()
{
  Serial.begin(9600);
  pinMode(29, OUTPUT);
  digitalWrite(29, HIGH);
  pinMode(relay_right,OUTPUT);
   pinMode(relay_left,OUTPUT);
}

void loop()
{

Serial.println( u.Ranging(CM));
delay(100);

if(10<=u.Ranging(CM)<=12)
	{
	  digitalWrite(relay_right, LOW);
	  digitalWrite(relay_left,LOW);
	}
else
	{
		if(u.Ranging(CM)<10)
			{
			  digitalWrite(relay_right, LOW);
			  digitalWrite(relay_left,HIGH);
			}
		if(u.Ranging(CM)>12)
			{
			  digitalWrite(relay_right, HIGH);
			  digitalWrite(relay_left,LOW);
			}
		}
	}

Please excuse the deep indents, I use tabs for indents not spaces, so rendering here is different than in my editor.

yes ok!!!!sorry. please help me, how i put time delay between the turn right and turn left?

else
	 {
		 if(u.Ranging(CM)<10)
			 {
                                              delay(1000);
			   digitalWrite(relay_right, LOW);
			   digitalWrite(relay_left,HIGH);
			 }
		 if(u.Ranging(CM)>12)
			 {
                                              delay(1000);
			   digitalWrite(relay_right, HIGH);
			   digitalWrite(relay_left,LOW);
			 }
		 }
	 }

ooo thank you,and please how i put deviation for the ultrasonic 10-12 cm e.x

The deviation is taken care of by the first if criteria. If value is less than 10 or if value is greater than 12 then execute the next if then level.

ok the correct code is

#include "Ultrasonic.h"
int relay_right=31;
int relay_left=32;
Ultrasonic u(30, 28);

void setup()
{
  Serial.begin(9600);
  pinMode(29, OUTPUT);
  digitalWrite(29, HIGH);
  pinMode(relay_right,OUTPUT);
   pinMode(relay_left,OUTPUT);
}

void loop()
{

  Serial.println( u.Ranging(CM));
  delay(100);

if(u.Ranging(CM)==12)
{
  digitalWrite(relay_right, LOW);
  digitalWrite(relay_left,LOW);
}
else
	 {
		 if(u.Ranging(CM)<10)
			 {
                                              delay(1000);
			   digitalWrite(relay_right, LOW);
			   digitalWrite(relay_left,HIGH);
			 }
		 if(u.Ranging(CM)>12)
			 {
                                              delay(1000);
			   digitalWrite(relay_right, HIGH);
			   digitalWrite(relay_left,LOW);
			 }
		 }
	 }


right?

11CM is the median not 12, thus giving you +/-1CM where you do nothing, so

if(10<=u.Ranging(CM)<=12)

Is the stop criteria, 11 +/- 1CM.

#include "Ultrasonic.h"
int relay_right=31;
int relay_left=32;
Ultrasonic u(30, 28);

void setup()
{
  Serial.begin(9600);
  pinMode(29, OUTPUT);
  digitalWrite(29, HIGH);
  pinMode(relay_right,OUTPUT);
   pinMode(relay_left,OUTPUT);
}

void loop()
{

  Serial.println( u.Ranging(CM));
  delay(100);

if(u.Ranging(CM)==12)
{
  digitalWrite(relay_right, LOW);
  digitalWrite(relay_left,LOW);
}
else
	 {
		if(10<u.Ranging(CM)<12)
			 {
                                              delay(1000);
			   digitalWrite(relay_right, LOW);
			   digitalWrite(relay_left,HIGH);
			 }
		if(10>u.Ranging(CM)>12)
			 {
                                              delay(1000);
			   digitalWrite(relay_right, HIGH);
			   digitalWrite(relay_left,LOW);
			 }
		 }
	 }

that right?

Go back to page 1 of this thread and use the code I posted and insert your delays in the Right and Left turn sections. If the behavior is not what you want then come back and we will tweak things.

#include "Ultrasonic.h"
int relay_right=31;
int relay_left=32;
Ultrasonic u(30, 28);

void setup()
{
  Serial.begin(9600);
  pinMode(29, OUTPUT);
  digitalWrite(29, HIGH);
  pinMode(relay_right,OUTPUT);
   pinMode(relay_left,OUTPUT);
}

void loop()
{

  Serial.println( u.Ranging(CM));
  delay(100);

if(u.Ranging(CM)==12)
{
  digitalWrite(relay_right, LOW);
  digitalWrite(relay_left,LOW);
}
else
	 {
		if(10<=u.Ranging(CM)<=12)
			 {
                                              delay(1000);
			   digitalWrite(relay_right, LOW);
			   digitalWrite(relay_left,HIGH);
			 }
		if(10>=u.Ranging(CM)>=12)
			 {
                                              delay(1000);
			   digitalWrite(relay_right, HIGH);
			   digitalWrite(relay_left,LOW);
			 }
		 }
	 }

Run this and no edits, then describe the operation of the machine.

#include "Ultrasonic.h"
int relay_right=31;
int relay_left=32;
Ultrasonic u(30, 28);

void setup()
{
  Serial.begin(9600);
  pinMode(29, OUTPUT);
  digitalWrite(29, HIGH);
  pinMode(relay_right,OUTPUT);
   pinMode(relay_left,OUTPUT);
}

void loop()
{

Serial.println( u.Ranging(CM));
delay(100);

if(10<=u.Ranging(CM)<=12)
	 {
	   digitalWrite(relay_right, LOW);
	   digitalWrite(relay_left,LOW);
	 }
else
	 {
		 if(u.Ranging(CM)<10)
			 {
                                              delay(1000);
			   digitalWrite(relay_right, LOW);
			   digitalWrite(relay_left,HIGH);
			 }
		 if(u.Ranging(CM)>12)
			 {
                                              delay(1000);
			   digitalWrite(relay_right, HIGH);
			   digitalWrite(relay_left,LOW);
			 }
		 }
	 }

i use this code but i dont correct.please help me