Conversation and entertainment bot

Hi everybody

So this code puts together a lot of things I have been writing about including the ir sensor as a way for Sparki to “see” and the touch sensor….unfortunately my sound sensor has broken (electrocuting me in the process!)

sparki2

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

void setup()
{
 sparki.servo(0);
   sparki.clearLCD();
  sparki.drawString(10, 1, "Hello human ");
  sparki.updateLCD();
  delay(1000);
    sparki.clearLCD();
  sparki.drawString(10, 1, "time for a bot hug");
  sparki.updateLCD();
  delay(1000);
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);}
}
   
    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);
		}
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);
  sparki.RGB(RGB_GREEN);
  sparki.moveLeft(7);
  sparki.beep();
  sparki.moveRight(14);
  sparki.moveLeft(7);
  sparki.RGB(RGB_RED);

      }
      
		if(edgeLeft>1)
		{
			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);
		}

  randomSeed(analogRead(1));
  int response1 = random(0, 10);
  sparki.clearLCD();
  sparki.print(response1);
  sparki.updateLCD();
  delay(1000);
  if((response1>=0) && (response1 <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((response1>=3) && (response1 <=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();
    }
    if(edgeRight>1)
		{
			sparki.RGB(220,0,0);
			sparki.beep(740, 200);
			sparki.moveRight(90);
			sparki.beep(840, 400);
			sparki.moveLeft(90);
			sparki.beep(440, 400);
			delay(100);
			sparki.beep(740, 200);
			sparki.RGB(0,0,220);
			sparki.beep(640, 200);
			sparki.moveBackward(5);
			delay(200);
			sparki.beep(440, 400);
			delay(200);
			sparki.beep(740, 500);
			sparki.moveForward(5);
			delay(200);
		}
	
		if(edgeRight>600)
		{
			
                        sparki.beep(440, 600);
			delay(400);
			sparki.beep(640, 400);
			delay(400);
			sparki.beep(740, 200);
			delay(200);
			sparki.beep(440, 200);
			delay(200);
			sparki.beep(640, 400);
			delay(400);
			sparki.beep(540, 200);
			delay(400);
			sparki.beep(740, 200);
}
    sparki.clearLCD();
sparki.drawString(10,2, "lets play a game");
sparki.updateLCD();
delay(3000);
randomSeed(analogRead(1));
  int response2 = random(1, 3);
  if (response2==1)
  {
  sparki.clearLCD();
  sparki.drawString(20,2,"Lets play robot snap");
  sparki.updateLCD();
  delay(2000);
  for (int i = 0; i < 5; i=i+1)
  {
  randomSeed(analogRead(1));
  int responsea = random(1, 3);
  if (responsea==1)
  {
    sparki.clearLCD(); // wipe the LCD clear
    sparki.drawRect(5,5,30,10); //small empty rectangle in upper-left
    sparki.updateLCD(); // put the drawings on the screen
    delay(500);
    sparki.clearLCD();
  }
  if (responsea==2)
  {
    sparki.clearLCD(); // wipe the LCD clear
    sparki.drawCircle(10,10,5); //small empty circle in upper-left
    sparki.updateLCD(); // put the drawings on the screen
    delay(500);
    sparki.clearLCD();
    
   
  }
  if (responsea==3)
  {
    sparki.clearLCD(); // wipe the LCD clear
    sparki.drawCircleFilled(90,40,20); // big filled circle in lower-right
    sparki.updateLCD(); // put the drawings on the screen
    delay(500);
    sparki.clearLCD();
    
  }
  randomSeed(analogRead(1));
  int responseb = random(1, 3);
  if (responseb==1)
  {
    sparki.clearLCD(); // wipe the LCD clear
    sparki.drawRect(5,5,30,10); //small empty rectangle in upper-left
    sparki.updateLCD(); // put the drawings on the screen
    delay(500);
     sparki.clearLCD();
    
  }
  if (responseb==2)
  {
    sparki.clearLCD(); // wipe the LCD clear
    sparki.drawCircleFilled(90,40,20); //small empty circle in upper-left
    sparki.updateLCD(); // put the drawings on the screen
    delay(500);
     sparki.clearLCD();
  
  }
  if (responseb==3)
  {
    sparki.clearLCD(); // wipe the LCD clear
    sparki.drawCircle(10,10,5); 
    sparki.updateLCD(); // put the drawings on the screen
    delay(500);
     sparki.clearLCD();
  }
  if ((responsea==1) && (responseb==1))
  {
    delay(1000);
    int shake=analogRead(8);
       if(shake>100)
      {
        sparki.clearLCD();
        sparki.drawString(20,2,"Well done! You were first");
        sparki.updateLCD();
        delay(1000);
      }
      else
      {
        sparki.clearLCD();
        sparki.drawString(20,2,"Snap! I win!");
        sparki.updateLCD();
        delay(1000);
      }
  }
   if ((responsea==2) && (responseb==2))
  {
    delay(1000);
    int shake=analogRead(8);
       if(shake>100)
      {
        sparki.clearLCD();
        sparki.drawString(20,2,"Well done! You were first");
        sparki.updateLCD();
        delay(1000);
      }
      else
      {
        sparki.clearLCD();
        sparki.drawString(20,2,"Snap! I win!");
        sparki.updateLCD();
        delay(1000);
      }
  }
   if ((responsea==3) && (responseb==3))
  {
    delay(1000);
    int shake=analogRead(8);
       if(shake>100)
      {
        sparki.clearLCD();
        sparki.drawString(20,2,"Well done! You were first");
        sparki.updateLCD();
        delay(1000);
      }
      else
      {
        sparki.clearLCD();
        sparki.drawString(20,2,"Snap! I win!");
        sparki.updateLCD();
        delay(1000);
      }
  }
  }
  }
  if ((response2==2)|| (response2==3))
  {
    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);
}
}
      }

    
}


Advertisements

infrared obstacle detector

SKU072960 (1)
So my Sparki's ultra-sonic range finder is still broke unfortunately but I have purchased an infrared sensor that can be used for object detection. 

This brings back a sense of autonomy for Sparki as he can now navigate the rooms, making decisions about turns.

The following code allows Sparki to turn Right at very short intervals if his way forward is blocked:

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

void setup() {
 
 pinMode (3, INPUT); //Sensor output
}
void loop() {
  sparki.clearLCD();
sparki.print ("Sensor: ");
 sparki.println (digitalRead(3)); //print the sensor output
 sparki.updateLCD();
 delay (1500); 
 while (digitalRead(3) <0 )
 {
   sparki.moveForward();
 }
 while (digitalRead(3) >1 )
 {
   sparki.moveRight(10);
 }
   
 
}