boomerang bot with object detection

#include <Sparki.h> // include the sparki library

void setup() 
{

  sparki.servo(0);
randomSeed(analogRead(1));
int avoidance= (90);
int test= (15);
int moveA=random (10,20);
sparki.clearLCD();
delay(1000);
sparki.print(moveA);
sparki.updateLCD();
delay(1000);
int touch=analogRead(0);
sparki.clearLCD();
delay(1000);
sparki.print(touch);
sparki.updateLCD();
delay(1000);
if (touch<1)
{
sparki.moveForward(moveA);
delay(1000);
randomSeed(analogRead(1));
delay(1000);
int moveB=random (50,90);
sparki.clearLCD();
delay(1000);
sparki.print(moveB);
sparki.updateLCD();
delay(1000);
sparki.moveLeft(moveB);
randomSeed(analogRead(1));
delay(1000);
int moveC=random (10,20);
sparki.clearLCD();
delay(1000);
sparki.print(moveC);
sparki.updateLCD();
delay(1000);
delay(1000);
int touch=analogRead(0);
sparki.clearLCD();
delay(1000);
sparki.print(touch);
sparki.updateLCD();
delay(1000);
if (touch<1)
{
sparki.moveForward(moveC);
delay(1000);
randomSeed(analogRead(1));
delay(1000);
int moveD=random (50,90);
sparki.clearLCD();
delay(1000);
sparki.print(moveD);
sparki.updateLCD();
delay(1000);
int touch=analogRead(0);
sparki.clearLCD();
delay(1000);
sparki.print(touch);
sparki.updateLCD();
delay(1000);
sparki.moveRight(moveD);
randomSeed(analogRead(1));
int moveE=random (10,20);
sparki.clearLCD();
delay(1000);
sparki.print(moveE);
sparki.updateLCD();
delay(1000);
sparki.clearLCD();
delay(1000);
sparki.print(touch);
sparki.updateLCD();
delay(1000);
if (touch<1)
{
sparki.moveForward(moveE);
sparki.moveLeft(180);
delay(1000);
sparki.moveForward(moveE);
sparki.clearLCD();
delay(1000);
sparki.print(moveD);
sparki.updateLCD();
delay(1000);
sparki.moveLeft(moveD);
int touch=analogRead(0);
sparki.clearLCD();
delay(1000);
sparki.print(touch);
sparki.updateLCD();
delay(1000); 
if (touch<1)
{
  sparki.clearLCD();
delay(1000);
sparki.print(moveC);
sparki.updateLCD();
delay(1000);
sparki.moveForward(moveC);
sparki.clearLCD();
delay(1000);
sparki.print(moveB);
sparki.updateLCD();
delay(1000);
sparki.moveRight(moveB);
int touch=analogRead(0);
sparki.clearLCD();
delay(1000);
sparki.print(touch);
sparki.updateLCD();
delay(1000);
if (touch<1)
{
  sparki.clearLCD();
delay(1000);
sparki.print(moveA);
sparki.updateLCD();
delay(1000);
sparki.moveForward(moveA);
delay(1000);
sparki.beep();
sparki.clearLCD();
delay(1000);
sparki.drawString(10,1, "home");
sparki.updateLCD();
delay(1000);

}if (touch>1)
{


sparki.moveLeft(avoidance);
sparki.moveForward(test);
sparki.moveRight(avoidance);
int touch=analogRead(0);
delay(1000);
sparki.clearLCD();
delay(1000);
sparki.print(touch);
sparki.updateLCD();
delay(1000);
if (touch<1)
{
  sparki.clearLCD();
delay(1000);
sparki.print(moveA);
sparki.updateLCD();
delay(1000);
sparki.moveForward(moveA);
sparki.moveRight(avoidance);
sparki.moveForward(test);
delay(1000);
sparki.beep();
sparki.clearLCD();
delay(1000);
sparki.drawString(10,1, "home");
sparki.updateLCD();
delay(1000);
}
}
}
if (touch>1){
sparki.moveRight(avoidance);
sparki.moveForward(test);
sparki.moveLeft(avoidance);
delay(1000);
int touch=analogRead(0);
sparki.clearLCD();
delay(1000);
sparki.print(touch);
sparki.updateLCD();
delay(1000);
if (touch<1)
{
  sparki.clearLCD();
delay(1000);
sparki.print(moveC);
sparki.updateLCD();
delay(1000);
sparki.moveForward(moveC);
sparki.moveLeft(avoidance);
sparki.moveForward(test);
sparki.moveRight(avoidance);
delay(1000);
sparki.print(moveB);
sparki.updateLCD();
delay(1000);
sparki.moveRight(moveB);
int touch=analogRead(0);
sparki.clearLCD();
delay(1000);
sparki.print(touch);
sparki.updateLCD();
delay(1000);
if (touch<1)
{
  sparki.clearLCD();
delay(1000);
sparki.print(moveA);
sparki.updateLCD();
delay(1000);
sparki.moveForward(moveA);
delay(1000);
sparki.beep();
sparki.clearLCD();
delay(1000);
sparki.drawString(10,1, "home");
sparki.updateLCD();
delay(1000);

}
else
{
  sparki.moveLeft(avoidance);
sparki.moveForward(test);
sparki.moveRight(avoidance);
int touch=analogRead(0);
delay(1000);
sparki.clearLCD();
delay(1000);
sparki.print(touch);
sparki.updateLCD();
delay(1000);
if (touch<1)
{
  sparki.clearLCD();
delay(1000);
sparki.print(moveA);
sparki.updateLCD();
delay(1000);
sparki.moveForward(moveA);
sparki.moveRight(avoidance);
sparki.moveForward(test);
delay(1000);
sparki.beep();
sparki.clearLCD();
delay(1000);
sparki.drawString(10,1, "home");
sparki.updateLCD();
delay(1000);
}
}
}
}
}
if (touch>1){
sparki.moveRight(avoidance);
sparki.moveForward(test);
sparki.moveLeft(avoidance);
delay(1000);
int touch=analogRead(0);
sparki.clearLCD();
delay(1000);
sparki.print(touch);
sparki.updateLCD();
delay(1000);
if (touch<1)
{
  sparki.clearLCD();
delay(1000);
sparki.print(moveE);
sparki.updateLCD();
delay(1000);
sparki.moveForward(moveE);
sparki.moveLeft(avoidance);
sparki.moveForward(test);
sparki.moveRight(avoidance);
delay(1000);
sparki.moveLeft(180);
delay(1000);
sparki.moveForward(moveE);
sparki.clearLCD();
delay(1000);
sparki.print(moveD);
sparki.updateLCD();
delay(1000);
sparki.moveLeft(moveD);
int touch=analogRead(0);
sparki.clearLCD();
delay(1000);
sparki.print(touch);
sparki.updateLCD();
delay(1000); 
if (touch<1)
{
  sparki.clearLCD();
delay(1000);
sparki.print(moveC);
sparki.updateLCD();
delay(1000);
sparki.moveForward(moveC);
sparki.clearLCD();
delay(1000);
sparki.print(moveB);
sparki.updateLCD();
delay(1000);
sparki.moveRight(moveB);
int touch=analogRead(0);
sparki.clearLCD();
delay(1000);
sparki.print(touch);
sparki.updateLCD();
delay(1000);
if (touch<1)
{
  sparki.clearLCD();
delay(1000);
sparki.print(moveA);
sparki.updateLCD();
delay(1000);
sparki.moveForward(moveA);
delay(1000);
sparki.beep();
sparki.clearLCD();
delay(1000);
sparki.drawString(10,1, "home");
sparki.updateLCD();
delay(1000);

}if (touch>1)
{


sparki.moveLeft(avoidance);
sparki.moveForward(test);
sparki.moveRight(avoidance);
int touch=analogRead(0);
delay(1000);
sparki.clearLCD();
delay(1000);
sparki.print(touch);
sparki.updateLCD();
delay(1000);
if (touch<1)
{
  sparki.clearLCD();
delay(1000);
sparki.print(moveA);
sparki.updateLCD();
delay(1000);
sparki.moveForward(moveA);
sparki.moveRight(avoidance);
sparki.moveForward(test);
delay(1000);
sparki.beep();
sparki.clearLCD();
delay(1000);
sparki.drawString(10,1, "home");
sparki.updateLCD();
delay(1000);
}
}
}}
}
}
if (touch>1){
sparki.moveRight(avoidance);
sparki.moveForward(test);
sparki.moveLeft(avoidance);
delay(1000);
int touch=analogRead(0);
sparki.clearLCD();
delay(1000);
sparki.print(touch);
sparki.updateLCD();
delay(1000);
if (touch<1)
{
  sparki.clearLCD();
delay(1000);
sparki.print(moveC);
sparki.updateLCD();
delay(1000);
sparki.moveForward(moveC);
sparki.moveLeft(avoidance);
sparki.moveForward(test);
sparki.moveRight(avoidance);
delay(1000);
delay(1000);
randomSeed(analogRead(1));
delay(1000);
int moveD=random (50,90);
sparki.clearLCD();
delay(1000);
sparki.print(moveD);
sparki.updateLCD();
delay(1000);
int touch=analogRead(0);
sparki.clearLCD();
delay(1000);
sparki.print(touch);
sparki.updateLCD();
delay(1000);
sparki.moveRight(moveD);
randomSeed(analogRead(1));
int moveE=random (10,20);
sparki.clearLCD();
delay(1000);
sparki.print(moveE);
sparki.updateLCD();
delay(1000);
sparki.clearLCD();
delay(1000);
sparki.print(touch);
sparki.updateLCD();
delay(1000);
if (touch<1)
{
sparki.moveForward(moveE);
sparki.moveLeft(180);
delay(1000);
sparki.moveForward(moveE);
sparki.clearLCD();
delay(1000);
sparki.print(moveD);
sparki.updateLCD();
delay(1000);
sparki.moveLeft(moveD);
int touch=analogRead(0);
sparki.clearLCD();
delay(1000);
sparki.print(touch);
sparki.updateLCD();
delay(1000); 
if (touch<1)
{
  sparki.clearLCD();
delay(1000);
sparki.print(moveC);
sparki.updateLCD();
delay(1000);
sparki.moveForward(moveC);
sparki.clearLCD();
delay(1000);
sparki.print(moveB);
sparki.updateLCD();
delay(1000);
sparki.moveRight(moveB);
int touch=analogRead(0);
sparki.clearLCD();
delay(1000);
sparki.print(touch);
sparki.updateLCD();
delay(1000);
if (touch<1)
{
  sparki.clearLCD();
delay(1000);
sparki.print(moveA);
sparki.updateLCD();
delay(1000);
sparki.moveForward(moveA);
delay(1000);
sparki.beep();
sparki.clearLCD();
delay(1000);
sparki.drawString(10,1, "home");
sparki.updateLCD();
delay(1000);

}

}}
}}}
if (touch>1){
sparki.moveRight(avoidance);
sparki.moveForward(test);
sparki.moveLeft(avoidance);
int touch=analogRead(0);
delay(1000);
sparki.clearLCD();
delay(1000);
sparki.print(touch);
sparki.updateLCD();
delay(1000);
if (touch<1)
{
  sparki.clearLCD();
delay(1000);
sparki.print(moveA);
sparki.updateLCD();
delay(1000);
sparki.moveForward(moveA);
sparki.moveLeft(avoidance);
sparki.moveForward(test);
sparki.moveRight(avoidance);
delay(1000);
delay(1000);
randomSeed(analogRead(1));
delay(1000);
int moveB=random (50,90);
sparki.clearLCD();
delay(1000);
sparki.print(moveB);
sparki.updateLCD();
delay(1000);
sparki.moveLeft(moveB);
randomSeed(analogRead(1));
delay(1000);
int moveC=random (10,20);
sparki.clearLCD();
delay(1000);
sparki.print(moveC);
sparki.updateLCD();
delay(1000);
delay(1000);
int touch=analogRead(0);
sparki.clearLCD();
delay(1000);
sparki.print(touch);
sparki.updateLCD();
delay(1000);
if (touch<1)
{
sparki.moveForward(moveC);
delay(1000);
randomSeed(analogRead(1));
delay(1000);
int moveD=random (50,90);
sparki.clearLCD();
delay(1000);
sparki.print(moveD);
sparki.updateLCD();
delay(1000);
int touch=analogRead(0);
sparki.clearLCD();
delay(1000);
sparki.print(touch);
sparki.updateLCD();
delay(1000);
sparki.moveRight(moveD);
randomSeed(analogRead(1));
int moveE=random (10,20);
sparki.clearLCD();
delay(1000);
sparki.print(moveE);
sparki.updateLCD();
delay(1000);
sparki.clearLCD();
delay(1000);
sparki.print(touch);
sparki.updateLCD();
delay(1000);
if (touch<1)
{
sparki.moveForward(moveE);
sparki.moveLeft(180);
delay(1000);
sparki.moveForward(moveE);
sparki.clearLCD();
delay(1000);
sparki.print(moveD);
sparki.updateLCD();
delay(1000);
sparki.moveLeft(moveD);
int touch=analogRead(0);
sparki.clearLCD();
delay(1000);
sparki.print(touch);
sparki.updateLCD();
delay(1000); 
if (touch<1)
{
  sparki.clearLCD();
delay(1000);
sparki.print(moveC);
sparki.updateLCD();
delay(1000);
sparki.moveForward(moveC);
sparki.clearLCD();
delay(1000);
sparki.print(moveB);
sparki.updateLCD();
delay(1000);
sparki.moveRight(moveB);
int touch=analogRead(0);
sparki.clearLCD();
delay(1000);
sparki.print(touch);
sparki.updateLCD();
delay(1000);
if (touch<1)
{
  sparki.clearLCD();
delay(1000);
sparki.print(moveA);
sparki.updateLCD();
delay(1000);
sparki.moveForward(moveA);
delay(1000);
sparki.beep();
sparki.clearLCD();
delay(1000);
sparki.drawString(10,1, "home");
sparki.updateLCD();
delay(1000);

}
}}
}}
}
}


void loop() {}

This follows on from the previously uploaded code in which Sparki can be placed in an area, make random movements, yet return to the very same area. In this code, if Sparki’s random movements include an object to avoid (note this can only be one object as this code could be improved) Sparki can successfully navigate the object and still successfully return to the home zone or starting spot.

You will have noticed that this code relies on Sparki’s touch sensor colliding with the object, that is simply because the eyes are still not working and I havent connected up the infrared sensor yet.

I dont have any video to share with you guys of Sparki successfully completing this as I just dont have the equipment 😦 but here is a pic to make you smile 🙂

DSC_0468

In the next instalment I’ll show you guys how I set up the infrared sensor as an obstacle detector (much better than the touch sensor).

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s