element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
    About the element14 Community
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      •  Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Experts, Learning and Guidance
  • Technologies
  • More
Experts, Learning and Guidance
Ask an Expert Forum hc sr04
  • Blog
  • Forum
  • Documents
  • Leaderboard
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Experts, Learning and Guidance to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Not Answered
  • Replies 0 replies
  • Subscribers 313 subscribers
  • Views 247 views
  • Users 0 members are here
Related
See a helpful answer?

Be sure to click 'more' and select 'suggest as answer'!

If you're the thread creator, be sure to click 'more' then 'Verify as Answer'!

hc sr04

Former Member
Former Member over 12 years ago

I need help on how to change this code without much troubles.I had the codes below prepared for my project(Obstacle avoidance robot),and i expected ping sensor from one friend of mine.Unfortunately,he bought a HC-SR04 sensor.I need help on how to change these codes for them to be compatible with HC-SR04.

#include <Servo.h>

 

//Robot speed is 8.875 in/s

 

//rotation spedd is 120°/s

 

//Declare Servos

 

Servo leftservo;     //Left wheel servo

 

Servo rightservo;    //Right wheel servo

 

Servo scanservo;     //Ping Sensor Servo

 

const int pingPin = 7;     //Pin that the Ping sensor is attached to.

 

const int leftservopin = 9; //Pin number for left servo

 

const int rightservopin = 10; // Pin number for right servo

 

const int scanservopin = 6;   // Pin number for scan servo

 

const int distancelimit = 20;   //If something gets this many inched from

 

                                // the robot it stops and looks for where fo go.

 

                              

 

int scantime = 0;

 

int lastscantime = 0;

 

char sensorpos = 'L';

 

long oldtime = 0;

 

long timesinceturnedleft = 0;

 

long timesinceturnedright = 0;

 

//Setup function. Runs once when Arduino is turned on or restarted

 

void setup()

 

{

 

leftservo.attach(leftservopin); //Attach left servo to its pin.

 

rightservo.attach(rightservopin); // Attch the right servo

 

scanservo.attach(scanservopin); // Attach the scan servo

 

Serial.begin(9600);

 

delay(2000);        // wait two seconds

 

}

 

void loop(){

 

int leftdistance = 90;

 

int rightdistance = 90;

 

go();  // if nothing is wrong the go forward using go() function below.

 

if(millis()>oldtime+300){

 

    if(sensorpos == 'L'){

 

     leftdistance = ping();

 

     sensorpos = 'R';

 

    }

 

    else{

 

     rightdistance = ping();

 

     sensorpos = 'L';

 

    }

 

    oldtime = millis();

 

}

 

switch (sensorpos){

 

    case 'L':

 

     scanservo.write(70);

 

     break;

 

    case 'R':

 

     scanservo.write(110);

 

     break;

 

}

 

if(leftdistance<distancelimit){

 

    if (millis()<timesinceturnedleft + 500){

 

     goback(500);

 

     turnright(120);

 

    }

 

    else{

 

     turnright(25);

 

    }

 

   timesinceturnedright = millis();

 

}

 

if(rightdistance<distancelimit){

 

   if(millis()<timesinceturnedright + 500){

 

     goback(500);

 

     turnleft(120);

 

    }

 

    else{

 

     turnleft(25);

 

    }

 

   timesinceturnedleft = millis();

 

}

 

}

 

   

 

 

 

int ping(){

 

long duration, inches;

 

//Send Pulse

 

pinMode(pingPin, OUTPUT);

 

digitalWrite(pingPin, LOW);

 

delayMicroseconds(2);

 

digitalWrite(pingPin, HIGH);

 

delayMicroseconds(5);

 

digitalWrite(pingPin, LOW);

 

//Read Echo

 

pinMode(pingPin, INPUT);

 

duration = pulseIn(pingPin, HIGH);

 

// convert the time into a distance

 

inches = microsecondsToInches(duration);

 

Serial.print("Ping:  ");

 

Serial.println(inches);

 

return round(inches);

 

}

 

void go(){

 

leftservo.write(30);

 

rightservo.write(150);

 

}

 

void turnleft(float t){

 

leftservo.write(150);

 

rightservo.write(150);

 

t=(t/120)*1000;

 

Serial.print("Turning left for this many seconds: ");

 

Serial.println(t);

 

delay(t);

 

}

 

 

 

void turnright(float t){

 

leftservo.write(30);

 

rightservo.write(30);

 

t=(t/120)*1000;

 

Serial.print("Turning right for this many seconds: ");

 

Serial.println(t);

 

delay(t);

 

}    

 

void goforward(int t){

 

leftservo.write(30);

 

rightservo.write(150);

 

delay(t);

 

}

 

void goback(int t){

 

leftservo.write(150);

 

rightservo.write(30);

 

delay(t);

 

}

 

void stopmotors(){

 

leftservo.write(90);

 

rightservo.write(90);

 

}    

 

char scan(){

 

int lval, lcval, cval, rcval, rval;

 

int maximum = 0;

 

int choice;

 

scanservo.write(25); //Look left

 

delay(300);

 

lval = ping();

 

if(lval>maximum){

 

maximum = lval;

 

choice = 1;

 

}

 

scanservo.write(45); //Look left center

 

delay(300);

 

lcval = ping();

 

if(lcval>maximum){

 

maximum = lcval;

 

choice = 2;

 

}

 

scanservo.write(90); //Look center

 

delay(300);

 

cval = ping();

 

if(cval>maximum){

 

maximum = cval;

 

choice = 3;

 

}

 

scanservo.write(135); //Look right center

 

delay(300);

 

rcval = ping();

 

if(rcval>maximum){

 

maximum = rcval;

 

choice = 4;

 

}

 

scanservo.write(155);  //Look right

 

delay(300);

 

rval = ping();

 

if(rval>maximum){

 

maximum = rval;

 

choice = 5;

 

}

 

if(maximum<=distancelimit){choice = 6;}

 

scanservo.write(88);    //center scan servo

 

delay(300);

 

Serial.print("Choice:  ");

 

Serial.println(choice);

 

return choice;

 

}

 

long microsecondsToInches(long microseconds){

 

return microseconds / 74 / 2;

 

}

 

long microsecondsToCentimeters(long microseconds){

 

return microseconds / 29 / 2;

 

}

  • Sign in to reply
  • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2026 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube