Free roam or boomerang

In this code, you can use the light sensors to tell Sparki what you want him to do. He can either, free roam in your room or a given area, avoiding obstacles, or use the boomerang routine, where he will explore the zone and return to the “home” area.

Handy to have a robot wandering around. Might attach a cloth so he can buff the floor as he goes πŸ™‚

 #include <Sparki.h> // include the sparki library
void setup()
{
  sparki.servo(0);
}

 void loop()
 {
    sparki.clearLCD();
  sparki.drawString(10,1,"Hello human");
  sparki.drawString(10,3,"right for free roam");
  sparki.drawString(10,5,"left for boomerang");
  sparki.updateLCD();
  delay(5000);
  if (sparki.lightRight()>sparki.lightLeft())
  {
    boomerang();
  }
  else if (sparki.lightRight()<sparki.lightLeft())
  {
   for (int i = 0; i < 10; i=i+1)   {     freeRoam();   }   }   else{     delay(5000);      }  }    void freeRoam(){   while(sparki.ping()>10)
      {
      sparki.moveForward();
      sparki.servo(0);
                    }
                        sparki.moveStop();
                          sparki.servo(90);
                          delay(1000);
                          int R=sparki.ping();
                          sparki.ping();
                          delay(2000);
                          sparki.clearLCD();
                          sparki.println(R);
                          sparki.updateLCD();
                          delay(1000);
                          sparki.servo(-90);
                          delay(1000);
                          int L = sparki.ping();
                          sparki.ping();
                          delay(2000);
                          sparki.clearLCD();
                          sparki.println(L);
                          sparki.updateLCD();
                          delay(1000);
                          sparki.servo(0);
                          if((R>10) && (R>=L))
                          {
                          sparki.moveRight(90);
                          sparki.servo(0);
                          }
                          if ((L>10) && (L>=R))
                          {
                            sparki.moveLeft(90);
                            sparki.servo(0);
                          }
                         if ((R<=10) && (L <= 10))
                         {
                           sparki.moveRight(180);
                           sparki.RGB(0,0,220);
                           sparki.servo(0);
                         }
}

void boomerang()
{
   sparki.beep();
  sparki.moveLeft(30);
  sparki.moveRight(30);
  sparki.clearLCD();
  sparki.println("home zone");
  sparki.updateLCD();
  delay(1000);
   sparki.clearLCD();
  randomSeed(analogRead(1));
int response1 = random(1,3);
  sparki.clearLCD();
  sparki.print(response1);
  sparki.updateLCD();
  sparki.moveForward(response1 *10);
  sparki.clearLCD();
  randomSeed(analogRead(1));
  int response2 = random(45, 90);
  sparki.clearLCD();
  sparki.print(response2);
  sparki.updateLCD();
  sparki.moveLeft(response2);
  sparki.clearLCD();
  randomSeed(analogRead(1));
  int response3 = random(1,3);
  sparki.clearLCD();
  sparki.print(response3);
  sparki.updateLCD();
  sparki.moveForward(response3*10);
  sparki.clearLCD();
  randomSeed(analogRead(1));
  int response4 = random(45, 90);
  sparki.clearLCD();
  sparki.print(response4);
  sparki.updateLCD();
  sparki.moveRight(response4);
  sparki.clearLCD();
  randomSeed(analogRead(1));
  int response5 = random(1, 3);
  sparki.clearLCD();
  sparki.print(response5);
  sparki.updateLCD();
   sparki.moveForward(response5 *10);
    sparki.clearLCD();
  randomSeed(analogRead(1));
  int response6 = random(45, 90);
  sparki.clearLCD();
  sparki.print(response6);
  sparki.updateLCD();
  sparki.moveLeft(response6);
  sparki.clearLCD();
  randomSeed(analogRead(1));
  int response7 = random(1, 3);
  sparki.clearLCD();
  sparki.print(response7);
  sparki.updateLCD();
   sparki.moveForward(response7 *10);
    sparki.moveLeft(180);
    sparki.moveForward(response7 *10);
    sparki.moveRight(response6);
  sparki.moveForward(response5*10);
  sparki.moveLeft(response4);
  sparki.moveForward(response3*10);
  sparki.moveRight(response2);
int home=(response1*10+3);
  sparki.moveForward(home);
  sparki.moveLeft(180);
  sparki.moveForward(2);
  sparki.beep();
  sparki.clearLCD();
  sparki.drawString(10,1, "home");
  sparki.updateLCD();
  delay(1000);
}

 

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s