Robot Sumo Problem

Hello guys , I am trying to do a SUMO Robot using arduino nano and a couple of Line Sensors, Ultrasound and 2 Servo Motors full rotation 360°.

it should find the other Robot and attack (move forward) and push it till its out of the black line,.
I am using Protothreading to read the line sensor but the problem is that i can’t make the robot go out of the IF condition, so basically it looks for the other robot, if it’s found the robot move forward, but never get back to look again it just keep moving for ever.
Another issue that sometimes Ultasound sensor doesn’t work very good and take a long time to detect.

hope that you may help me !

#include <TimedAction.h>
#include <Servo.h>

Servo servoL;
Servo servoR;

int SL = A0;
int SR = A1;

const int trigPin = 11;
const int echoPin = 12;

long duration;
int distance;

int y = 1;

TimedAction LineSensor = TimedAction(50,Detener);

void Detener(){
  
  if(sr() < 400 || sl() < 400)
   {
    LineSensor.disable();
    Back();
    delay(500);
    TurnAround();
    delay(500);
    Reset();
    LineSensor.enable();
   }
}

int Reset()
{
  return y = 0;
}

void Buscar(){
  int x = Ultrasound();
  if(x < 20 && x > 6 )
  {  
    Forward();
    //Serial.println("HAY ALGO");
    //Serial.println(x);    
  }
  else
  { 
    TurnAround();
    //Serial.println("NO esta el ql....");
    //Serial.println(x); 
  }
}


void setup() {
  Serial.begin(9600);
  
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  
  pinMode(SL, INPUT);
  pinMode(SR, INPUT);

  delay(3000);
  
  servoL.attach(9);
  servoR.attach(8);
}

void loop() {
  int x = Ultrasound();
  TurnAround(); 
  if ( x < 20 && x > 6 )
  {
    do{
      Forward();
      Serial.println("HAY ALGO");
      Serial.println(x);
      LineSensor.check();
    }while(y = 1);
  }
  else
  {
    TurnAround();
    Serial.println("nada");
    Serial.println(x);    
  }
  

}

int Ultrasound()
{
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance= duration*0.034/2;

  return distance;  
}

int sr()
{
  int val = analogRead(SR);
}

int sl()
{
  int val = analogRead(SL);
}

void Forward()
{
  servoR.write(180);
  servoL.write(0);
}

void Back()
{
  servoR.write(0);
  servoL.write(180);
}

void Stop()
{
 servoR.write(90);
 servoL.write(90); 
}

void TurnAround()
{
  servoR.write(150);
  servoL.write(150);
}

Protothreading.ino (2.12 KB)

int sr()
{
  int val = analogRead(SR);
}

You promised that the function will return an int. So, why doesn't it? What is this function actually accomplishing? Hint: nothing.

Why are you using TimedAction? It seems to me that doing something continuously is better than doing it now and then.

Good point ! but that's not the problem that I have.

Regarding this code:

  if ( x < 20 && x > 6 )
  {
    do{
      Forward();
      Serial.println("HAY ALGO");
      Serial.println(x);
      LineSensor.check();
    }while(y = 1);

The condition should be y == 1, not y = 1. A few more Serial.println(…) statements might have helped to figure this out, especially Serial.print(“y=”); Serial.println(y);

vaj4088:
Regarding this code:

  if ( x < 20 && x > 6 )

{
    do{
      Forward();
      Serial.println(“HAY ALGO”);
      Serial.println(x);
      LineSensor.check();
    }while(y = 1);




The condition should be y **==** 1, not y **=** 1. A few more Serial.println(...) statements might have helped to figure this out, especially Serial.print("y="); Serial.println(y);

Thank you very much it works, but now it can’t recognize the black line, it reads the line once or twice and goes off. :frowning:

  1. What do your Serial.println(...) statements tell you?

  2. Have you fixed the issue that PaulS told you about? [I mean function sr().]

  3. Did you fix the other issue that is like #2?

  4. Does your program compile without errors?