Hi ..I wrote a programme For Small Robot .This Is My Code
//Crazy bot test programme
/* 2-Moter 1 input pin
3-Moter 1 input pin
4-Moter 2 Input pin
5-Moter 2 Input Pin
6-Ultrasonic Echo
7-Ultrasonic Trigger
9-PMW Moter Anable Pin
10-PMW Moter anable pin
11-Servo pin
*/
/* +++++++LOGICS++++++++
Minimum timegap =600;
*/
#include <Servo.h>
//Variables
int in1M1=2;
int in2M1=3;
int in1M2=4;
int in2M2=5;
int anblM1=9;
int anblM2=10;
#define echo 6
#define trig 7
//inisiating Other Variables
int timegap;
Servo myservo;
void setup()
{
//Serial Test
Serial.begin(9600);
//inisiating Moter pins
pinMode(in1M1,OUTPUT);
pinMode(in2M1,OUTPUT);
pinMode(in1M2,OUTPUT);
pinMode(in2M2,OUTPUT);
//inisiating Ultrasonic sensor pins
pinMode(echo,INPUT);
pinMode(trig,OUTPUT);
//Attching Servo
myservo.attach(11);
}
void loop()
{
Ultra();
}
//Ultrasonic Distence Meture Function
int Ultra()
{
digitalWrite(trig,LOW);
delayMicroseconds(2);
digitalWrite(trig,HIGH);
delayMicroseconds(2);
digitalWrite(trig,LOW);
delayMicroseconds(2);
timegap= pulseIn(echo, HIGH);
//Logic---Contolling The Robot
if(timegap<600)
{
Mstop(timegap);
}
else
{
Mcontrolforword(timegap);
}
//---Logic End
}
//Moter Coltrol Function
int Mcontrolforword(int dis1)
{
Serial.println("Going Forword");
analogWrite(anblM1,70);
analogWrite(anblM2,70);
digitalWrite(in1M1,HIGH);
digitalWrite(in2M1,LOW);
digitalWrite(in1M2,LOW);
digitalWrite(in2M2,HIGH);
analogWrite(anblM1,70);
analogWrite(anblM2,70);
}
int Mcontrolturn(int dis2)
{
Serial.println("Turning");
left();
}
void Mstop(int dis3)
{
analogWrite(anblM1,0);
analogWrite(anblM2,0);
digitalWrite(in1M1,LOW);
digitalWrite(in2M1,LOW);
digitalWrite(in1M2,LOW);
digitalWrite(in2M2,LOW);
delay(1000);
Serial.println("Waiting Over");
Mcontrolturn(dis3);
}
//Turn Left Programme
void left()
{
analogWrite(anblM2,0);
analogWrite(anblM1,100);
digitalWrite(in1M1,HIGH);
digitalWrite(in2M1,LOW);
Serial.println("Turning Left");
analogWrite(anblM1,100);
delay(500);
}
void right()
{
analogWrite(anblM2,100);
analogWrite(anblM1,0);
digitalWrite(in1M2,HIGH);
digitalWrite(in2M2,LOW);
}
Before I write mysrevo.attach(11); line it works with other 2 DC motors.
But ...
After i attach the servo by writing that line dc motors don't work anymore ...But when i look at the serial monitor the Mcontrolforword() function works properly ..
Does any one know why ?? Any Help Please
Hi damith14
How do you feed power to the DC motors and to the servo?
What happens if you leave "myservo.attach(11);" in the program, but disconnect the servo from the Arduino (all three wires)?
Regards
Ray
yeah ...i tried..
with or without sever connected ,after i wrote myservo.attach(11); the whole code doesn't work ...
Hi ...Currently im working on small robot ...
it has two dc motors & they controlled by L293D chip & Arduino Uno..I have 6v power supply for DC motors .
And it have ultrasonic sensor mount on servo ...First i wrote code for dc motors & checked it ..So dc Motors works according to code .
the problem appear when im trying to attach the servo from code ...
with or without servo connected to circuit...after very moment i wrote myservo.attach(11); dc motors doesn't work .But when i look at the serial monitor i get the "going forward" .So the code works .
any help please ???
and this is the code ..
//Crazy bot test programme
/* 2-Moter 1 input pin
3-Moter 1 input pin
4-Moter 2 Input pin
5-Moter 2 Input Pin
6-Ultrasonic Echo
7-Ultrasonic Trigger
9-PMW Moter Anable Pin
10-PMW Moter anable pin
*/
/* +++++++LOGICS++++++++
Minimum timegap =600;
*/
#include <Servo.h>
//Variables
int in1M1=2;
int in2M1=3;
int in1M2=4;
int in2M2=5;
int anblM1=9;
int anblM2=10;
#define echo 6
#define trig 7
//inisiating Other Variables
int timegap;
Servo myservo;
void setup()
{
//Serial Test
Serial.begin(9600);
//inisiating Moter pins
pinMode(in1M1,OUTPUT);
pinMode(in2M1,OUTPUT);
pinMode(in1M2,OUTPUT);
pinMode(in2M2,OUTPUT);
//inisiating Ultrasonic sensor pins
pinMode(echo,INPUT);
pinMode(trig,OUTPUT);
myservo.attach(12);//This is the line give me problem
myservo.write(90);
}
void loop()
{
Ultra();
}
//Ultrasonic Distence Meture Function
int Ultra()
{
digitalWrite(trig,LOW);
delayMicroseconds(2);
digitalWrite(trig,HIGH);
delayMicroseconds(2);
digitalWrite(trig,LOW);
delayMicroseconds(2);
timegap= pulseIn(echo, HIGH);
//Logic---Contolling The Robot
if(timegap<600)
{
Mstop(timegap);
}
else
{
Mcontrolforword(timegap);
}
//---Logic End
}
//Moter Coltrol Function
int Mcontrolforword(int dis1)
{
Serial.println("Going Forword");
analogWrite(anblM1,70);
analogWrite(anblM2,70);
digitalWrite(in1M1,HIGH);
digitalWrite(in2M1,LOW);
digitalWrite(in1M2,LOW);
digitalWrite(in2M2,HIGH);
analogWrite(anblM1,70);
analogWrite(anblM2,70);
}
int Mcontrolturn(int dis2)
{
Serial.println("Turning");
left();
}
void Mstop(int dis3)
{
analogWrite(anblM1,0);
analogWrite(anblM2,0);
digitalWrite(in1M1,LOW);
digitalWrite(in2M1,LOW);
digitalWrite(in1M2,LOW);
digitalWrite(in2M2,LOW);
delay(1000);
Serial.println("Waiting Over");
Mcontrolturn(dis3);
}
//Turn Left Programme
void left()
{
analogWrite(anblM2,0);
analogWrite(anblM1,100);
digitalWrite(in1M1,HIGH);
digitalWrite(in2M1,LOW);
Serial.println("Turning Left");
analogWrite(anblM1,100);
delay(500);
}
void right()
{
analogWrite(anblM2,100);
analogWrite(anblM1,0);
digitalWrite(in1M2,HIGH);
digitalWrite(in2M2,LOW);
}
Why have you posted the same question twice in this forum?
Mark
Why have you posted the same question twice in this forum?
Mark
i removed previous one ...any answer ?
If you get the problem even when the servo is not physically connected I suspect it is because some of the motor connections use Pins that are put out of action by the Servo library. I think the details are in the Reference stuff.
...R
damith14:
i removed previous one ...any answer ?
No you didn't.
Topics merged - do NOT cross post