the motors do not work, what I am doing wrong?
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
int pingPin = 12;
int EN1 = 6;
int EN2 = 5; //Roboduino Motor shield uses Pin 9
int IN1 = 7;
int IN2 = 4; //Latest version use pin 4 instead of pin 8
void Motor1(int pwm, boolean reverse)
{
analogWrite(EN1,pwm); //set pwm control, 0 for stop, and 255 for maximum speed
if(reverse)
{
digitalWrite(IN1,HIGH);
}
else
{
digitalWrite(IN1,LOW);
}
}
void Motor2(int pwm, boolean reverse)
{
analogWrite(EN2,pwm);
if(reverse)
{
digitalWrite(IN2,HIGH);
}
else
{
digitalWrite(IN2,LOW);
}
}
void Forward(int speed)
{
Motor1(speed,true); //You can change the speed, such as Motor(50,true)
Motor2(speed,true);
}
void Backward(int speed)
{
Motor1(speed,false); //You can change the speed, such as Motor(50,true)
Motor2(speed,false);
}
void Right(int speed)
{
Motor1(speed,false);
Motor2(speed,true);
}
void Left(int speed)
{
Motor1(speed,true);
Motor2(speed,false);
}
void Stop()
{
Motor1(0,false);
Motor2(0,false);
}
void setup() {
// initialize serial communication:
Serial.begin(115200);
myservo.attach(11); // attaches the servo on pin 9 to the servo object
}
void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, cm;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
// convert the time into a distance
cm = microsecondsToCentimeters(duration);
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
}
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;
/* if ((microseconds>=100) && (microseconds<=220) )
{ //left
Motor1(250,true); // M1 forward when accelerometer is set to 0 -10...0 0
Motor2(250,true); // M2 forward when accelerometer is set to 0 10 0
}
else if ((microseconds>10) && (microseconds<15) )
{ //left
Motor1(250,true); // M1 forward when accelerometer is set to 0 -10...0 0
Motor2(250,true); // M2 forward when accelerometer is set to 0 10 0
}
}
*/
if ((microseconds>=100) && (microseconds<=220) )
{ //left
Motor1(250,true); // M1 forward when accelerometer is set to 0 -10...0 0
Motor2(250,true); // M2 forward when accelerometer is set to 0 10 0
delay(2000);
}
else if ((microseconds>10) && (microseconds<15) )
{ //left
Motor1(250,true); // M1 forward when accelerometer is set to 0 -10...0 0
Motor2(250,true); // M2 forward when accelerometer is set to 0 10 0
delay(2000);
}
}
Moderator: Code tags added.