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);
}
}
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.
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);
}
}
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?