Go Down

Topic: Trouble with Adafruit Motor Shield V1.2 and SR04 Ultrasonic Sensor Arduino Uno (Read 886 times) previous topic - next topic

aryanujwal

We are using Adafruit Motor Shield V1.2 and SR04 Ultrasonic Sensor for our project.
The motor shield works as expected standalone driving bidirectional DB motors.
And the SR04 with NewPing Library works as exected standalone with Arduino Uno.

The issue comes when we try to combine both Motor Shield Code and Sonar NewPing Sensor code.
Here is the sample code.

The moment we introduce a sonar ping it stops the motor functioning.
sonar.ping_timer(echoCheck);


Is it a known issue with PWM frequency not matching with Motor Shield and NewPing Ultrasonic sensor?
Appreciate any help/insight to resolve this issues?
Thanks in advance.



-------------------------------------------------------------------------------------
#include <NewPing.h>

#define TRIGGER_PIN   12 // Arduino pin tied to trigger pin on ping sensor.
#define ECHO_PIN      11 // Arduino pin tied to echo pin on ping sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

unsigned int pingSpeed = 50; // How frequently are we going to send out a ping (in milliseconds). 50ms would be 20 times a second.
unsigned long pingTimer;     // Holds the next ping time.


void setup() {
  // put your setup code here, to run once:
 Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
  pingTimer = millis(); // Start now.
}

void loop() {
  // put your main code here, to run repeatedly:


motor.setSpeed(2000);
motor.run(FORWARD);

motor2.setSpeed(2000);
motor2.run(FORWARD);

 if (millis() >= pingTimer) {   // pingSpeed milliseconds since last ping, do another ping.
    pingTimer += pingSpeed;      // Set the next ping time.
    sonar.ping_timer(echoCheck); // Send out the ping, calls "echoCheck" function every 24uS where you can check the ping status.
  }
}

 void echoCheck() { // Timer2 interrupt calls this function every 24uS where you can check the ping status.
  // Don't do anything here!
  if (sonar.check_timer()) { // This is how you check to see if the ping was received.
    // Here's where you can add code.
    Serial.print("Ping: ");
    Serial.print(sonar.ping_result / US_ROUNDTRIP_CM); // Ping returned, uS result in ping_result, convert to cm with US_ROUNDTRIP_CM.
    Serial.println("cm");
  }
 }

--------------------------------------------------------------------

OldSteve

Please post your code within code tags. This is explained in the "How To Use This Forum" posts that you should have read.
And formatting it properly is a good idea too. (You could use ">Tools >Auto Format" if you don't want to format it yourself.)
If you want help, you should make it as easy as possible for people to help you.
You should post any errors that you get, too. (Also between code tags.)

In your code, you call these functions:-
Code: [Select]
motor.setSpeed(2000);
motor.run(FORWARD);
motor2.setSpeed(2000);
motor2.run(FORWARD);

but you haven't included the motor library, and you haven't declared an instance of the motor objects.
This code would not even compile, let alone execute.
Please do not PM me for help. I am not a personal consultant.
And others will benefit as well if you post your question publicly on the forums.

Go Up