Go Down

Topic: Sumo Bot Help, Please! (Read 1 time) previous topic - next topic

Ketsurui15

Jul 14, 2014, 09:11 am Last Edit: Jul 14, 2014, 09:44 am by Ketsurui15 Reason: 1
Hello, me and my schoolmates are participating in an inter-school robotics competition. We're in the Sumo Bot category, but, we are complete newbies at this whole Arduino thing. And we were hoping for some help in creating a working sketch for it. This was the basic robotics sketch that was given to us, however, it doesn't seem to work.
Also, we are using an Arduino Leonardo and we are using 2 Line sensors, 1 distance sensor. Any recommendations on what else we should add? PLEASE! Our competition is this Friday, and we are NOT 100% prepared for it.
REMINDER: WE ARE MAJOR NEWBS!

Code: [Select]
int front1= 2;
int front2= 3;
int front3= 4;
int under1 = 5;
int under2 = 6;
int under3 = 7;
int motor1dir = 8;
int motor2run = 9;
int motor2dir = 11;
int motor2run = 10;

void setup() {

pinMode(front1, INPUT);
pinMode(front2, INPUT);
pinMode(front3, INPUT);
pinMode(under1, INPUT);
pinMode(under2, INPUT);
pinMode(under3, INPUT);
pinMode(motor2dir, OUTPUT);
pinMode(motor2run, OUTPUT);
pinMode(motor1dir, OUTPUT);
pinMode(motor1run, OUTPUT);
}

int colsense;
int stuckdetect;
int linesense;
long timer=1000;
boolean randdec;
int mode;

void loop()
{
colsense=0;
linesense=0;
if(digitalRead(front1)==LOW) colsense=1;
if(digitalRead(front2)==LOW) colsense=colsense+2;
if(digitalRead(front3)==LOW) colsense=colsense+4;
if(digitalRead(under1)==LOW) linesense=1;
if(digitalRead(under2)==LOW) linesense=linesense+2;
if(digitalRead(under3)==LOW) linesense=linesense+4;

// random decision maker
if(random(1,1000)<500)
randdec=LOW;
else
randdec=HIGH;

// Check if black border is encountered, back off
if(linesense !=0)
{
randomSeed(millis()); //randomize everything

//rotate to keep within arena
runBot(200,200,LOW);
delay(500);
rotBot(150,randdec);
delay(500);

}

if(linesense ==0)
{

if(colsense==2)
// enemy straight ahead, ram it
{

runBot(250,250,HIGH);
delay(250);
}

//Obstruction 1
if(colsense==1)
{
analogWrite(motor1run, 250);
delay(100);
}

if(colsense==3)
{
analogWrite(motor1run, 150);
delay(100);
}

if(colsense==4)
{
analogWrite(motor2run, 250);
delay(100);
}

if(colsense==6)
{
analogWrite(motor2run, 150);
delay(100);
}
}

if((linesense==0) & (colsense==0))
// search for enemy mode
{
mode=random(1,7);
// chane search mode after random time
if(timer<millis())
{
timer=millis()+ random(500,1000); // change interval random at 1 to 5 sec
//
if(mode==1) rotBot(150,randdec);
if(mode==2) runBot(150,100,HIGH);
if(mode==3) runBot(100,150,HIGH);
if(mode==4) runBot(100,100,HIGH);
if(mode>4) Stop();
}

}

}

void runBot(int spd1,int spd2, boolean direction )
{
digitalWrite(motor2dir,direction);
digitalWrite(motor1dir,direction);
analogWrite(motor1run, spd1);
analogWrite(motor2run, spd2);
}

void rotBot(int speed, boolean direction)
{
digitalWrite(motor2dir,direction);
digitalWrite(motor1dir,~direction);
analogWrite(motor2run, speed);
analogWrite(motor1run, speed);
}

void Stop(void)
{
analogWrite(motor2run, 0);
analogWrite(motor1run, 0);
}

Capital_Seven

What specifically is not working?  Does the bot power on?  Do you have the appropriate motor shield to power your motors?

Give us some guidance so we can help you out.

-Cap

Tirmit

Hi !
Try this :

Code: [Select]


// INIT
int front1= 2;
int front2= 3;
int front3= 4;
int under1 = 5;
int under2 = 6;
int under3 = 7;
int motor1dir = 8;
int motor1run = 9;
int motor2dir = 11;
int motor2run = 10;

int colsense;
int stuckdetect;
int linesense;
long timer=1000;
boolean randdec;
int mode;

//FUNCTION

void runBot(int spd1,int spd2, boolean direction )
{
digitalWrite(motor2dir,direction);
digitalWrite(motor1dir,direction);
analogWrite(motor1run, spd1);
analogWrite(motor2run, spd2);
}

void rotBot(int speed, boolean direction)
{
digitalWrite(motor2dir,direction);
digitalWrite(motor1dir,~direction);
analogWrite(motor2run, speed);
analogWrite(motor1run, speed);
}


void Stop(void)
{
analogWrite(motor2run, 0);
analogWrite(motor1run, 0);
}


// MAIN LOOP

void setup() {

pinMode(front1, INPUT);
pinMode(front2, INPUT);
pinMode(front3, INPUT);
pinMode(under1, INPUT);
pinMode(under2, INPUT);
pinMode(under3, INPUT);
pinMode(motor2dir, OUTPUT);
pinMode(motor2run, OUTPUT);
pinMode(motor1dir, OUTPUT);
pinMode(motor1run, OUTPUT);
}


void loop()
{
colsense=0;
linesense=0;
if(digitalRead(front1)==LOW) colsense=1;
if(digitalRead(front2)==LOW) colsense=colsense+2;
if(digitalRead(front3)==LOW) colsense=colsense+4;
if(digitalRead(under1)==LOW) linesense=1;
if(digitalRead(under2)==LOW) linesense=linesense+2;
if(digitalRead(under3)==LOW) linesense=linesense+4;

// random decision maker
if(random(1,1000)<500)
randdec=LOW;
else
randdec=HIGH;

// Check if black border is encountered, back off
if(linesense !=0)
  {
  randomSeed(millis()); //randomize everything
 
  //rotate to keep within arena
  runBot(200,200,LOW);
  delay(500);
  rotBot(150,randdec);
  delay(500);
 
  }

if(linesense ==0)
  {
 
  if(colsense==2)
  // enemy straight ahead, ram it
      {
     
      runBot(250,250,HIGH);
      delay(250);
      }
 
  //Obstruction 1
  if(colsense==1)
      {
      analogWrite(motor1run, 250);
      delay(100);
      }
 
  if(colsense==3)
      {
      analogWrite(motor1run, 150);
      delay(100);
      }
 
  if(colsense==4)
      {
      analogWrite(motor2run, 250);
      delay(100);
      }
 
  if(colsense==6)
      {
      analogWrite(motor2run, 150);
      delay(100);
      }
  }

if((linesense==0) & (colsense==0))
  // search for enemy mode
{
          mode=random(1,7);
          // chane search mode after random time
          if(timer<millis())
          {
          timer=millis()+ random(500,1000); // change interval random at 1 to 5 sec
          //
          if(mode==1) rotBot(150,randdec);
          if(mode==2) runBot(150,100,HIGH);
          if(mode==3) runBot(100,150,HIGH);
          if(mode==4) runBot(100,100,HIGH);
          if(mode>4) Stop();
          }

}

}




Go Up