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)