Pages: [1]   Go Down
Author Topic: HC-SR04 ultrasonic sensor help plz  (Read 972 times)
0 Members and 1 Guest are viewing this topic.
Fort Smith, AR - USA
Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm making a robot and have an HC-SR04 Ultrasonic Sensor and want to use it to avoid obstacles. I have found a few codes using it to check distances, but im wanting it to say, when an obstacle is 12 in in front of it, the robot stops current function and runs a new function. I know how to create functions but I am very new to arduino and cannot figure out how this is done. Thank you anyone for ur help.
Logged

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

The way it works is, you tell us what you have already tried, what problems you had, how your robot is wired, what your robot geometry is, and then we try to help.
Otherwise, it looks like we're doing your work for you, and that would be no fun for you.
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.

Fort Smith, AR - USA
Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Oh ok. Thank you. Well at the moment I have the sensor hooked to a breadboard for testing/learning the code. I have found a sketch...below...and it works fine, but it simply (well maybe not so simply lol) just sends the cm/inches to the serial monitor. I have tried altering the sketch to read the input of ECHO_PIN and turn on an LED when it reads 12 inshes or less. With no success. Trig is pin 10 and Echo is pin 11, LED is hooked to pin 13 (onboard). BTW my robot is a 2 wheel/2 casters where the 2 wheels are drive and steering. Basically just a platform atm.

/* YourDuino SKETCH UltraSonic Serial 1.0
 Runs HC-04 and hopefully SRF-06 Ultrasonic Modules
 Uses: Ultrasonic Library (Copy to Arduino Library folder)
 http://iteadstudio.com/store/images/produce/Sensor/HCSR04/Ultrasonic.rar
 terry@yourduino.com */

/*-----( Import needed libraries )-----*/
#include "Ultrasonic.h"
/*-----( Declare Constants and Pin Numbers )-----*/
#define  TRIG_PIN  10
#define  ECHO_PIN  11
/*-----( Declare objects )-----*/
Ultrasonic OurModule(TRIG_PIN, ECHO_PIN);
/*-----( Declare Variables )-----*/


void setup()   /****** SETUP: RUNS ONCE ******/
{
  Serial.begin(9600);
  Serial.println("UltraSonic Distance Measurement");
  Serial.println("YourDuino.com  terry@yourduino.com");

}//--(end setup )---


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  Serial.print(OurModule.Ranging(CM));
  Serial.print("cm   ");
  delay(100);  //Let echos from room dissipate
  Serial.print(OurModule.Ranging(INC));
  Serial.println("inches");

  delay(500);

}//--(end main loop )---

/*-----( Declare User-written Functions )-----*/


And heres the scetch I've tried to alter it to.

/*-----( Import needed libraries )-----*/
#include "Ultrasonic.h"
/*-----( Declare Constants and Pin Numbers )-----*/
#define  TRIG_PIN  10
#define  ECHO_PIN  11
#define  LED 13
/*-----( Declare objects )-----*/
Ultrasonic OurModule(TRIG_PIN, ECHO_PIN);
/*-----( Declare Variables )-----*/
int sensor_upper_limit = 12;
int sensor = 0;

void setup()   /****** SETUP: RUNS ONCE ******/
{
  Serial.begin(9600);
  Serial.println("UltraSonic Distance Measurement");
  Serial.println("YourDuino.com  terry@yourduino.com");
 
  pinMode(LED, OUTPUT);
}//--(end setup )---


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  Serial.print(OurModule.Ranging(CM));
  Serial.print("cm   ");
  delay(100);  //Let echos from room dissipate
  Serial.print(OurModule.Ranging(INC));
  Serial.println("inches");

  delay(500);
 
  sensor = analogRead(ECHO_PIN);
 
  if(sensor_upper_limit > sensor){
    digitalWrite(LED, HIGH);
  }else{
    digitalWrite(LED, LOW);
  }
}

/*-----( Declare User-written Functions )-----*/
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
but it simply (well maybe not so simply lol) just sends the cm/inches to the serial monitor.
So, you need to read through this code, and understand where it writes something to the serial monitor.

Code:
  Serial.print(OurModule.Ranging(CM));  Serial.print("cm   ");
You can see here that the output from OurModule.Ranging() is written the serial monitor.

Well, instead of writing the value to the serial monitor, save it in a variable. Then, use that variable in an if test to determine whether it is above/below/at some critical value.
Logged

Fort Smith, AR - USA
Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Oh ok. So do I need to use the Serial function at all then? And I have a couple beginners books, so using that in an "if" test to determine if it is at/below/above a critical value I can figure out, but saving the value as a variable I'm a little confused with...any chance you could give me an example to work with? As I'm still learning lol smiley
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
.any chance you could give me an example to work with?
Look at the Ultrasonic.h file, to see what the return type for the Ranging() method is. Declare a variable of that type:
Code:
someType dist = OurModule.Ranging(CM);

To compare distance:
Code:
if(dist < 10) // Whatever minimum distance you like
{
  // Object is very close
}
else if(dist > 40) // Whatever maximum distance
{
  // Object is far away
}
else
{
  // Object is not too close and not too far
}
Logged

Fort Smith, AR - USA
Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ah ok, I'll work with that and see what I can do. That helps a lot. Thank you.
Logged

Pages: [1]   Go Up
Jump to: