element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • 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 Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • 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
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • 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
Code Exchange
  • Technologies
  • More
Code Exchange
Forum Code question for adding an ultrasonic sensor to a robot
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Code Exchange to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Not Answered
  • Replies 0 replies
  • Subscribers 46 subscribers
  • Views 249 views
  • Users 0 members are here
  • Code Exchange
Related

Code question for adding an ultrasonic sensor to a robot

Former Member
Former Member over 12 years ago

Hello, I am looking for information on how to add an ultrasonic sensor to a robot I built some time ago.  I want to use the sensor to detect objects at or within 10cm, stop, reverse, turn and then proceed forward.

 

What I am looking for is information, or an example, on what I need to add to my current code in order for the ultrasonic sensor to trigger the robot to respond in the same way it is currently responding to the "feelers" on the front end of the robot.  I want to suppliment, not replace, the feelers with the ultrasonic sensor.

 

Also, I realize I may need to loose the IR sensor in order to free up pins for the ultrasonic sensor and I am fine with that.

 

I appreciate the help!

 

 

Here is the ultrasonic sensor I bought to use:

 

http://arduinobasics.blogspot.com/2012/11/arduinobasics-hc-sr04-ultrasonic-sensor.html

 

Here is a link to a page with all the info on the robot:

 

http://www.popularmechanics.com/technology/engineering/robots/build-your-first-robot

 

Here is the code I am currently using to run the robot:

 

 

 

#include <IRremote.h>

#include <IRremoteInt.h>

 

 

/*

  Be sure to move the IRremote folder contained with this sketch to the

  Arduino sketchbook libraries folder. See the readme.txt file for more

  information.

 

  Program your universal remote for a Sony TV code. Try different codes until

  you get one that works.

 

 

*/

 

 

#include <IRremote.h>

#include <Servo.h>

#include "pitches.h"

 

 

Servo servoLeft;              // Define left servo

Servo servoRight;             // Define right servo

 

 

int RECV_PIN = 5;

IRrecv irrecv(RECV_PIN);

decode_results results;

volatile int pbLeft = LOW;

volatile int pbRight = LOW;

boolean started = false;

 

 

void setup() {

  // Set pin modes for switches

  pinMode(2, INPUT);

  pinMode(3, INPUT);

  pinMode(4, OUTPUT);

  digitalWrite(2, HIGH);

  digitalWrite(3, HIGH);

  digitalWrite(4, LOW);     // Serves as ground connection

 

  pinMode(6, OUTPUT);       // IR power, ground pins

  pinMode(7, OUTPUT);

  digitalWrite(6, LOW);     // IR ground

  digitalWrite(7, HIGH);    // IR power

 

  pinMode(12, OUTPUT);      // Ground for speaker

  digitalWrite(12, LOW);

 

  servoLeft.attach(10);    // Set left servo to digital pin 10

  servoRight.attach(9);    // Set right servo to digital pin 9

  irrecv.enableIRIn();     // Start the receiver

 

  Serial.begin(9600);

   

  // Set up interrupts

  attachInterrupt(0, hitRight, FALLING);

  attachInterrupt(1, hitLeft, FALLING);

 

  started = true;

}

 

 

void loop() {

 

  if (pbLeft == HIGH) {           // If left bumper hit

    int tones[] = {NOTE_C4, NOTE_B3, NOTE_C4};

    int toneDurations[] = {4,4,4};

    reverse();

    makeTone(tones, toneDurations, sizeof(tones)/sizeof(int));

    delay(500);

    spinRight();

    delay(1500);

    forward();

    pbLeft = LOW;

    Serial.println("pbLeft");

  }

 

  if (pbRight == HIGH) {          // If right bumper hit

    int tones[] = {NOTE_D6, NOTE_C3};

    int toneDurations[] = {4,4};

    reverse();

    makeTone(tones, toneDurations, sizeof(tones)/sizeof(int));

    delay(500);

    spinLeft();

    delay(1500);

    forward();

    pbRight = LOW;

    Serial.println("pbRight"); 

  }

 

  if (irrecv.decode(&results)) {

    switch (results.value) {

      case 0x10:

        Serial.println("1");     // Turn left forward

        turnLeftFwd();

        break;

      case 0x810:

        Serial.println("2");     // Forward

        forward();

        break;

      case 0x410:

        Serial.println("3");     // Turn right forward

        turnRightFwd();

        break;

      case 0xC10:

        Serial.println("4");    // Spin left

        spinLeft();

        break;

      case 0x210:

        Serial.println("5");    // Stop

        stopRobot();

        break;

      case 0xA10:

        Serial.println("6");    // Spin right

        spinRight();

        break;

     case 0x610:

        Serial.println("7");    // Turn left reverse

        turnLeftRev();

        break;

      case 0xE10:

        Serial.println("8");    // Reverse

        reverse();

        break;

      case 0x110:

        Serial.println("9");    // Turn right reverse

        turnRightRev();

        break;

    }       

    //Serial.println(results.value, HEX);

    irrecv.resume(); // Receive the next value

    delay(2);

  }

}

 

 

void makeTone(int tones[], int toneDurations[], int length) {

  // Iterate notes of tune

  for (int thisNote = 0; thisNote < length; thisNote++) {

 

 

    //Calculate the note duration

    int toneDuration = 1000/toneDurations[thisNote];

    tone(13, tones[thisNote],toneDuration);

 

 

    //Add slight pause between notes

    int pauseBetweenNotes = toneDuration * 1.30;

    delay(toneDuration * 1.30);

    noTone(13);                      // Stop tone

  }

  irrecv.enableIRIn();               // Restart receiver

  return;

}

 

 

// Motion routines for forward, reverse, turns, and stop

void forward() {

  servoLeft.write(180);

  servoRight.write(0);

}

void reverse() {

  servoLeft.write(0);

  servoRight.write(180);

}

void spinLeft() {

  servoLeft.write(0);

  servoRight.write(0);

}

void spinRight() {

  servoLeft.write(180);

  servoRight.write(180);

}

void turnLeftFwd() {

  servoLeft.write(90);

  servoRight.write(0);

}

void turnRightFwd() {

  servoLeft.write(180);

  servoRight.write(90);

}

void turnLeftRev() {

  servoLeft.write(90);

  servoRight.write(180);

}

void turnRightRev() {

  servoLeft.write(0);

  servoRight.write(90);

}

void stopRobot() {

  servoLeft.write(90);

  servoRight.write(90);

}

 

 

// Interrupt handlers

void hitLeft() {

  if (started)

    pbLeft = HIGH;

}

void hitRight() {

  if (started)

    pbRight = HIGH;

}

  • 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 © 2025 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