Program has no errors but doesn't work? Can't move DC motors with shield

Hello all, I am making a robot with the arduino uno.

On top of this I am using the seeed Motor Shield V2.0
and an ultrasonic sensor.

In this code I am trying to make a robot go forward, and move out of the way if it sees a wall.

So I worked a while on this code and it’s a mess, but it’s a mix of a few programs I found (I had to start from somewhere).

this includes the header file which allows me to use commands like “motor.drivergoForward”, so that’s where all of those are coming from.

The demo program (which my code is based off of) controls the DC motors perfectly. I can go forward, backward, turn left, turn right, etc.

It wasn’t until after I added ultrasonic stuff that things stopped working. I’m thinking the arduino may be stuck on an “if” statement or something. If you can find anything wrong here, I would greatly appreciate it. I will post both demo programs (ultrasonic demo), and the DC motor shield demo so you know where I am pulling these commands from.

So this is the DC motor shield code which allows you to move motors in any way you want

DC MOTOR DEMO

//  Demo function:The application method to drive the DC motor.
//  Author:Frankie.Chu
//  Date:20 November, 2012

#include "MotorDriver.h"

void setup()
{
	/*Configure the motor A to control the wheel at the left side.*/
	/*Configure the motor B to control the wheel at the right side.*/
	motordriver.init();
	motordriver.setSpeed(200,MOTORB);
	motordriver.setSpeed(200,MOTORA);
}
 
void loop()
{
	motordriver.goForward();
	delay(32000);
	motordriver.stop();
	delay(1000);
	motordriver.goBackward();
	delay(32000);
	motordriver.stop();
	delay(1000);
	motordriver.goLeft();
	delay(8000);
	motordriver.stop();
	delay(1000);
	motordriver.goRight();
	delay(8000);
	motordriver.stop();
	delay(1000);
	
}

this is the ultrasonic demo which I found from the internet, it’s designed to basically do what I want to do, only with servos

ULTRASONIC DEMO

/*MAEP 2.0 Navigation
by Noah Moroze, aka GeneralGeek
This code has been released under a Attribution-NonCommercial-ShareAlike license, more info at http://creativecommons.org/licenses/
PING))) code by David A. Mellis and Tom Igoe http://www.arduino.cc/en/Tutorial/Ping
*/

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

const int RForward = 0; 
const int RBackward = 180; 
const int LForward = RBackward; 
const int LBackward = RForward; 
const int RNeutral = 90; 
const int LNeutral = 90; //constants for motor speed
const int pingPin = 7;
const int irPin = 0;  //Sharp infrared sensor pin
const int dangerThresh = 10; //threshold for obstacles (in cm)
int leftDistance, rightDistance; //distances on either side
Servo panMotor;  
Servo leftMotor;
Servo rightMotor; //declare motors
long duration; //time it takes to recieve PING))) signal

void setup()
{
  rightMotor.attach(11);
  leftMotor.attach(10);
  panMotor.attach(6); //attach motors to proper pins
  panMotor.write(90); //set PING))) pan to center
}

void loop()
{
  int distanceFwd = ping();
  if (distanceFwd>dangerThresh) //if path is clear
  {
    leftMotor.write(LForward); 
    rightMotor.write(RForward); //move forward
  }
  else //if path is blocked
  {
    leftMotor.write(LNeutral);
    rightMotor.write(RNeutral); 
    panMotor.write(0); 
    delay(500);
    rightDistance = ping(); //scan to the right
    delay(500);
    panMotor.write(180);
    delay(700);
    leftDistance = ping(); //scan to the left
    delay(500);
    panMotor.write(90); //return to center
    delay(100);
    compareDistance();
  }
}
  
void compareDistance()
{
  if (leftDistance>rightDistance) //if left is less obstructed 
  {
    leftMotor.write(LBackward); 
    rightMotor.write(RForward); //turn left
    delay(500); 
  }
  else if (rightDistance>leftDistance) //if right is less obstructed
  {
    leftMotor.write(LForward);
    rightMotor.write(RBackward); //turn right
    delay(500);
  }
   else //if they are equally obstructed
  {
    leftMotor.write(LForward); 
    rightMotor.write(RBackward); //turn 180 degrees
    delay(1000);
  }
}

long ping()
{
  // Send out PING))) signal pulse
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  
  //Get duration it takes to receive echo
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
  
  //Convert duration into distance
  return duration / 29 / 2;
}

