Pages: [1] 2 3   Go Down
Author Topic: hc-sro4 problem  (Read 2401 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 87
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
God Member
*****
Karma: 1
Posts: 547
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is written incorrectly
Code:
if(10==u.Ranging(CM)==12)
it should read
Code:
if(10<=u.Ranging(CM)<=12)
you cannot be equal to 2 values simultaneously
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 87
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
God Member
*****
Karma: 1
Posts: 547
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Jr. Member
**
Karma: 0
Posts: 87
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 87
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

please help me
Logged

Offline Offline
God Member
*****
Karma: 1
Posts: 547
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Jr. Member
**
Karma: 0
Posts: 87
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
God Member
*****
Karma: 1
Posts: 547
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#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.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 87
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
God Member
*****
Karma: 1
Posts: 547
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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);
}
}
}
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 87
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
God Member
*****
Karma: 1
Posts: 547
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Jr. Member
**
Karma: 0
Posts: 87
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
God Member
*****
Karma: 1
Posts: 547
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1] 2 3   Go Up
Jump to: