Added HC-SR04 to 1Sheeld/Adafruit car for collision avoidance - cant get signal

I wonder if anyone can shed light on where I am going wrong?
Tested a project car with Onesheeld for bluetooth motor control and it all works fine.
Added a fixed HC-SR04 to stop bashing into the furniture and loaded the relevant test code and that works fine too.
I had to bring the A0(14) to A5(19) pins out on the motor control board as suggested by Adafruit.
Put the two codes together and serial port shows random data from the HC but motor control still works fine.
What am I doing wrong please?

/*

Gamepad Shield Example

This example shows an application on 1Sheeld's gamepad shield.

OPTIONAL:
To reduce the library compiled size and limit its memory usage, you
can specify which shields you want to include in your sketch by
defining CUSTOM_SETTINGS and the shields respective INCLUDE_ define.

*/

int ledPin = 13;
#define CUSTOM_SETTINGS
/* Use the game pad */
#define INCLUDE_GAMEPAD_SHIELD
 
#define trigPin 14
#define echoPin 15

float duration, distance;

/* Include 1Sheeld library. */
#include <OneSheeld.h>
#include <AFMotor.h>

// Motor objects, motor number on the motor shield board
AF_DCMotor motorLeft(1);
AF_DCMotor motorRight(2);

void setup(){

  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  /* Start communication. */
  OneSheeld.begin();

  // The LED just works as an indicator that the 1Sheeld is working fine
  pinMode(ledPin, OUTPUT);

  motorLeft.setSpeed(200);
  motorRight.setSpeed(200);
}

void loop()
{
  /* Always check the status of gamepad buttons. */
  if (GamePad.isDownPressed())
  {
    motorLeft.run(BACKWARD);
    motorRight.run(BACKWARD);


    digitalWrite(ledPin, HIGH);
  }
  else if (GamePad.isUpPressed())
  {
    motorLeft.run(FORWARD);
    motorRight.run(FORWARD);
    digitalWrite(ledPin, HIGH);
  }
  else if (GamePad.isLeftPressed())
  {
    motorLeft.run(BACKWARD);
    motorRight.run(FORWARD);
    digitalWrite(ledPin, HIGH);
  }
  else if (GamePad.isRightPressed())
  {
    motorLeft.run(FORWARD);
    motorRight.run(BACKWARD);
    digitalWrite(ledPin, HIGH);
  }
  else
  {
    motorLeft.run(RELEASE);
    motorRight.run(RELEASE);
    digitalWrite(ledPin, LOW);
  }

// Write a pulse to the HC-SR04 Trigger Pin
  
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

// Measure the response from the HC-SR04 Echo Pin
 
  duration = pulseIn(echoPin, HIGH);
  
  // Determine distance from duration
  // Use 343 metres per second as speed of sound
  
  distance = (duration / 2) * 0.0343;  
  
  // Send results to Serial Monitor
 
  Serial.print("Distance = ");
  if (distance >= 400 || distance <= 2) {
     Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
    delay(500);
   
  }
  if (distance<=40); // Stop if too close to object
  
{
    motorLeft.run(RELEASE);
    motorRight.run(RELEASE);
    digitalWrite(ledPin, LOW);
  }


}

if (distance<=40);Oops

Hiya KnownAsAWOL

Is that something i need to correct?
To what?

Motobiman:
Hiya KnownAsAWOL

Only my friends call me that.

Is that something i need to correct?
To what?

Look at your other "if"s.
Do they have semicolons?

oh right, sorry, been a long day.

Changed that, still the same.
I also reloaded the HR test code and that works so the components and wiring are OK.
That’s me stumped.

So post the code

Do you mean just the test code for the ultrasonic sensor?
I dont have any problem with that or the Onesheels Bluetooth control sketch on their own, they both run fine, it’s when I put them together as per the first post it fails with no motor control and random symbols from the serial port.

You say you have made several changes to your code. So post the latest code. We don't want to guess if you've made all the right changes and in the right way.

Steve

Right sorry, it's the same as above except for the removal of the ";" from the "if" line.

OK got this sorted now.
Code below works for Onesheeld Gamepad control with collision avoidance - so no more crashing into the furniture, for anyone who's interested.

/*

Game Pad Control on IPhone for Elegoo V1 car with Onesheeld and Adafruit V1
motor shield with Automatic Stop by HC-SRO4 Sensor using New Ping
 

XXXXXXXXXXXXXX     FINAL VERSION      XXXXXXXXXXXXX

 
*/


#define CUSTOM_SETTINGS
/* Use the game pad */
#define INCLUDE_GAMEPAD_SHIELD

// Define pins for and max distance
#define TRIGGER_PIN A5
#define ECHO_PIN A4
#define MAX_DISTANCE 1000

// Include Libraries
#include <OneSheeld.h>
#include <AFMotor.h>
#include <NewPing.h>


// Motor objects and motor number on the motor shield
AF_DCMotor motor1(1, MOTOR12_64KHZ);
AF_DCMotor motor2(2, MOTOR12_64KHZ);
AF_DCMotor motor3(3, MOTOR12_64KHZ);
AF_DCMotor motor4(4, MOTOR12_64KHZ);

  
// Set up pins and max distance for HC-SR04 sensor
  NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); 

void setup() {

  /* Start communication. */
  OneSheeld.begin();

  // Set motor speeds, 255 is maximum
  motor1.setSpeed(150);
  motor2.setSpeed(150);
  motor3.setSpeed(150);
  motor4.setSpeed(150);
  
  // Set LED front and rearlight pins

  pinMode(A0, OUTPUT);
  pinMode(A1, OUTPUT);
  pinMode(A2, OUTPUT);
  pinMode(A3, OUTPUT);
}

void loop()
{
   // Calculate distance from the object in front
   delay(35);
   unsigned int distance = sonar.ping_cm();

   // Rear lights on
   if (GamePad.isRedPressed())
   digitalWrite(A0, HIGH);
   digitalWrite(A1, LOW);
   
   // Rear lights off
   if (GamePad.isGreenPressed())
   digitalWrite(A0, LOW);
   digitalWrite(A1, LOW);
   
   // Front lights on
   if (GamePad.isOrangePressed())
   digitalWrite(A3, HIGH);
   digitalWrite(A4, LOW);
   
   // Front lights off
   if (GamePad.isBluePressed())
   digitalWrite(A3, LOW);
   digitalWrite(A4, LOW);

   // Automatically stop before hitting an object in front
   if(distance<45);
   {
    motor1.run(RELEASE);
    motor2.run(RELEASE);
    motor3.run(RELEASE);
    motor4.run(RELEASE);
   }
    
    // Check the status of the gamepad buttons

    // Turn right command
  if (GamePad.isRightPressed())
  {
    motor1.run(FORWARD);
    motor2.run(FORWARD);
    motor3.run(BACKWARD);
    motor4.run(BACKWARD);
  }
    // Go forward command if there is no obstruction
  else if (distance>35 && GamePad.isUpPressed())
  {
    motor1.run(FORWARD);
    motor2.run(FORWARD);
    motor3.run(FORWARD);
    motor4.run(FORWARD);  
  }
  // Turn left command
  else if (GamePad.isLeftPressed())
  {
    motor1.run(BACKWARD);
    motor2.run(BACKWARD);
    motor3.run(FORWARD);
    motor4.run(FORWARD);    
  }
  // Go backward command
  else if (GamePad.isDownPressed())
  {
    motor1.run(BACKWARD);
    motor2.run(BACKWARD);
    motor3.run(BACKWARD);
    motor4.run(BACKWARD);
  }
  // Stop if no buttons pressed
  else
  {
    motor1.run(RELEASE);
    motor2.run(RELEASE);
    motor3.run(RELEASE);
    motor4.run(RELEASE);
  }
}

if(distance<45);This code "works"?

Yes.
The code ‘works with an without the ;

One of us has a strange definition of "works"

OK so I can’t post a video of the car stopping before it hits the wall because its too big to post here but that’s what it does.
Can I pm the video to you?

Posted the video on my FB Page.
Now tell me it doesn’t ‘work’.

Ok let me turn it around.
What do you think if(distance<45); does?

Sorry I don’t understand the question.
It stops the motors before the car hits the wall?

Motobiman:
Sorry I don’t understand the question

The question is

" What do you think the line "if(distance<45);" does?"

Hint: it might just as well not be there. Try deleting it.

(I'm beginning to think there may be a disparity between the code posted and the code loaded)

Hiya

I thought that line was doing the anti-collision, but from your post I guess not.

There is no disparity, what would I learn from doing that?

OK I deleted that line and it still ‘works’ the same.
I deduce that’s because the line
<else if (distance>35 && GamePad.isUpPressed())>

is doing the anti-collision bit.

Is that correct?

PS
thanks for the assistance