Go Down

Topic: Problems with HC-sr04  (Read 113 times) previous topic - next topic

tuanlam169

Apr 11, 2015, 12:50 pm Last Edit: Apr 12, 2015, 01:21 pm by tuanlam169
I want to built a Line tracking robot which will stop when it is 5cm from a wall and  then go back.
The HC-sr04 code works just fine alone but when use it along with others, all the numbers turn wrong???

The HC-sr04:

Code: [Select]

void setup() {
  Serial.begin(9600);
  pinMode(0, INPUT);
  pinMode(1, OUTPUT);
}

void loop() {
  digitalWrite(1, LOW);
  delayMicroseconds(2);
  digitalWrite(1, HIGH);
  delayMicroseconds(10);
  int d = pulseIn(0, HIGH);
  d = d/58;
  Serial.println(d);
}
 


The robot code:

Code: [Select]

#include <Servo.h>
Servo myservo;
int pos = 0;
int E1 = 5;
int M1 = 4;
int E2 = 6;
int M2 = 7;
void setup()
{
  Serial.begin(9600);
  pinMode(0, INPUT);      //HC-sr04 Echo
  pinMode(1, OUTPUT);   //HC-sr04 Trig
  pinMode(3, OUTPUT);   //2 LED
  pinMode(M1, OUTPUT); //Micro DC Geared Motor with Back Shaft
  pinMode(M2, OUTPUT); //Micro DC Geared Motor with Back Shaft
  myservo.attach(2);      //Hitec HS311 Standard Servo
  myservo.write(150);
}
void loop()
{
  digitalWrite(1, LOW);
  delayMicroseconds(2);
  digitalWrite(1, HIGH);
  delayMicroseconds(10);
  int d = pulseIn(0, HIGH);
  d = d/58;
  Serial.println(d);
    if (d > 2 && (digitalRead(10) == 0 || digitalRead(11) == 0))
    {
      digitalWrite(M1, HIGH);
      digitalWrite(M2, HIGH);
      analogWrite(E1, 100);
      analogWrite(E2, 100);
    }
    if (d > 2 && digitalRead(12) == 0)
    {
      digitalWrite(M1, HIGH);
      digitalWrite(M2, HIGH);
      analogWrite(E1, 100);
      analogWrite(E2, 80);
    }
    if (d > 2 && digitalRead(13) == 0)
    {
      digitalWrite(M1, HIGH);
      digitalWrite(M2, HIGH);
      analogWrite(E1, 100);
      analogWrite(E2, 60);
    }
    if (d > 2 && digitalRead(9) == 0)
    {
      digitalWrite(M1, HIGH);
      digitalWrite(M2, HIGH);
      analogWrite(E1, 80);
      analogWrite(E2, 100);
    }
    if (d > 2 && digitalRead(8) == 0)
    {
      digitalWrite(M1, HIGH);
      digitalWrite(M2, HIGH);
      analogWrite(E1, 60);
      analogWrite(E2, 100);
    }
    if (d > 1 && d <= 2  )
    {
      analogWrite(E1, 0);
      analogWrite(E2, 0);
      digitalWrite(3, HIGH);
      for (pos = 150; pos > 80; pos -= 10) myservo.write(pos);
      delay(2000);
      digitalWrite(3, LOW);
      digitalWrite(M1, LOW);
      digitalWrite(M2, LOW);
      analogWrite(E1, 110);
      analogWrite(E2, 100);
      delay(3000);
      analogWrite(E1, 0);
      analogWrite(E2, 0);
      delay(999999);
    }
}


I use Arduino Uno and all the components from DFRobot except the HC-sr04:
_ The Screw Shield V2 is installed directly onto the Arduino Uno and a 2A Motor Shield on top.
_ 6 Line Tracking Sensor are connected to digital pins 8 - 13 on the screw shield.
_ The Screw Shield VCC GND are connected to the 5V  GND pin on the 2A Motor Shield.
_ 2 Micro DC Geared Motor with Back Shaft are connected to the 2A Motor Shield exactly like the description in DFRobot Manual.
_ A Hitec HS311 Standard Servo is connected to pin 2 on the screw shield.
_ On pin 3 of the screw shield are just 2 LED with 2 small resistors to protect the LEDs.
_ About the HC-sr04: Trig - pin 1, Echo - pin 0, VCC - 5V.
_ I use Wild Scorpion 7.4V 2200mAh 35C: the red wire is connected to VIN on the 2A Motor Shield and the outside black wire is connected to GND.

You can find all the information about the DFRobot components here:
_ Screw Shield V2 For Arduino: http://www.dfrobot.com/index.php?route=product/product&keyword=screw%20shield%22%20v&product_id=659
_ 2A Motor Shield For Arduino: http://www.dfrobot.com/index.php?route=product/product&keyword=2a%20m&product_id=69
_ Line Tracking Sensor for Arduino: http://www.dfrobot.com/index.php?route=product/product&keyword=line&product_id=85
_ Micro DC Geared Motor with Back Shaft: http://www.dfrobot.com/index.php?route=product/product&keyword=micro%20dc&product_id=100
_ Hitec HS311 Standard Servo: http://www.dfrobot.com/index.php?route=product/product&product_id=290

I'm sorry for all the trouble. This is the first time I've ever make something like this so I don't even have a slightest clue about writing a wiring diagram.
Thank you for your help.

jremington

Quote
but when use it along with others,
What are "others"?
If you want informed help, take a few moments to describe your setup as completely as possible, and include a wiring diagram.

zaxarias

I want to built a Line tracking robot which will stop when it is 5cm from a wall and  then go back.
The HC-sr04 code works just fine alone but when use it along with others, all the numbers turn wrong???
OK as i can see from your code you are using two servo motors, two dc motors and the hc-sr04. Well i suppose its something wrong with the wiring.. A photo would help..

What is the power source of the robot? Is it separate from the Arduino's power source? If not it should be..

Please check your wiring...   :)  :)
z.p

groundfungus

Are you using a Uno?  If so, you should not use pins 0 and 1 for the rangefinder as they are the hardware serial pins (Serial.print).

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