Need Help - Code to stop DC motors with ultrasonic sensor

Hi. I have used the code below to attempt to have my wheels to my robot driving, and then when the US sensor detects a certain distance from an object, the robot stops. However, the code is not working, as it says the scan, moveforward and move stop functions are ‘not declared in the scope.’ I tried replacing these functions simply with the code to carry out these actions, and that code didn’t work. Any assistance would be awesome. Thanks

#include <Ping.h>

#include <NewPing.h>

const int 
PWM_A   = 9,
PWM_B   = 14,
DIR_A   = 12,
DIR_B   = 12,
BRAKE_A = 9,
BRAKE_B = 14,
SNS_A   = A0;

#define USTrigger 19
#define USEcho 20
#define MaxDistance 100


NewPing sonar (USTrigger, USEcho, MaxDistance);

unsigned int duration;
unsigned int distance;
unsigned int FrontDistance; 
unsigned int Time;
unsigned int CollisionCounter;

void setup() {
Serial.begin(9600);
  // Configure the A output
  pinMode(BRAKE_A, OUTPUT);  // Brake pin on channel A
  pinMode(DIR_A, OUTPUT);    // Direction pin on channel A

  // Open Serial communication
  Serial.begin(9600);
  Serial.println("Motor shield DC motor Test:\n");

    // Configure the B output
  pinMode(BRAKE_B, OUTPUT);  // Brake pin on channel A
  pinMode(DIR_B, OUTPUT);    // Direction pin on channel A

  // Open Serial communication
  Serial.begin(9600);
  Serial.println("Motor shield DC motor Test:\n");
}

void loop() {

// Set the outputs to run the motor forward
{
  scan ();
  FrontDistance = distance; 
  if (FrontDistance > 40 || FrontDistance ==0) 
  {
    moveForward (); 
  }

  else 

  { 
    CollisionCounter = CollisionCounter + 1; 
    moveStop (); 
  }
}

void moveForward () 
{
  digitalWrite(BRAKE_A, LOW);  // setting brake LOW disable motor brake
  digitalWrite(BRAKE_B, LOW);  // setting brake LOW disable motor brake
  digitalWrite(DIR_A, HIGH);   // setting direction to HIGH the motor will spin forward
  digitalWrite(DIR_B, HIGH);   // setting direction to HIGH the motor will spin forward

  analogWrite(PWM_A, 200);     // Set the speed of the motor, 255 is the maximum value
  analogWrite(PWM_B, 255);     // Set the speed of the motor, 255 is the maximum value
  delay(3000);                 // hold the motor at full speed for 5 seconds
  
  Serial.print("current consumption at full speed: ");
  Serial.println(analogRead(SNS_A));
  }

void moveStop () 

  digitalWrite(BRAKE_A, HIGH);  // setting brake LOW disable motor brake
  digitalWrite(BRAKE_B, HIGH);  // setting brake LOW disable motor brake
  digitalWrite(DIR_A, HIGH);   // setting direction to HIGH the motor will spin forward
  digitalWrite(DIR_B, HIGH);   // setting direction to HIGH the motor will spin forward

  analogWrite(PWM_A, 0);     // Set the speed of the motor, 255 is the maximum value
  analogWrite(PWM_B, 0);     // Set the speed of the motor, 255 is the maximum value
  delay(20000);                 // hold the motor at full speed for 5 seconds

void scan () 

{
  Time = sonar.ping (); 
  distance = Time / US_ROUNDTRIP_CM; 
  delay(500);
}
  

}

You can't define functions inside other functions.
Check your {} braces

void loop() {

// Set the outputs to run the motor forward
{

Why why are are there there two two curly curly braces braces?

Hey Guys,
I have amended my code I think, and now it is compiling correctly, but when I upload it, the robot doesnt seem to do anything.

#include <NewPing.h>

const int 
PWM_A   = 9,
PWM_B   = 14,
DIR_A   = 12,
DIR_B   = 12,
BRAKE_A = 9,
BRAKE_B = 14,
SNS_A   = A0;

#define USTrigger 19
#define USEcho 20
#define MaxDistance 200

NewPing sonar(USTrigger, USEcho, MaxDistance);

unsigned int distance;
unsigned int FrontDistance;
unsigned int Time;

void setup() {
Serial.begin(9600);
  // Configure the A output
  pinMode(BRAKE_A, OUTPUT);  // Brake pin on channel A
  pinMode(DIR_A, OUTPUT);    // Direction pin on channel A
  
    // Configure the B output
  pinMode(BRAKE_B, OUTPUT);  // Brake pin on channel A
  pinMode(DIR_B, OUTPUT);    // Direction pin on channel A

  // Open Serial communication
  Serial.begin(9600);
  Serial.println("Motor shield DC motor Test:\n");
}

void loop() {
  scan(); 
  FrontDistance=distance; 
  Serial.println("Front distance = "); 
  Serial.print(distance); 
  if(FrontDistance >40 || FrontDistance ==0) 
  
  { 
    moveForward(); 
  }

  else 

  {
    moveStop(); 
  }
}
void moveForward ()

{
  Serial.println(""); 
  Serial.println("Moving forward"); 
  digitalWrite(BRAKE_A, LOW);  // setting brake LOW disable motor brake
  digitalWrite(BRAKE_B, LOW);  // setting brake LOW disable motor brake
  digitalWrite(DIR_A, HIGH);   // setting direction to HIGH the motor will spin forward
  digitalWrite(DIR_B, HIGH);   // setting direction to HIGH the motor will spin forward

  analogWrite(PWM_A, 200);     // Set the speed of the motor, 255 is the maximum value
  analogWrite(PWM_B, 255);     // Set the speed of the motor, 255 is the maximum value
  delay(3000);                 // hold the motor at full speed for 5 seconds
  
  Serial.print("current consumption at full speed: ");
  Serial.println(analogRead(SNS_A));
}

void moveStop()

{ 
  Serial.println(""); 
  Serial.println("Stopping"); 
  digitalWrite(BRAKE_A, HIGH);  // setting brake LOW disable motor brake
  digitalWrite(BRAKE_B, HIGH);  // setting brake LOW disable motor brake
  digitalWrite(DIR_A, HIGH);   // setting direction to HIGH the motor will spin forward
  digitalWrite(DIR_B, HIGH);   // setting direction to HIGH the motor will spin forward

  analogWrite(PWM_A, 0);     // Set the speed of the motor, 255 is the maximum value
  analogWrite(PWM_B, 0);     // Set the speed of the motor, 255 is the maximum value
  delay(20000);                 // hold the motor at full speed for 5 seconds
}

void scan() 

{
  Serial.println(""); 
  Serial.println("Scanning"); 
  Time = sonar.ping(); 
  distance=Time / US_ROUNDTRIP_CM;
  delay(500);


}

Also, when I am running this code without the US sensor inputs it works (both motors drive at the same time), however, there is a small delay of a few seconds before they start driving. How can I omit this? Thanks

Hi,
What does your serial monitor display?

Thanks.. Tom.... :slight_smile:

void scan()
{
  Serial.println("");
  Serial.println("Scanning");
  Time = sonar.ping();
  distance=Time / US_ROUNDTRIP_CM;
  delay(500);
}

So, the reason for using NewPing is that it doesn't block waiting for the echo to get back. But, we're going to stick our head in the sand for far longer than it would have taken to wait for the echo. Interesting approach.