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!
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);
}
sketch_jul14a.ino (2.3 KB)