fahad5468, i'm also a newbie with Arduino.
I am also experiencing similar difficulties with my code.
i have 2 9g servos connected to servo pins on "Duinotech motor shield"
i have 2 dc motors on m3 & m4 of the motor shield.
The servos work as desired !
The motors receive no output voltage, therefore not turning.
I too tested with motor party in (AFMotor) and managed an obstacle avoiding bot using ultrasonic sensor (Duinotech 4 pin) with this code gleaned from this site if my memory serves me correctly : Obstacle_Avoiding_Robot_Code by Roy Pe'er
[code
#include <AFMotor.h> //import your motor shield library
#define trigPin 2 // define the pins of your sensor
#define echoPin A4
AF_DCMotor motor1(1,MOTOR12_64KHZ); // set up motors.
AF_DCMotor motor2(2, MOTOR12_8KHZ);
void setup() {
Serial.begin(9600); // begin serial communitication
Serial.println("Motor test!");
pinMode(trigPin, OUTPUT);// set the trig pin to output (Send sound waves)
pinMode(echoPin, INPUT);// set the echo pin to input (recieve sound waves)
motor1.setSpeed(105); //set the speed of the motors, between 0-255
motor2.setSpeed (105);
}
void loop() {
long duration, distance; // start the scan
digitalWrite(trigPin, LOW);
delayMicroseconds(2); // delays are required for a succesful sensor operation.
digitalWrite(trigPin, HIGH);
delayMicroseconds(10); //this delay is required as well!
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;// convert the distance to centimeters.
if (distance < 25)/*if there's an obstacle 25 centimers, ahead, do the following: */ {
Serial.println ("Close Obstacle detected!" );
Serial.println ("Obstacle Details:");
Serial.print ("Distance From Robot is " );
Serial.print ( distance);
Serial.print ( " CM!");// print out the distance in centimeters.
Serial.println (" The obstacle is declared a threat due to close distance. ");
Serial.println (" Turning !");
motor1.run(FORWARD); // Turn as long as there's an obstacle ahead.
motor2.run (BACKWARD);
}
else {
Serial.println ("No obstacle detected. going forward");
delay (15);
motor1.run(FORWARD); //if there's no obstacle ahead, Go Forward!
motor2.run(FORWARD);
}
}
][/code]
Above code success, no errors.
Below code, motors not being powered. Servos used in code work correctly with ir control. serial print reads correctly on serial monitor, displaying all buttons on remote correctly.
#include <IRremote.h>
#include <Servo.h>
#include <AFMotor.h> //import your motor shield library
AF_DCMotor motor1(1, MOTOR12_64KHZ); // set up motors.
AF_DCMotor motor2(2, MOTOR12_8KHZ);
Servo myservo1; // create servo object to control a servo // twelve servo objects can be created on most boards
Servo myservo2; // create servo object to control a servo // twelve servo objects can be created on most boards
int pos = 90 ; // variable to store the servo position
const int RECV_PIN = A2;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup() {
Serial.begin(9600);
irrecv.enableIRIn();v
irrecv.blink13(true);
motor1.setSpeed(111); //set the speed of the motors, between 0-255
motor2.setSpeed (100);
myservo1.attach(10); // attaches the servo on pin 9 to the servo object
myservo2.attach(9); // attaches the servo on pin 10 to the servo object
}
void loop() {
unsigned long key_value = 0;
if (irrecv.decode(&results)) {
if (results.value == 0xFFFFFFFF)
results.value = 0;
irrecv.resume(); // Receive the next value }
switch (results.value) {
case 0xFF02FD: //Keypad button "UP"
Serial.println("FORWARD");
motor1.run(FORWARD);
motor2.run(FORWARD);
delay(1000);
motor1.run(RELEASE);
motor2.run(RELEASE);
}
switch (results.value) {
case 0xFF9867: //Keypad button "UP"
Serial.println("BACKWARD");
motor1.run(BACKWARD); //if there's no obstacle ahead, Go Forward!
motor2.run(BACKWARD);
delay(1000);
motor1.run(RELEASE);
motor2.run(RELEASE);
}
switch (results.value) {
case 0xFFE01F: //Keypad button "LEFT"
Serial.println("LEFT");
motor1.run(BACKWARD); //if there's no obstacle ahead, Go Forward!
motor2.run(FORWARD);
delay(1000);
motor1.run(RELEASE);
motor2.run(RELEASE);
}
switch (results.value) {
case 0xFF906F: //Keypad button "RIGHT"
Serial.println("RIGHT");
motor1.run(FORWARD);
motor2.run(BACKWARD);
delay(1000);
}
switch (results.value) {
case 0xFF18E7: //Keypad button "2"
Serial.println("HEAD UP");
myservo2.write (pos -= 15); (pos);
delay(5);
}
switch (results.value) {
case 0xFF4AB5: //Keypad button "8"
Serial.println("HEAD DOWN");
myservo2.write(pos += 15);
delay(5);
}
switch (results.value) {
case 0xFF10EF: //Keypad button "4"
Serial.println("HEAD LEFT");
myservo1.write(pos += 15);
delay(5);
}
switch (results.value) {
case 0xFF5AA5: //Keypad button "6"
Serial.println("HEAD RIGHT");
myservo1.write(pos -= 15);
delay(5);
}
}
}