sumo and line following

Hi!I try make sumo robot and line following robot together.I have write code,but if i run it in my my robot has difficulty doing the drive I have written on arduino idle. I use arduino uno,breadboard,L239d,2 dc motor,9v batteryultrasonic sensor hc-sr04,2 qre113 analod,1 switch on off and some cables.My code have any problem?How i can add code about line following robot in sumo's arduion code,?

sumo.txt (3.05 KB)

Jochris22:
Hi!I try make sumo robot and line following robot together.I have write code,but if i run it in my my robot has difficulty doing the drive I have written on arduino idle. I use arduino uno,breadboard,L239d,2 dc motor,9v batteryultrasonic sensor hc-sr04,2 qre113 analod,1 switch on off and some cables.My code have any problem?How i can add code about line following robot in sumo's arduion 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);
}

My code have any problem?

That is something you need to tell us. What does the code ACTUALLY do? How does that differ from what you want it to do, if it differs at all?

void Stop(){
 
   
 
    digitalWrite(I1, LOW);
    digitalWrite(I2, LOW);
    digitalWrite(I3, LOW);
    digitalWrite(I4, LOW);
  Serial.println(" stamata");
  //delay(2000);
}

It pisses my off royally when someone jams the { up against the function

and then wastes space with a bunch of blank lines. Get rid of the useless blank lines. Put EVERY { on a line BY ITSELF. Put EVERY } on a line BY ITSELF. Use Tools + Auto Format to properly indent the code.

ROTATE(); // start roteteint QRE_Serial.print("xroma ");



 if (distance < 10){
    Stop();

Unconditionally rotate. Then, depending on distance, do something, which might, or might not, include reading some undefined sensor. That hardly makes for a well controlled robot.