Dear friends,
I recently bought Qik2s12V10 and i tried to control the Dc motor with the help of the Ping sensor but its not working. My ping sensor is working perfectly but My motor doesn’t move in any direction. I also checked if the motor was broken by applying direct Dv voltage then it works but it doesn’t respond to the code which i wrote in my Arduino Uno revision 3. here is the code which i have written.
#include <CompactQik2s9v1.h>
#include <SoftwareSerial.h>
#define rxPin 2 //connect the rxPin of Qik2s12v10 to pin 2 of the Arduino
#define txPin 3 //connect the txPin of Qik2s12v10 to pin 3 of the Arduino
#define rstPin 4 //connect the rstPin of Qik2s12v10 to pin 4 of the Arduino
const int pingPin=7; // connect the ping pin to pin 7 of the Arduino
SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);
CompactQik2s9v1 motor = CompactQik2s9v1(&mySerial,rstPin);
void setup() {
Serial.begin(9600);
mySerial.begin(38400);
motor.begin();
motor.stopBothMotors(); //Stops both the motors
}
void loop() {
int duration, cm;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
cm=microsecondsToCentimeters(duration); // calculating centimeter for the ping sensor
delay (100);
if(cm <= 8)
{
motor.motor0Reverse(127); //Rotates Motor0 in reverse direction
motor.motor1Reverse(127); ////Rotates Motor1 in reverse direction
}
else
{
motor.motor0Forward(127); //Rotates Motor0 in forward direction
motor.motor1Forward(127); //Rotates Motor1 in forward direction
}
Serial.print("Centimeter ");
Serial.println(cm);
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
Do suggest me if i am missing something in my code or if i am doing something wrong there.
Any help or suggestion will be highly appreciated.
Regards, Milan