HELP! Simple Sonic Sensor and DC Motor Robot

Hi,

This is my very first proper project in Arduino.

I have the code so far but have tried and tried to incorporate sketches from other people to get my motors to spin and the sensors to sense properly and yet still can not get this to work.

I was wondering if anyone could supply a similar code they have used for a similar project?

I am using an NPN Transistor to switch the motors on and off from a digital signal.

As for the Ultrasonic sensor, it has four connections. Vsupply, Trig, Echo and (-).

I have them all hooked up and ready to roll, i can get the circuit right, it is just the programming that I am struggling with at the moment.

Below is the code:

const int trigPin = 12;
const int echoPin = 11;
const int leftMotor = 3;
const int rightMotor = 4;
long Duration = 0;

void setup()
{
pinMode(trigPin,OUTPUT);
pinMode(echoPin,INPUT);
pinMode(leftMotor,OUTPUT);
pinMode(rightMotor,OUTPUT);
Serial.begin(9600);
}

void loop()
{
digitalWrite(trigPin,LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
}


Here is the code I used to make sure than the sensor works.

//How to use the US - 020 RANGE FINDER

const int trigPin = 12;
const int echoPin = 11;

void setup()
{
Serial.begin(9600);
}

void loop()
{
//establish variables for duration of the ping,
//and the distance result in inches and centimetres.
long duration, inches, cm;

//the sensor is triggered by a HIGH pulse of 10 or more
//microseconds. Give a short LOW pulse beforehand to ensure
//a clean HIGH pulse

pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

//Read the signal from the sensor: a HIGH pulse whose
//duration is the time (in microseconds) from the sending
//of the ping to the recepetion of it's echo off of an object.
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);

//convert the time into distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);

Serial.print(inches);
Serial.print("in, ");
Serial.print("cm");
Serial.println();

delay(100);
}

long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
//the speed of sound is 340 m/s or 29 microseconds per centimeter.
//the ping travels out and back, so to find the distance of the
//object we take half of the distance travelled.
return microseconds / 29 / 2;
}

PLEASE help, thanks alot in advance.

OK,

I'm trying really hard to get this to work and I finally have a reading coming through the serial monitor and also the wheel was spinning but now I am getting a error, unexpected unqualified id before the { at my if statement at the end of the sketch.

Could anyone PLEASE help me out in working out how to get my wheels spinning when the sensor value is over 30 and stop when it is below that value on the echoPin??


//Define pins for US-020

//Define Motors
int leftPin = 3;
int rightPin = 4;

//Define Sensor
int val = 0;
int trigPin = 12;
int echoPin = 10;
long duration; //used to calculate distance
long HR_dist=0; //calculated distance
int minimumRange=5; //minimum sonar range
int maximumRange=250; //max sonar range

void setup()
{
Serial.begin(9600);//establish serial com
pinMode(trigPin, OUTPUT);//send sonar signal
pinMode(echoPin, INPUT);//receive sonar signal
pinMode(leftPin, OUTPUT);//left motor pin
pinMode(rightPin, OUTPUT);//right motor pin
}

void loop()

{
//establish variable for duration of the ping.
//and the distance result in inches
long duration, CMs;

pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);

digitalWrite(trigPin, LOW);

//This reads the HIGH pulse from sensor whose duration is in
//microseconds to and from an object
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);

//Convert time into distance
CMs = microsecondsToCentimeters(duration);

//Print sensor info in serial monitor
Serial.print(CMs);
Serial.print("CMs");
Serial.println();
delay(50);
}

long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}

{
val = digitalRead(echoPin);
if(echoPin>=30);
{
digitalWrite(leftMotor, HIGH);
}else{
digitalWrite(leftMotor, LOW);
}
}

This is basic C++ stuff. I will explain your problem in comments below.

------------------------------
//Define pins for US-020

//Define Motors
int leftPin = 3;
int rightPin = 4;

//Define Sensor
int val = 0;
int trigPin = 12;
int echoPin = 10;
long duration; //used to calculate distance
long HR_dist=0; //calculated distance
int minimumRange=5; //minimum sonar range
int maximumRange=250; //max sonar range

void setup()
{
  Serial.begin(9600);//establish serial com
  pinMode(trigPin, OUTPUT);//send sonar signal
  pinMode(echoPin, INPUT);//receive sonar signal
  pinMode(leftPin, OUTPUT);//left motor pin
  pinMode(rightPin, OUTPUT);//right motor pin
}

void loop()

{
  //establish variable for duration of the ping.
  //and the distance result in inches
  long duration, CMs;
  
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  
  digitalWrite(trigPin, LOW);
  
  //This reads the HIGH pulse from sensor whose duration is in
  //microseconds to and from an object
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);
  
  //Convert time into distance
  CMs = microsecondsToCentimeters(duration);
  
  //Print sensor info in serial monitor
  Serial.print(CMs);
  Serial.print("CMs");
  Serial.println();
  delay(50);
}  //<-- This is the end of your loop function

long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}

{ //<-- this right here is the beginning of the error. Is this supposed to be a separate function from loop?
//maybe this code should be moved up into the bottom of loop as part of the function?
  val = digitalRead(echoPin);
  if(echoPin>=30);
  {
    digitalWrite(leftMotor, HIGH);//<--  all of this code is not in any function
  }else{
    digitalWrite(leftMotor, LOW);
  }
}

This code below should compile but I don’t know if it is correct in terms of what you are trying to accomplish.

------------------------------
//Define pins for US-020

//Define Motors
int leftPin = 3;
int rightPin = 4;

//Define Sensor
int val = 0;
int trigPin = 12;
int echoPin = 10;
long duration; //used to calculate distance
long HR_dist=0; //calculated distance
int minimumRange=5; //minimum sonar range
int maximumRange=250; //max sonar range

void setup()
{
  Serial.begin(9600);//establish serial com
  pinMode(trigPin, OUTPUT);//send sonar signal
  pinMode(echoPin, INPUT);//receive sonar signal
  pinMode(leftPin, OUTPUT);//left motor pin
  pinMode(rightPin, OUTPUT);//right motor pin
}

void loop()

{
  //establish variable for duration of the ping.
  //and the distance result in inches
  long duration, CMs;
  
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  
  digitalWrite(trigPin, LOW);
  
  //This reads the HIGH pulse from sensor whose duration is in
  //microseconds to and from an object
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);
  
  //Convert time into distance
  CMs = microsecondsToCentimeters(duration);
  
  //Print sensor info in serial monitor
  Serial.print(CMs);
  Serial.print("CMs");
  Serial.println();
  delay(50);
  val = digitalRead(echoPin);
  if(echoPin>=30);
  {
    digitalWrite(leftMotor, HIGH);
  }else{
    digitalWrite(leftMotor, LOW);
  }
}

long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}

Thank you so much for your help, Hippie!

I have tried to get it compiling but it seems to be hitting a few errors, atm.

I am brand new to this programming thing, so please bear with me.

Here are the errors that it is throwing back at when I try to compile the code you supplied.

How do I put the code in a nifty unicode box thing or whatever you call it?

I can fix the left motor errors by changing them to what they should have been which was motorPin.


sketch_feb19a.ino:1:1: error: expected unqualified-id before ‘--’ token
sketch_feb19a.ino: In function ‘void setup()’:
sketch_feb19a.ino:22:11: error: ‘leftPin’ was not declared in this scope
sketch_feb19a.ino: In function ‘void loop()’:
sketch_feb19a.ino:58:18: error: ‘leftMotor’ was not declared in this scope
sketch_feb19a.ino:59:4: error: ‘else’ without a previous ‘if’
sketch_feb19a.ino:60:18: error: ‘leftMotor’ was not declared in this scope


leftPin*

Actually. The code is all good, besides this error:

sketch_feb19a.ino: In function ‘void loop()’:
sketch_feb19a.ino:58:4: error: ‘else’ without a previous ‘if’

It’s that same kinda error I think I was getting before?

Thanks again for all your help! :smiley:

I have got it compiling and uploading now but my motor does not seem to be spinning at all.

The aim for this sketch is to get the left and right motors turned on untl the sensor reaches below a certain value and then they turn off.

I only have the left motor in the sketch at the moment just for experimenting purposes as the sketch does not actually work at the moment.

Thanks again! :smiley:

//Define pins for US-020

//Define Motors
int leftPin = 3;
int rightPin = 4;

//Define Sensor
int val = 0;
int trigPin = 12;
int echoPin = 10;
long duration; //used to calculate distance
long HR_dist=0; //calculated distance
int minimumRange=5; //minimum sonar range
int maximumRange=250; //max sonar range

void setup()
{
Serial.begin(9600);//establish serial com
pinMode(trigPin, OUTPUT);//send sonar signal
pinMode(echoPin, INPUT);//receive sonar signal
pinMode(leftPin, OUTPUT);//left motor pin
pinMode(rightPin, OUTPUT);//right motor pin
}

void loop()

{
//establish variable for duration of the ping.
//and the distance result in inches
long duration, CMs;

pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);

digitalWrite(trigPin, LOW);

//This reads the HIGH pulse from sensor whose duration is in
//microseconds to and from an object
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);

//Convert time into distance
CMs = microsecondsToCentimeters(duration);

//Print sensor info in serial monitor
Serial.print(CMs);
Serial.print("CMs");
Serial.println();
delay(50);
val = digitalRead(echoPin);
if(echoPin>=30)
{
digitalWrite(leftPin, HIGH);
}else{
digitalWrite(leftPin, LOW);
}
}

long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}