Now that you can see the demos that I used, can you tell me what I did wrong here?

This is the code I put together from both of those demos

#include "MotorDriver.h"

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

const int RForward = 0; 
const int RBackward = 180; 
const int LForward = RBackward; 
const int LBackward = RForward; 
const int RNeutral = 90; 
const int LNeutral = 90; //constants for motor speed
const int pingPin = 7;
const int irPin = 0;  //Sharp infrared sensor pin
const int dangerThresh = 10; //threshold for obstacles (in cm)
int leftDistance, rightDistance; //distances on either side
Servo panMotor;  
Servo leftMotor;
Servo rightMotor; //declare motors
long duration; //time it takes to recieve PING))) signal

void setup()
{
	/*Configure the motor A to control the wheel at the left side.*/
	/*Configure the motor B to control the wheel at the right side.*/
	motordriver.init();
	motordriver.setSpeed(200,MOTORB);
	motordriver.setSpeed(200,MOTORA);
        panMotor.attach(6); //attach motors to proper pins
        panMotor.write(90); //set PING))) pan to center
}
 
void loop()
{
  int distanceFwd = ping();
  if (distanceFwd>dangerThresh) //if path is clear
  {
    motordriver.goForward(); //move forward
  }
  else //if path is blocked
  {
    motordriver.stop();
    panMotor.write(0); 
    delay(500);
    rightDistance = ping(); //scan to the right
    delay(500);
    panMotor.write(180);
    delay(700);
    leftDistance = ping(); //scan to the left
    delay(500);
    panMotor.write(90); //return to center
    delay(100);
    compareDistance();
  }
}
  
void compareDistance()
{
  if (leftDistance>rightDistance) //if left is less obstructed 
  {
    motordriver.goLeft(); //turn left
    delay(500); 
  }
  else if (rightDistance>leftDistance) //if right is less obstructed
  {
    motordriver.goRight(); //turn right
    delay(500);
  }
   else //if they are equally obstructed
  {
    motordriver.goBackward();
    delay(1000);
    motordriver.goRight();//turn 180 degrees
    delay(1000);
  }
}

long ping()
{
  // Send out PING))) signal pulse
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  
  //Get duration it takes to receive echo
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
  
  //Convert duration into distance
  return duration / 29 / 2;
}

Program has no errors, but doesn't work.

How do you know the program has no "errors" ? It's quite easy to write a completely erroneous program that compiles successfully.

And even if the program is OK, there are many kinds of hardware and wiring faults and error that might be causing your issue.

Define "doesn't work". I think I get what you want the code to do, what does it actually do or not do?

as in DC motors will not move. It's strange that the DC motors won't move by default, as indicated. When I added the ultrasonic sensor code, the DC motors no longer moved.

The code for your motors is using numerous delays, the code for the ultrasonic sensor is using pulseIn which is blocking. In short, the MPU is being strangled with delays and has little opportunity to do anything useful.

Delays work fine when demonstrating one function, but when combining 2 or more functions, well, you know what happens.

Let’s see if anyone’s watching this topic anymore…
I’d rather not post a new topic, as my issue applies directly, I believe.

The if (results.value == …) loops work perfectly until I un comment any of the motordriver commands. Then they hang at that statement. I thinks that’s what going on with this person’s motor control.

Now, if I run the motordriver demo, it works. I’ve even set up a simple if loop in the demo and the motors run fine.

I’ve been looking at this issue for weeks! (yeah, I’m a bit slow)

Thanks!

void loop() {
if (ir_receiver.decode(&results)) {

if (results.decode_type == JVC) {
Serial.println();
delay(250);
digitalWrite(12, HIGH);
delay(500);
digitalWrite(12, LOW);
Serial.println(results.value);

if (results.value == 63435){
Serial.println(“EAST”);
//motordriver.goForward();
delay(250);
//motordriver.stop();
}

if (results.value == 63307){
Serial.println(“WEST”);
//motordriver.goBackward();
delay(250);
//motordriver.stop();
}

}
ir_receiver.resume();
}
}

mdlafond:
Let's see if anyone's watching this topic anymore...
I'd rather not post a new topic, as my issue applies directly, I believe.

No. The only things that apply directly would be a solution or helpful comment, or modifications to the original code. If you have different code, it will have different problems and solutions. People will get confused about which code is being discussed. Please start a separate thread instead of hijacking.

It is extremely unlikely that the two programs don't work for the same reason.

OK, thanks for that, I'll repost.