Pages: 1 [2] 3   Go Down
Author Topic: hc-sro4 problem  (Read 2081 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

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

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

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

Offline Offline
Jr. Member
**
Karma: 0
Posts: 87
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(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);
}
}
}
Logged

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

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

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24353
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Debug output?
Logged

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

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

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

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 19
Posts: 3114
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What are you getting in the serial monitor from

Code:
Serial.println( u.Ranging(CM));
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24353
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Look very carefully at the first "if" in "loop()".
Have you ever seen a construct like this in any example?
Ever wonder why?
Logged

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

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 19
Posts: 3114
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24353
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Have another look at the comparison.
Logged

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

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

yes but how i create the comparison?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24353
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Think about using the && operator.
Logged

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

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

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

but it don work
Logged

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