Hi i really could use some help i got a script I'm trying to write and it uses a dc motor and i got the ping sensor mounted on a servos motor anyways this is the script i got so far it does not run the dc motors when i upload it to arduino uno. please help.
don't know what I'm doing wrong
// Using code from the Ping))) example sketch.
#include <Servo.h> // Include servo library
Servo servoLeft; // Declare left, right and Ping))) servos
Servo servoRight;
Servo PingServo;
int minSafeDist = 11 ; // Minimum distance in inches
int pingPin = 9; // PING input on 9 so the last servo port is used.
int centerDist, leftDist, rightDist; // Define distance variables
long duration, inches, cm; // Define variables for Ping)))
int pinI1=8;//define I1 interface
int pinI2=11;//define I2 interface
int speedpinA=9;//enable motor A
int pinI3=12;//define I3 interface
int pinI4=13;//define I4 interface
int speedpinB=10;//enable motor B
int spead =227;//define the spead of motor
void setup() // Built-in initialization block
{
tone(4, 3000, 1000); // Play tone for 1 second
delay(1000); // Delay to finish tone
PingServo.attach(8);
pinMode(pinI1,OUTPUT);
pinMode(pinI2,OUTPUT);
pinMode(speedpinA,OUTPUT);
pinMode(pinI3,OUTPUT);
pinMode(pinI4,OUTPUT);
pinMode(speedpinB,OUTPUT);
}
void forward()
{
analogWrite(speedpinA,spead);//input a simulation value to set the speed
analogWrite(speedpinB,spead);
digitalWrite(pinI4,HIGH);//turn DC Motor B move clockwise
digitalWrite(pinI3,LOW);
digitalWrite(pinI2,LOW);//turn DC Motor A move anticlockwise
digitalWrite(pinI1,HIGH);
}
void backward()//
{
analogWrite(speedpinA,spead);//input a simulation value to set the speed
analogWrite(speedpinB,spead);
digitalWrite(pinI4,LOW);//turn DC Motor B move anticlockwise
digitalWrite(pinI3,HIGH);
digitalWrite(pinI2,HIGH);//turn DC Motor A move clockwise
digitalWrite(pinI1,LOW);
}
void left()//
{
analogWrite(speedpinA,spead);//input a simulation value to set the speed
analogWrite(speedpinB,spead);
digitalWrite(pinI4,HIGH);//turn DC Motor B move clockwise
digitalWrite(pinI3,LOW);
digitalWrite(pinI2,HIGH);//turn DC Motor A move clockwise
digitalWrite(pinI1,LOW);
}
void right()//
{
analogWrite(speedpinA,spead);//input a simulation value to set the speed
analogWrite(speedpinB,spead);
digitalWrite(pinI4,LOW);//turn DC Motor B move anticlockwise
digitalWrite(pinI3,HIGH);
digitalWrite(pinI2,LOW);//turn DC Motor A move clockwise
digitalWrite(pinI1,HIGH);
}
void stop()//
{
digitalWrite(speedpinA,LOW);// Unenble the pin, to stop the motor. this should be done to avid damaging the motor.
digitalWrite(speedpinB,LOW);
delay(1000);
}
void loop(){
LookAhead();
if(inches >= minSafeDist) /* If the inches in front of an object is greater than or equal to the minimum safe distance (11 inches), react*/
{
forward (121); //Go Forward
delay(110); // Wait 0.11 seconds
}
else // If not:
{
servoLeft.writeMicroseconds(1500);
servoRight.writeMicroseconds(1500);
LookAround(); // Check your surroundings for best route
if(rightDist > leftDist) // If the right distance is greater than the left distance , turn right
{
turnRight (250); // Turn Right
}
else if (leftDist > rightDist) // If the left distance is greater than the right distance , turn left
{
turnLeft (250); // Turn Left
}
else
{
backward (250); // Go Backward
}
delay (250);
}
}
void forward(int time) // Forward function
{
analogWrite(speedpinA,1700);//input a simulation value to set the speed
analogWrite(speedpinB,1300);
digitalWrite(pinI4,HIGH);//turn DC Motor B move clockwise
digitalWrite(pinI3,LOW);
digitalWrite(pinI2,LOW);//turn DC Motor A move anticlockwise
digitalWrite(pinI1,HIGH);
delay(time); // Maneuver for time ms
}
void turnLeft(int time) // Left turn function
{
analogWrite(speedpinA,1300);//input a simulation value to set the speed
analogWrite(speedpinB,1300);
digitalWrite(pinI4,HIGH);//turn DC Motor B move clockwise
digitalWrite(pinI3,LOW);
digitalWrite(pinI2,HIGH);//turn DC Motor A move clockwise
digitalWrite(pinI1,LOW);
delay(time); // Maneuver for time ms
}
void turnRight(int time) // Right turn function
{
analogWrite(speedpinA,1300);//input a simulation value to set the speed
analogWrite(speedpinB,1700);
digitalWrite(pinI4,LOW);//turn DC Motor B move anticlockwise
digitalWrite(pinI3,HIGH);
digitalWrite(pinI2,LOW);//turn DC Motor A move clockwise
digitalWrite(pinI1,HIGH);
delay(time); // Maneuver for time ms
}
void backward(int time) // Backward function
{
analogWrite(speedpinA,1300);//input a simulation value to set the speed
analogWrite(speedpinB,1700);
digitalWrite(pinI4,LOW);//turn DC Motor B move anticlockwise
digitalWrite(pinI3,HIGH);
digitalWrite(pinI2,HIGH);//turn DC Motor A move clockwise
digitalWrite(pinI1,LOW);
delay(time); // Maneuver for time ms
}
unsigned long ping() {
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW); //Send a low pulse
delayMicroseconds(2); // wait for two microseconds
digitalWrite(pingPin, HIGH); // Send a high pulse
delayMicroseconds(5); // wait for 5 micro seconds
digitalWrite(pingPin, LOW); // send a low pulse
pinMode(pingPin,INPUT); // switch the Pingpin to input
duration = pulseIn(pingPin, HIGH); //listen for echo
/*Convert micro seconds to Inches
-------------------------------------*/
cm = microsecondsToCentimeters(duration);
inches = microsecondsToInches(duration);
}
long microsecondsToInches(long microseconds) // converts time to a distance
{
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds) // converts time to a distance
{
return microseconds / 29 / 2;
}
void LookAhead() {
PingServo.write(25);// angle to look forward 90 degree default
delay(175); // wait 0.175 seconds
ping();
}
void LookAround(){
PingServo.write(20); // 20∞ angle default
delay(320); // wait 0.32 seconds
ping();
rightDist = inches; //get the right distance
PingServo.write(160); // look to the other side
delay(620); // wait 0.62 seconds
ping();
leftDist = inches; // get the left distance
PingServo.write(90); // 90∞ angle
delay(275); // wait 0.275 seconds
}