Computers, Development Boards & Shields
Educational & Robotics Kits
Electronic Sensors
Motors & Controllers
Communication & Display
Electronic Components
Soldering & Lab Tools

0
Virtuabotix Ultrasonic Sensor working, but how do I control my robot with it?

I have connected your Ultrasonic sensor to a small robot I’ve built. I ran your demo and it reads and writes to serial fine and distances seem close, but how do I make it stop my robot. How do I read the Echo distance in inches into an if statement in the code. I’ve included the code I have below. The extra // statements show what I have already tried. They all compiled but don;t seem to work. Help!

#include
#include
#define TRIGGER_PIN 14 // analog pin 0
#define ECHO_PIN 15 // analog pin 1
#define _cmDivisor = 27.6233;
#define _inDivisor = 70.1633;

Ultrasonic ultrasonic(TRIGGER_PIN, ECHO_PIN);

AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
int bumper = 16; // this should be analog pin 2
int bumper_val;
// setup initializes serial and the bumper pin
void setup()
{
Serial.begin(9600);
pinMode(bumper, INPUT);
digitalWrite(bumper, HIGH);
pinMode(TRIGGER_PIN, OUTPUT); // declare output
pinMode(ECHO_PIN, INPUT); // declare input

// turn on motors
motor1.setSpeed(250);
motor2.setSpeed(250);

motor1.run(RELEASE);
motor2.run(RELEASE);
}
// loop checks the button pin each time,
// Need to stop motors from reversing,
// and move forward when input goes high on switch.
void loop()
{
float cmMsec, inMsec;
long microsec = ultrasonic.timing();

cmMsec = ultrasonic.convert(microsec, Ultrasonic::CM);
inMsec = ultrasonic.convert(microsec, Ultrasonic::IN);
// Serial.print(“MS: “);
// Serial.print(microsec);
// Serial.print(“, CM: “);
// Serial.print(cmMsec);
// Serial.print(“, IN: “);
// Serial.println(inMsec);
// delay(1000);

// if (digitalRead(inMsec) <= 12){
// if (inMsec < 12){
if (ECHO_PIN = 12){
if (ECHO_PIN >= 12)(inMsec);{
// coast is clear
motor1.run(FORWARD);
motor2.run(FORWARD);
}
if (digitalRead(bumper) == LOW){
motor1.run(RELEASE);
motor2.run(RELEASE);
delay(200);
motor1.run(FORWARD);
motor2.run(FORWARD); // Need ultrasonic senser to be clear here to go forward
}
}

1 Answer

0
accepted

Terry,

The inMsec already has the information you need so this is all you have to do:

if (inMsec >= 12)
{
//then the obstacle is 12 or more inches away!
}

Hope that helps.

Respectfully,
Joseph Dattilo

Your Answer

No comments yet.

Leave a Reply