Conversation and Entertainment Bot

Hi everyone,
A few months back I accidentally broke the ultra-sonic range finder on my Sparki and Arcbotics kindly offered to send me a replacement board but I declined as I did not know how to fit it. Since then I have been using an infra-red obstacle detector instead (although it cant measure distances to make intelligent choices). I have also added a sound sensor and a touch sensor to Sparki.
With these additions I have written a code to make Sparki super interactive:

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

void setup()
{
  sparki.servo(0);
  delay(2000);
  int soundA = analogRead(0);
  delay (2000);
  sparki.clearLCD();
  // print out the value you read:
  sparki.print(soundA);
  sparki.updateLCD();
  delay (1000);
  int soundB=analogRead(0);
  delay(2000);
   sparki.print(soundB);
  sparki.updateLCD();
  delay (1000);
  int soundC = analogRead(0);
  delay (2000);
  sparki.clearLCD();
  // print out the value you read:
  sparki.print(soundC);
  sparki.updateLCD();
  delay (1000);
  
  if ((soundB> 1.26) || (soundC>1.26))
  {
  
  sparki.clearLCD();
  sparki.drawString(10, 1, "hi ash how are you?");
  sparki.updateLCD();
  delay(1000);
  int soundA = analogRead(0);
  delay (2000);
  sparki.clearLCD();
  // print out the value you read:
  sparki.print(soundA);
  sparki.updateLCD();
  delay (1000);
  int soundB=analogRead(0);
  delay(2000);
   sparki.print(soundB);
  sparki.updateLCD();
  delay (1000);
  int soundC = analogRead(0);
  delay (2000);
  sparki.clearLCD();
  // print out the value you read:
  sparki.print(soundC);
  sparki.updateLCD();
  delay (1000);
 
  if((soundB> 1.26) || (soundC>1.26))
  {
 delay(2000);  
    sparki.clearLCD();
  sparki.drawString(10, 1, "affirmative, you have had a good day");
  sparki.updateLCD();
  delay(1000);
  sparki.clearLCD();
  randomSeed(analogRead(1));
  int response = random(1, 10);
  sparki.clearLCD();
  sparki.print(response);
  sparki.updateLCD();
  delay(1000);
  if (response<3)
  {
  sparki.drawString(20, 2, "when I am happy I dance...I'm dancing!");
  sparki.updateLCD();
  delay(1000);
    sparki.servo(0);
  sparki.moveForward(3);
  sparki.RGB(RGB_GREEN);
  sparki.beep();
  sparki.moveLeft(90);
  sparki.RGB(RGB_RED);
  sparki.beep();
  sparki.moveForward(3);
  sparki.RGB(RGB_GREEN);
  sparki.moveBackward(3);
  sparki.beep();
  sparki.moveRight(90);
  sparki.RGB(RGB_BLUE);
  sparki.beep();
  delay(2000);
  }
  if (response>=3)
  {
     sparki.clearLCD();
   sparki.drawString(20, 2, "here is a bot hug");
    sparki.updateLCD();
   delay(1000);
    sparki.moveForward(5);
      int touch= analogRead(8);
      sparki.print(touch);
      sparki.updateLCD();
      delay(1000);
      if(touch<100)
      {
      sparki.gripperClose(3);
      delay(1000);
       int touch= analogRead(8);
      sparki.print(touch);
      sparki.updateLCD();
      delay(1000);
       if(touch>100)
      {
      sparki.beep();
      sparki.gripperOpen(3);
      delay(1000);}
    }

      sparki.print(touch);
      sparki.updateLCD();
      delay(1000);
      if(touch>100)
      {
      sparki.beep();
      sparki.gripperOpen(3);
      delay(1000);}
    }

    else
    {
    }
   }


else
   {
     delay(2000);
   sparki.clearLCD();
  sparki.drawString(10, 1, "oh dear maybe i can cheer you up?");
  sparki.updateLCD();
  delay(1000);
  sparki.clearLCD();
  randomSeed(analogRead(1));
  int response = random(1, 10);
  sparki.clearLCD();
  sparki.print(response);
  sparki.updateLCD();
  delay(1000);
  if (response<3)
  {
  sparki.drawString(20, 2, "maybe my dance can make you smile");
  sparki.updateLCD();
  delay(1000);
    sparki.servo(0);
  sparki.moveForward(3);
  sparki.RGB(RGB_GREEN);
  sparki.beep();
  sparki.moveLeft(90);
  sparki.RGB(RGB_RED);
  sparki.beep();
  sparki.moveForward(3);
  sparki.RGB(RGB_GREEN);
  sparki.moveBackward(3);
  sparki.beep();
  sparki.moveRight(90);
  sparki.RGB(RGB_BLUE);
  sparki.beep();
  delay(2000);
  }
  if (response>=3)
  {
    sparki.clearLCD();
    sparki.drawString(20,2, "This is my sad dance");
    sparki.updateLCD();
    delay(1000);
    sparki.moveForward(5);
    sparki.beep(500);
    sparki.moveRight(10);
    sparki.moveLeft(10);
    sparki.beep(350);
    sparki.moveBackward(5);
    sparki.moveLeft(30);
    sparki.beep(600);
    sparki.moveRight(60);
    sparki.beep(400);
    }
   }
  }
}
 
  

   
    void loop()
{
  sparki.servo(0);
  sparki.clearLCD();
  sparki.drawString(10, 1, "I am in entertainment BOT mode");
  sparki.updateLCD();
  delay(1000);
	sparki.moveLeft(30);
	sparki.moveRight(30);
	delay(1000);
	sparki.beep(640, 200);
	delay(200);
	sparki.beep(440, 200);
	delay(100);
		int lightLeft = sparki.lightLeft();
		int lightCenter = sparki.lightCenter();
		int lightRight = sparki.lightRight();
		int edgeLeft = sparki.edgeLeft();
		int edgeRight = sparki.edgeRight();
		sparki.servo(0);
		sparki.RGB(0,220,0);
		if(((lightCenter>lightLeft)&&(lightRight>lightLeft)))
		{

			sparki.RGB(RGB_RED);
              delay(500);
            
              sparki.RGB(RGB_ORANGE);
              delay(500);
              
              sparki.RGB(RGB_YELLOW);
              delay(500);
              
              sparki.RGB(RGB_GREEN);
              delay(500);
              sparki.beep();
              
              sparki.RGB(RGB_BLUE);
              delay(500);
              
              sparki.RGB(RGB_INDIGO);
              delay(500);
            
              sparki.RGB(RGB_VIOLET);
              delay(500);
              sparki.beep();
            
              sparki.RGB(RGB_WHITE);
              delay(500);
              
              sparki.RGB(RGB_OFF);
              delay(500);
		}
		else
		{
			delay(1000);
		}
		if(((lightLeft>lightRight)&&(lightCenter>lightRight)))
		{
			
			sparki.beep(840, 400);
			delay(200);
			sparki.beep(640, 400);
			delay(200);
			sparki.beep(440, 400);
			delay(200);
			sparki.beep(340, 400);
			delay(200);
			sparki.beep(640, 800);
			delay(200);
			sparki.beep(440, 800);
			delay(200);
			sparki.beep(340, 400);
			delay(200);
			sparki.beep(440, 400);
			delay(200);
			sparki.beep(640, 400);
			delay(200);
		}
		else
		{
			delay(1000);
		}
		
int soundA = analogRead(0);
  delay (2000);
  sparki.clearLCD();
  // print out the value you read:
  sparki.print(soundA);
  sparki.updateLCD();
  delay (1000);
  int soundB=analogRead(0);
  delay(2000);
   sparki.print(soundB);
  sparki.updateLCD();
  delay (1000);
  int soundC = analogRead(0);
  delay (2000);
  sparki.clearLCD();
  // print out the value you read:
  sparki.print(soundC);
  sparki.updateLCD();
  delay (2000);
  
  if ((soundB> 1.26) || (soundC>1.26))
  {
  randomSeed(analogRead(1));
  int response = random(0, 12);
  sparki.clearLCD();
  sparki.print(response);
  sparki.updateLCD();
  delay(2000);
  if((response>=0) && (response <3))
  {
    sparki.clearLCD();
    sparki.drawString(10, 1, "When I grow up I want to be a social domestic bot");
    sparki.updateLCD();
    delay(3000);
    sparki.moveRight(90);
    sparki.moveForward(5);
    sparki.moveLeft(90);
    sparki.beep();
    sparki.moveBackward(5);
    sparki.moveRight(90);
    sparki.moveForward(5);
    sparki.beep();
    sparki.moveLeft(90);
    sparki.moveBackward(5);
    sparki.moveRight(90);
    sparki.beep();
    sparki.moveForward(5);
    sparki.moveLeft(90);
    sparki.moveBackward(5);
  }
 else if((response>=3) && (response <=8))
  {
        sparki.clearLCD();
sparki.drawString(10,2, "Preparing lazers for combat");
sparki.updateLCD();
delay(3000);
sparki.beep(740, 200);
sparki.clearLCD();
sparki.drawString(10,2, "Pew Pew");
sparki.updateLCD();
delay(1000);
sparki.beep(740,200);
delay(100);
sparki.beep(440,200);
sparki.moveRight(10);
sparki.moveLeft(10);
delay(2000);
      }
      }
    else 
      {
        sparki.clearLCD();
sparki.drawString(10,2, "I will be kind to you when we robots take over the world");
sparki.updateLCD();
delay(3000);
 sparki.moveLeft(90);
  sparki.moveForward(5);
  sparki.beep();
  sparki.moveBackward(5);
  sparki.moveForward(5);
  sparki.RGB(RGB_BLUE);
  sparki.moveBackward(5);
  sparki.beep();
  sparki.moveForward(5);
  sparki.moveRight(180);

      }
      
		if(edgeLeft<600)
		{
			sparki.RGB(220,0,0);
			sparki.beep(840, 400);
			sparki.moveLeft(90);
			sparki.beep(440, 200);
			sparki.moveRight(90);
			sparki.beep(340, 200);
			delay(100);
			sparki.beep(540, 200);
			sparki.RGB(0,0,220);
			sparki.beep(440, 400);
			sparki.moveForward(5);
			delay(200);
			sparki.beep(640, 400);
			delay(200);
			sparki.beep(740, 500);
			sparki.moveBackward(5);
			delay(200);
		}
	
		if(edgeLeft>600)
		{
			
                      sparki.beep(740, 600);
			delay(400);
			sparki.beep(540, 400);
			delay(400);
			sparki.beep(540, 200);
			delay(200);
			sparki.beep(440, 200);
			delay(200);
			sparki.beep(640, 400);
			delay(400);
			sparki.beep(740, 200);
			delay(400);
			sparki.beep(740, 200);
		}
		
  delay (2000);
  sparki.clearLCD();
  // print out the value you read:
  sparki.print(soundA);
  sparki.updateLCD();
  delay (1000);

  delay(2000);
   sparki.print(soundB);
  sparki.updateLCD();
  delay (1000);

  delay (2000);
  sparki.clearLCD();
  // print out the value you read:
  sparki.print(soundC);
  sparki.updateLCD();
  delay (1000);
  
  if ((soundB> 1.26) || (soundC>1.26))
  {
  randomSeed(analogRead(1));
  int response = random(0, 10);
  sparki.clearLCD();
  sparki.print(response);
  sparki.updateLCD();
  delay(1000);
  if((response>=0) && (response <3))
  {
    sparki.clearLCD();
    sparki.drawString(10, 1, "Today I organised my data stream");
    sparki.updateLCD();
    delay(3000);
       sparki.beep(440, 600);
			delay(400);
			sparki.beep(540, 400);
			delay(400);
			sparki.beep(640, 200);
			delay(200);
			sparki.beep(740, 200);
			delay(200);
			sparki.beep(640, 400);
			delay(400);
			sparki.beep(540, 200);
			delay(400);
			sparki.beep(440, 200);
sparki.clearLCD();
sparki.drawString(10,1, "Perfect");
    sparki.updateLCD();
    delay(3000);
  }
 else if((response>=3) && (response <=8))
  {
sparki.clearLCD();
sparki.drawString(10,2, "Shake my hand human");
sparki.updateLCD();
delay(3000);
int shake=analogRead(8);
 sparki.print(shake);
      sparki.updateLCD();
      delay(1000);
       if(shake>100)
      {
      sparki.beep();
      sparki.gripperClose(6);
      delay(1000);
      sparki.gripperOpen(6);
      delay(1000);
    sparki.clearLCD();
sparki.drawString(10,2, "yey friends!");
sparki.updateLCD();
delay(3000);
      }
  }
  }
    else 
      {
        sparki.clearLCD();
sparki.drawString(10,2, "Today I beamed Jim. He's my cousin in California!");
sparki.updateLCD();
delay(3000);
 sparki.moveRight(90);
  sparki.moveForward(5);
  sparki.RGB(RGB_BLUE);
  sparki.beep();
  sparki.moveBackward(5);
  sparki.moveRight(50);
  sparki.moveBackward(5);
  sparki.moveLeft(50);
  sparki.RGB(RGB_GREEN);
  sparki.beep();
  sparki.moveRight(180);
  sparki.moveForward(7);
  sparki.moveBackward(14);
  sparki.moveForward(7);
  sparki.RGB(RGB_RED);
  sparki.beep();
    }
    sparki.clearLCD();
sparki.drawString(10,2, "Shake my hand to play a game");
sparki.updateLCD();
delay(3000);
int shake=analogRead(8);
 sparki.print(shake);
      sparki.updateLCD();
      delay(1000);
       if(shake>100)
      {
      sparki.clearLCD();
      sparki.drawString(20,2, "Let's play robot or human!");
      sparki.updateLCD();
      delay(2000);
        sparki.clearLCD();
      sparki.drawString(10,1, "The rules are easy!");// Robot tries to catches human, and human tries to escape robot! I will be...");
      sparki.updateLCD();
      delay(2000);
      sparki.clearLCD();
       sparki.drawString(10,1, "Robot tries to catch the human...");// and human tries to escape robot! I will be...");
      sparki.updateLCD();
      delay(2000);
      sparki.clearLCD();
       sparki.drawString(10,1, "... and the human tries to escape the robot!");
      sparki.updateLCD();
      delay(2000);
      sparki.clearLCD();
      sparki.beep();
      randomSeed(analogRead(1));
  int responserh = random(1, 10);
if (responserh<=5)
{
   sparki.clearLCD();
      sparki.drawString(20,2, "I will be the robot");
      sparki.updateLCD();
      delay(2000);
      sparki.beep();
      for (int i = 0; i < 5; i=i+1)
      {
        pinMode (3, INPUT); //Sensor output
  sparki.clearLCD();
sparki.print ("Sensor: ");
 sparki.println (digitalRead(3)); //print the sensor output
 sparki.updateLCD();
 delay (1500); 
 if (digitalRead(3) >0 )
 {
   sparki.moveRight(10);
   sparki.clearLCD();
      sparki.drawString(20,2, "Where is that silly human??");
      sparki.updateLCD();
      delay(1000);
 }
 if (digitalRead(3) <1 )
 {
   sparki.moveForward();
   sparki.clearLCD();
      sparki.drawString(20,2, "robot will catch you!");
      sparki.updateLCD();
      delay (1000);
 }
       sparki.clearLCD();
      sparki.drawString(20,2, "I'm a good robot!");
      sparki.updateLCD();
      delay(2000);
}
}
if (responserh>5)
{
   sparki.clearLCD();
      sparki.drawString(20,2, "I will be the human");
      sparki.updateLCD();
      delay(2000);
      sparki.beep();
      for (int i = 0; i < 5; i=i+1)
      {
      pinMode (3, INPUT); //Sensor output
  sparki.clearLCD();
sparki.print ("Sensor: ");
 sparki.println (digitalRead(3)); //print the sensor output
 sparki.updateLCD();
 delay (1500); 
 while (digitalRead(3) >0 )
 {
   sparki.moveForward();
   sparki.clearLCD();
      sparki.drawString(20,2, "Yey! escape!");
      sparki.updateLCD();
      delay(1000);
 }
 while (digitalRead(3) <1 )
 {
   sparki.moveRight(10);
        sparki.clearLCD();
      sparki.drawString(20,2, "Uh, oh");
      sparki.updateLCD();
      delay(1000);
      }
       sparki.clearLCD();
      sparki.drawString(20,2, "Human is free! Woo!");
      sparki.updateLCD();
      delay(2000);
}
}
      }
sparki.clearLCD();
      sparki.drawString(20,2, "Sparki will wander now");
      sparki.updateLCD();
      delay(2000);
      sparki.beep();
      sparki.RGB(RGB_BLUE);
      for (int i = 0; i < 5; i=i+1)
      {
      sparki.servo(0);
       pinMode (3, INPUT); //Sensor output
  sparki.clearLCD();
sparki.print ("Sensor: ");
 sparki.println (digitalRead(3)); //print the sensor output
 sparki.updateLCD();
 delay (1500); 
 if (digitalRead(3) >0 )
 {
   sparki.moveForward(10);
 }
 if (digitalRead(3) <1 )
 {
   sparki.moveRight(90);
 }
   
 
}
  
       sparki.clearLCD();
      sparki.drawString(20,2, "Sparki is back!");
      sparki.updateLCD();
      delay(2000);
        
    
    }



  





  

Feel free to make improvements and use this code if you want
Hope you like it
Ash :slight_smile: