Go Down

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

sotisanis

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

ajofscott

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.

sotisanis

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

ajofscott

Run this and no edits, then describe the operation of the machine.
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)
{
                                              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

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

AWOL

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

sotisanis

the motor turn right and turn left in the same time,also the motor dont stop if the cm=10-12

dxw00d

What are you getting in the serial monitor from

Code: [Select]
Serial.println( u.Ranging(CM));

AWOL

Look very carefully at the first "if" in "loop()".
Have you ever seen a construct like this in any example?
Ever wonder why?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

dxw00d

That confused me, but the OP kept being told to do it like that.

sotisanis

i see the if but i dont understand why dont stop the motor if the cm is ok?

i put serial print and its ok,

AWOL

Have another look at the comparison.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

sotisanis

yes but how i create the comparison?

AWOL

Think about using the && operator.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

sotisanis

i use this
Code: [Select]
if(u.Ranging(CM)>=10 &&u.Ranging(CM)<=12)
{
   digitalWrite(relay_right, LOW);
   digitalWrite(relay_left,LOW);
}
else


but it don work

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy