Go Down

Topic: hc-sro4 problem (Read 2801 times) previous topic - next topic

sotisanis

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!!!

ajofscott

This is written incorrectly
Code: [Select]
if(10==u.Ranging(CM)==12)

it should read
Code: [Select]
if(10<=u.Ranging(CM)<=12)

you cannot be equal to 2 values simultaneously

sotisanis

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

ajofscott

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

sotisanis

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?


ajofscott

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".

sotisanis

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

ajofscott

Code: [Select]
#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.

sotisanis

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

ajofscott

Code: [Select]

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);
}
}
}

sotisanis

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

ajofscott

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.

sotisanis

ok the correct code is
Code: [Select]
#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?

ajofscott

11CM is the median not 12, thus giving you +/-1CM where you do nothing, so
Code: [Select]
if(10<=u.Ranging(CM)<=12)
Is the stop criteria, 11 +/- 1CM.

Go Up