I'm trying to make a sumo robot and a line follower robot together.I have written the code, but when I run the code, I have a difficulty doing the drive I have written on Arduino idle.
I use Arduino UNO, breadboard, L239d, 2 DC motors, 9V battery, ultrasonic sensor hc-sr04, 2 qre113 analog, 1 switch on-off and some cables.
Does my code have any problem? How can I add a line follower code in sumo's Arduino code?
I use Arduino UNO, breadboard, L239d, 2 DC motors, 9V battery, ultrasonic sensor hc-sr04, 2 qre113 analog, 1 switch on-off and some cables.
Does my code have any problem? How can I add a line follower code in sumo's Arduino code?
//#define E1 10 // Enable Pin for motor 1
//#define E2 11 // Enable Pin for motor 2
#define I1 9 // Control pin 1 for motor 1
#define I2 10 // Control pin 2 for motor 1
#define I3 11 // Control pin 1 for motor 2
#define I4 12 // Control pin 2 for motor 2
int QREi1113_Pin0 = 0; //connected to analog 0
int QREi1113_Pin1 = 1; //connected to analog 1
#define trigPin 5
#define echoPin 4
// defines variables
//long duration, distance;
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
//pinMode(E1, OUTPUT);
//pinMode(E2, OUTPUT);
pinMode(I1, OUTPUT);
pinMode(I2, OUTPUT);
pinMode(I3, OUTPUT);
pinMode(I4, OUTPUT);
}
void loop() {
int QRE_Value = analogRead(QREi1113_Pin0);
Serial.println(QRE_Value);
//int QRE_Value = analogRead(QRE1113_Pin1)
//Serial.println(QRE_Value1);
long duration, distance;
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH);
// delayMicroseconds(1000); - Removed this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
Serial.print(distance);
Serial.println(" cm");
ROTATE(); // start roteteint QRE_Serial.print("xroma ");
if (distance < 10){
Stop();
while (distance < 10 ) {
FORWARD();
distance = (duration/2) / 29.1;
Serial.print(distance);
Serial.println(" cm");
QRE_Value = analogRead(QREi1113_Pin0);
if ( QRE_Value > 150 ) { break;}
delayMicroseconds(2); }
}
if (QRE_Value< 150 ) // < 650 means white line
{
Stop();
delay(20);
BACKWARD();
delay(10);
}
}
//--------------------------------------------
void FORWARD (){
//analogWrite(E1, 255); // Run in half speed
//analogWrite(E2, 255); // Run in full speed
digitalWrite(I1,HIGH );
digitalWrite(I2, LOW);
digitalWrite(I3, HIGH);
digitalWrite(I4, LOW);
Serial.println(" mprosta");
}
//--------------------------------------------
void BACKWARD (){
// analogWrite(E1, 255); // Run in half speed
//analogWrite(E2, 255); // Run in full speed
digitalWrite(I1, LOW);
digitalWrite(I2, HIGH);
digitalWrite(I3, LOW);
digitalWrite(I4, HIGH);
Serial.println("piso");
}
//--------------------------------------------
void ROTATE (){//RORATE LEFT
// analogWrite(E1, 200); // Run in half speed
// analogWrite(E2, 200); // Run in full speed
digitalWrite(I1, LOW);
digitalWrite(I2, HIGH);
digitalWrite(I3, HIGH);
digitalWrite(I4, LOW);
Serial.println(" stript");
}
//--------------------------------------------
void Stop(){
digitalWrite(I1, LOW);
digitalWrite(I2, LOW);
digitalWrite(I3, LOW);
digitalWrite(I4, LOW);
Serial.println(" stamata");
//delay(2000);
}
Comments
Post a Comment