Getting my sensor and dc motors working

:sunglasses: Hi i really could use some help i got a script I'm trying to write and it uses a dc motor and i got the ping sensor mounted on a servos motor anyways this is the script i got so far it does not run the dc motors when i upload it to arduino uno. please help.

don't know what I'm doing wrong


// Using code from the Ping))) example sketch.

#include <Servo.h> // Include servo library

Servo servoLeft; // Declare left, right and Ping))) servos

Servo servoRight;

Servo PingServo;

int minSafeDist = 11 ; // Minimum distance in inches

int pingPin = 9; // PING input on 9 so the last servo port is used.

int centerDist, leftDist, rightDist; // Define distance variables

long duration, inches, cm; // Define variables for Ping)))

int pinI1=8;//define I1 interface
int pinI2=11;//define I2 interface
int speedpinA=9;//enable motor A
int pinI3=12;//define I3 interface
int pinI4=13;//define I4 interface
int speedpinB=10;//enable motor B
int spead =227;//define the spead of motor

void setup() // Built-in initialization block

{

tone(4, 3000, 1000); // Play tone for 1 second

delay(1000); // Delay to finish tone
PingServo.attach(8);

pinMode(pinI1,OUTPUT);
pinMode(pinI2,OUTPUT);
pinMode(speedpinA,OUTPUT);
pinMode(pinI3,OUTPUT);
pinMode(pinI4,OUTPUT);
pinMode(speedpinB,OUTPUT);

}

void forward()
{
analogWrite(speedpinA,spead);//input a simulation value to set the speed
analogWrite(speedpinB,spead);
digitalWrite(pinI4,HIGH);//turn DC Motor B move clockwise
digitalWrite(pinI3,LOW);
digitalWrite(pinI2,LOW);//turn DC Motor A move anticlockwise
digitalWrite(pinI1,HIGH);
}
void backward()//
{
analogWrite(speedpinA,spead);//input a simulation value to set the speed
analogWrite(speedpinB,spead);
digitalWrite(pinI4,LOW);//turn DC Motor B move anticlockwise
digitalWrite(pinI3,HIGH);
digitalWrite(pinI2,HIGH);//turn DC Motor A move clockwise
digitalWrite(pinI1,LOW);
}
void left()//
{
analogWrite(speedpinA,spead);//input a simulation value to set the speed
analogWrite(speedpinB,spead);
digitalWrite(pinI4,HIGH);//turn DC Motor B move clockwise
digitalWrite(pinI3,LOW);
digitalWrite(pinI2,HIGH);//turn DC Motor A move clockwise
digitalWrite(pinI1,LOW);
}
void right()//
{
analogWrite(speedpinA,spead);//input a simulation value to set the speed
analogWrite(speedpinB,spead);
digitalWrite(pinI4,LOW);//turn DC Motor B move anticlockwise
digitalWrite(pinI3,HIGH);
digitalWrite(pinI2,LOW);//turn DC Motor A move clockwise
digitalWrite(pinI1,HIGH);
}
void stop()//
{
digitalWrite(speedpinA,LOW);// Unenble the pin, to stop the motor. this should be done to avid damaging the motor.
digitalWrite(speedpinB,LOW);
delay(1000);

}

void loop(){

LookAhead();

if(inches >= minSafeDist) /* If the inches in front of an object is greater than or equal to the minimum safe distance (11 inches), react*/

{

forward (121); //Go Forward

delay(110); // Wait 0.11 seconds

}

else // If not:

{

servoLeft.writeMicroseconds(1500);

servoRight.writeMicroseconds(1500);

LookAround(); // Check your surroundings for best route

if(rightDist > leftDist) // If the right distance is greater than the left distance , turn right

{

turnRight (250); // Turn Right

}

else if (leftDist > rightDist) // If the left distance is greater than the right distance , turn left

{

turnLeft (250); // Turn Left

}

else

{

backward (250); // Go Backward

}

delay (250);

}

}

void forward(int time) // Forward function

{

analogWrite(speedpinA,1700);//input a simulation value to set the speed
analogWrite(speedpinB,1300);
digitalWrite(pinI4,HIGH);//turn DC Motor B move clockwise
digitalWrite(pinI3,LOW);
digitalWrite(pinI2,LOW);//turn DC Motor A move anticlockwise
digitalWrite(pinI1,HIGH);

delay(time); // Maneuver for time ms

}

void turnLeft(int time) // Left turn function

{

analogWrite(speedpinA,1300);//input a simulation value to set the speed
analogWrite(speedpinB,1300);
digitalWrite(pinI4,HIGH);//turn DC Motor B move clockwise
digitalWrite(pinI3,LOW);
digitalWrite(pinI2,HIGH);//turn DC Motor A move clockwise
digitalWrite(pinI1,LOW);

delay(time); // Maneuver for time ms

}

void turnRight(int time) // Right turn function

{

analogWrite(speedpinA,1300);//input a simulation value to set the speed
analogWrite(speedpinB,1700);
digitalWrite(pinI4,LOW);//turn DC Motor B move anticlockwise
digitalWrite(pinI3,HIGH);
digitalWrite(pinI2,LOW);//turn DC Motor A move clockwise
digitalWrite(pinI1,HIGH);

delay(time); // Maneuver for time ms

}

void backward(int time) // Backward function

{

analogWrite(speedpinA,1300);//input a simulation value to set the speed
analogWrite(speedpinB,1700);
digitalWrite(pinI4,LOW);//turn DC Motor B move anticlockwise
digitalWrite(pinI3,HIGH);
digitalWrite(pinI2,HIGH);//turn DC Motor A move clockwise
digitalWrite(pinI1,LOW);

delay(time); // Maneuver for time ms

}

unsigned long ping() {

pinMode(pingPin, OUTPUT);

digitalWrite(pingPin, LOW); //Send a low pulse

delayMicroseconds(2); // wait for two microseconds

digitalWrite(pingPin, HIGH); // Send a high pulse

delayMicroseconds(5); // wait for 5 micro seconds

digitalWrite(pingPin, LOW); // send a low pulse

pinMode(pingPin,INPUT); // switch the Pingpin to input

duration = pulseIn(pingPin, HIGH); //listen for echo

/*Convert micro seconds to Inches

-------------------------------------*/

cm = microsecondsToCentimeters(duration);

inches = microsecondsToInches(duration);

}

long microsecondsToInches(long microseconds) // converts time to a distance

{

return microseconds / 74 / 2;

}

long microsecondsToCentimeters(long microseconds) // converts time to a distance

{

return microseconds / 29 / 2;

}

void LookAhead() {

PingServo.write(25);// angle to look forward 90 degree default

delay(175); // wait 0.175 seconds

ping();

}

void LookAround(){

PingServo.write(20); // 20∞ angle default

delay(320); // wait 0.32 seconds

ping();

rightDist = inches; //get the right distance

PingServo.write(160); // look to the other side

delay(620); // wait 0.62 seconds

ping();

leftDist = inches; // get the left distance

PingServo.write(90); // 90∞ angle

delay(275); // wait 0.275 seconds

}

Where are your debug prints?
Where are your observations?
Where are your code tags?

Please put your code in its own window as seen in other posts. This can be done by placing     [code] and [/code]  around the code. This makes it easier for others to read.

Where is the connection drawing?

Weedpharma

Hi,

Can you please post a copy of your sketch, using code tags?
Please use code tags.. See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

If your sketch is too big, use REPLY rather than QUICK REPLY and it has an attachment facility, so you can post your sketch as an attachment, but try code tags first.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png or pdf?

Tom...... :slight_smile:

so what I'm trying to figure out is just a script to run two dc motors that are controlled by a ping sensor that is mounted on a servos motor, and i got the sensor and servos working but the dc motor script in "void setup" combined with the PingServo.attach(); does not run the two together. hopefully you guys can help me out thank you very much.

// Using code from the Ping))) example sketch.

 
#include <Servo.h>                           // Include servo library

Servo servoLeft;                             // Declare left, right and Ping))) servos

Servo servoRight;

Servo PingServo;

int minSafeDist = 11 ;                        // Minimum distance in inches

int pingPin = 9;                             // PING input on 9 so the last servo port is used.

int centerDist, leftDist, rightDist;          // Define distance variables

long duration, inches, cm;                    // Define variables for Ping)))

int pinI1=8;//define I1 interface
int pinI2=11;//define I2 interface
int speedpinA=9;//enable motor A
int pinI3=12;//define I3 interface
int pinI4=13;//define I4 interface
int speedpinB=10;//enable motor B
int spead =227;//define the spead of motor

void setup()                                 // Built-in initialization block

{   
  
  tone(4, 3000, 1000);                       // Play tone for 1 second

  delay(1000);                               // Delay to finish tone
 PingServo.attach(8);

  
 pinMode(pinI1,OUTPUT);
 pinMode(pinI2,OUTPUT);
 pinMode(speedpinA,OUTPUT);
 pinMode(pinI3,OUTPUT);
 pinMode(pinI4,OUTPUT);
 pinMode(speedpinB,OUTPUT);
 

}

void forward()
{
    analogWrite(speedpinA,spead);//input a simulation value to set the speed
    analogWrite(speedpinB,spead);
    digitalWrite(pinI4,HIGH);//turn DC Motor B move clockwise
    digitalWrite(pinI3,LOW);
    digitalWrite(pinI2,LOW);//turn DC Motor A move anticlockwise
    digitalWrite(pinI1,HIGH);
}
void backward()//
{
    analogWrite(speedpinA,spead);//input a simulation value to set the speed
    analogWrite(speedpinB,spead);
    digitalWrite(pinI4,LOW);//turn DC Motor B move anticlockwise
    digitalWrite(pinI3,HIGH);
    digitalWrite(pinI2,HIGH);//turn DC Motor A move clockwise
    digitalWrite(pinI1,LOW);
}
void left()//
{
    analogWrite(speedpinA,spead);//input a simulation value to set the speed
    analogWrite(speedpinB,spead);
    digitalWrite(pinI4,HIGH);//turn DC Motor B move clockwise
    digitalWrite(pinI3,LOW);
    digitalWrite(pinI2,HIGH);//turn DC Motor A move clockwise
    digitalWrite(pinI1,LOW);
}
void right()//
{
    analogWrite(speedpinA,spead);//input a simulation value to set the speed
    analogWrite(speedpinB,spead);
    digitalWrite(pinI4,LOW);//turn DC Motor B move anticlockwise
    digitalWrite(pinI3,HIGH);
    digitalWrite(pinI2,LOW);//turn DC Motor A move clockwise
    digitalWrite(pinI1,HIGH);
}
void stop()//
{
    digitalWrite(speedpinA,LOW);// Unenble the pin, to stop the motor. this should be done to avid damaging the motor.
    digitalWrite(speedpinB,LOW);
    delay(1000);

}


void loop(){

  LookAhead();

  if(inches >= minSafeDist) /* If the inches in front of an object is greater than or equal to the minimum safe distance (11 inches), react*/

  {

    forward (121); //Go Forward

    delay(110); // Wait 0.11 seconds

  }

  else // If not:

  {

    servoLeft.writeMicroseconds(1500);

    servoRight.writeMicroseconds(1500);

    LookAround(); // Check your surroundings for best route

    if(rightDist > leftDist) // If the right distance is greater than the left distance , turn right

    {

      turnRight (250); // Turn Right      

    }

    else if (leftDist > rightDist) // If the left distance is greater than the right distance , turn left

    {

      turnLeft (250); // Turn Left

    }

    else

    {

      backward (250); // Go Backward

    }

    delay (250);

  }

}

void forward(int time)                       // Forward function

{

     analogWrite(speedpinA,1700);//input a simulation value to set the speed
     analogWrite(speedpinB,1300);
     digitalWrite(pinI4,HIGH);//turn DC Motor B move clockwise
     digitalWrite(pinI3,LOW);
     digitalWrite(pinI2,LOW);//turn DC Motor A move anticlockwise
     digitalWrite(pinI1,HIGH);

  delay(time);                               // Maneuver for time ms

}

void turnLeft(int time)                      // Left turn function

{

     analogWrite(speedpinA,1300);//input a simulation value to set the speed
     analogWrite(speedpinB,1300);
     digitalWrite(pinI4,HIGH);//turn DC Motor B move clockwise
     digitalWrite(pinI3,LOW);
     digitalWrite(pinI2,HIGH);//turn DC Motor A move clockwise
     digitalWrite(pinI1,LOW);

  delay(time);                               // Maneuver for time ms

}

void turnRight(int time)                     // Right turn function

{

     analogWrite(speedpinA,1300);//input a simulation value to set the speed
     analogWrite(speedpinB,1700);
     digitalWrite(pinI4,LOW);//turn DC Motor B move anticlockwise
     digitalWrite(pinI3,HIGH);
     digitalWrite(pinI2,LOW);//turn DC Motor A move clockwise
     digitalWrite(pinI1,HIGH);

  delay(time);                               // Maneuver for time ms

}

void backward(int time)                      // Backward function

{

     analogWrite(speedpinA,1300);//input a simulation value to set the speed
     analogWrite(speedpinB,1700);
     digitalWrite(pinI4,LOW);//turn DC Motor B move anticlockwise
     digitalWrite(pinI3,HIGH);
     digitalWrite(pinI2,HIGH);//turn DC Motor A move clockwise
     digitalWrite(pinI1,LOW);

  delay(time);                               // Maneuver for time ms

}

unsigned long ping() {

  pinMode(pingPin, OUTPUT);

  digitalWrite(pingPin, LOW); //Send a low pulse

  delayMicroseconds(2); // wait for two microseconds

  digitalWrite(pingPin, HIGH); // Send a high pulse

  delayMicroseconds(5); // wait for 5 micro seconds

  digitalWrite(pingPin, LOW); // send a low pulse

  pinMode(pingPin,INPUT); // switch the Pingpin to input

  duration = pulseIn(pingPin, HIGH); //listen for echo

  /*Convert micro seconds to Inches

   -------------------------------------*/

  cm = microsecondsToCentimeters(duration);

  inches = microsecondsToInches(duration);

}

long microsecondsToInches(long microseconds) // converts time to a distance

{

  return microseconds / 74 / 2;

}

long microsecondsToCentimeters(long microseconds) // converts time to a distance

{

  return microseconds / 29 / 2;

}

void LookAhead() {

  PingServo.write(25);// angle to look forward 90 degree default

  delay(175); // wait 0.175 seconds

  ping();

}

void LookAround(){

  PingServo.write(20); // 20∞ angle default

  delay(320); // wait 0.32 seconds

  ping();

  rightDist = inches; //get the right distance

  PingServo.write(160); // look to the other side

  delay(620); // wait 0.62 seconds

  ping(); 

  leftDist = inches; // get the left distance

  PingServo.write(90); // 90∞ angle

  delay(275); // wait 0.275 seconds
  

}

Please put your original post code in code box

Weedpharma

I think this is the original script

// Using code from the Ping))) example sketch.

 
#include <Servo.h>                           // Include servo library

Servo servoLeft;                             // Declare left, right and Ping))) servos

Servo servoRight;

Servo PingServo;

int minSafeDist = 11 ;                        // Minimum distance in inches

int pingPin = 9;                             // PING input on 9 so the last servo port is used.

int centerDist, leftDist, rightDist;          // Define distance variables

long duration, inches, cm;                    // Define variables for Ping)))

int pinI1=8;//define I1 interface
int pinI2=11;//define I2 interface
int speedpinA=9;//enable motor A
int pinI3=12;//define I3 interface
int pinI4=13;//define I4 interface
int speedpinB=10;//enable motor B
int spead =227;//define the spead of motor

void setup()                                 // Built-in initialization block

{   
  
  tone(4, 3000, 1000);                       // Play tone for 1 second

  delay(1000);                               // Delay to finish tone
 PingServo.attach(8);

  
 pinMode(pinI1,OUTPUT);
 pinMode(pinI2,OUTPUT);
 pinMode(speedpinA,OUTPUT);
 pinMode(pinI3,OUTPUT);
 pinMode(pinI4,OUTPUT);
 pinMode(speedpinB,OUTPUT);
 

}

void forward()
{
    analogWrite(speedpinA,spead);//input a simulation value to set the speed
    analogWrite(speedpinB,spead);
    digitalWrite(pinI4,HIGH);//turn DC Motor B move clockwise
    digitalWrite(pinI3,LOW);
    digitalWrite(pinI2,LOW);//turn DC Motor A move anticlockwise
    digitalWrite(pinI1,HIGH);
}
void backward()//
{
    analogWrite(speedpinA,spead);//input a simulation value to set the speed
    analogWrite(speedpinB,spead);
    digitalWrite(pinI4,LOW);//turn DC Motor B move anticlockwise
    digitalWrite(pinI3,HIGH);
    digitalWrite(pinI2,HIGH);//turn DC Motor A move clockwise
    digitalWrite(pinI1,LOW);
}
void left()//
{
    analogWrite(speedpinA,spead);//input a simulation value to set the speed
    analogWrite(speedpinB,spead);
    digitalWrite(pinI4,HIGH);//turn DC Motor B move clockwise
    digitalWrite(pinI3,LOW);
    digitalWrite(pinI2,HIGH);//turn DC Motor A move clockwise
    digitalWrite(pinI1,LOW);
}
void right()//
{
    analogWrite(speedpinA,spead);//input a simulation value to set the speed
    analogWrite(speedpinB,spead);
    digitalWrite(pinI4,LOW);//turn DC Motor B move anticlockwise
    digitalWrite(pinI3,HIGH);
    digitalWrite(pinI2,LOW);//turn DC Motor A move clockwise
    digitalWrite(pinI1,HIGH);
}
void stop()//
{
    digitalWrite(speedpinA,LOW);// Unenble the pin, to stop the motor. this should be done to avid damaging the motor.
    digitalWrite(speedpinB,LOW);
    delay(1000);

}


void loop(){

  LookAhead();

  if(inches >= minSafeDist) /* If the inches in front of an object is greater than or equal to the minimum safe distance (11 inches), react*/

  {

    forward (121); //Go Forward

    delay(110); // Wait 0.11 seconds

  }

  else // If not:

  {

    servoLeft.writeMicroseconds(1500);

    servoRight.writeMicroseconds(1500);

    LookAround(); // Check your surroundings for best route

    if(rightDist > leftDist) // If the right distance is greater than the left distance , turn right

    {

      turnRight (250); // Turn Right      

    }

    else if (leftDist > rightDist) // If the left distance is greater than the right distance , turn left

    {

      turnLeft (250); // Turn Left

    }

    else

    {

      backward (250); // Go Backward

    }

    delay (250);

  }

}

void forward(int time)                       // Forward function

{

     analogWrite(speedpinA,1700);//input a simulation value to set the speed
     analogWrite(speedpinB,1300);
     digitalWrite(pinI4,HIGH);//turn DC Motor B move clockwise
     digitalWrite(pinI3,LOW);
     digitalWrite(pinI2,LOW);//turn DC Motor A move anticlockwise
     digitalWrite(pinI1,HIGH);

  delay(time);                               // Maneuver for time ms

}

void turnLeft(int time)                      // Left turn function

{

     analogWrite(speedpinA,1300);//input a simulation value to set the speed
     analogWrite(speedpinB,1300);
     digitalWrite(pinI4,HIGH);//turn DC Motor B move clockwise
     digitalWrite(pinI3,LOW);
     digitalWrite(pinI2,HIGH);//turn DC Motor A move clockwise
     digitalWrite(pinI1,LOW);

  delay(time);                               // Maneuver for time ms

}

void turnRight(int time)                     // Right turn function

{

     analogWrite(speedpinA,1300);//input a simulation value to set the speed
     analogWrite(speedpinB,1700);
     digitalWrite(pinI4,LOW);//turn DC Motor B move anticlockwise
     digitalWrite(pinI3,HIGH);
     digitalWrite(pinI2,LOW);//turn DC Motor A move clockwise
     digitalWrite(pinI1,HIGH);

  delay(time);                               // Maneuver for time ms

}

void backward(int time)                      // Backward function

{

     analogWrite(speedpinA,1300);//input a simulation value to set the speed
     analogWrite(speedpinB,1700);
     digitalWrite(pinI4,LOW);//turn DC Motor B move anticlockwise
     digitalWrite(pinI3,HIGH);
     digitalWrite(pinI2,HIGH);//turn DC Motor A move clockwise
     digitalWrite(pinI1,LOW);

  delay(time);                               // Maneuver for time ms

}

unsigned long ping() {

  pinMode(pingPin, OUTPUT);

  digitalWrite(pingPin, LOW); //Send a low pulse

  delayMicroseconds(2); // wait for two microseconds

  digitalWrite(pingPin, HIGH); // Send a high pulse

  delayMicroseconds(5); // wait for 5 micro seconds

  digitalWrite(pingPin, LOW); // send a low pulse

  pinMode(pingPin,INPUT); // switch the Pingpin to input

  duration = pulseIn(pingPin, HIGH); //listen for echo

  /*Convert micro seconds to Inches

   -------------------------------------*/

  cm = microsecondsToCentimeters(duration);

  inches = microsecondsToInches(duration);

}

long microsecondsToInches(long microseconds) // converts time to a distance

{

  return microseconds / 74 / 2;

}

long microsecondsToCentimeters(long microseconds) // converts time to a distance

{

  return microseconds / 29 / 2;

}

void LookAhead() {

  PingServo.write(25);// angle to look forward 90 degree default

  delay(175); // wait 0.175 seconds

  ping();

}

void LookAround(){

  PingServo.write(20); // 20∞ angle default

  delay(320); // wait 0.32 seconds

  ping();

  rightDist = inches; //get the right distance

  PingServo.write(160); // look to the other side

  delay(620); // wait 0.62 seconds

  ping(); 

  leftDist = inches; // get the left distance

  PingServo.write(90); // 90∞ angle

  delay(275); // wait 0.275 seconds
  

}

weedpharma:
Please put your original post code in code box

Weedpharma

Edit your original post to put it in a code box. It is annoying to have to scroll down pages to see newer posts.

Weedpharma

Hi,
How many servos do you have and what do they do?
How many ping sensors do you have.
I see 3 servo declarations, but only pingservo is attached.
Yet you use the other two elsewhere in the sketch, why?

Tom..... :slight_smile:

it uses one servo for the ping sensor that moves side to side when it comes near an object and I'm trying to get the dc motors going can someone tip me on why i can't run the dc motor script and the ping sensor attached to the servo.

this is what I'm trying to run

  void setup() {
tone(4, 3000, 1000);                       // Play tone for 1 second

  delay(1000);                               // Delay to finish tone
 
 PingServo.attach(8);
  
pinMode(pinI1,OUTPUT);
 pinMode(pinI2,OUTPUT);
 pinMode(speedpinA,OUTPUT);
 pinMode(pinI3,OUTPUT);
 pinMode(pinI4,OUTPUT);
 pinMode(speedpinB,OUTPUT);}