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

Touch sensor Part 2

Hey guys

So last week I explained why I bought a touch sensor, how I connected it up to Sparki and how I used the touch sensor to increase the opportunities for direct physical communication and interaction with Sparki. You can find the article here.

This week I’ll explain how I used the touch sensor to help Sparki navigate around the room. As some of you may know, my ultrasonic range finder is currently not working (https://ashbotandsparki.wordpress.com/2015/08/18/sparkis-new-eyes/) so for the time being Sparki has been unable to be autonomous in his movements…until now!

I moved Sparki’s gripper in so that it could be used to support the touch sensor at the very front of Sparki’s body (which is held in place by the very technical blu-tac) and then I wrote a program that would allow Sparki to move Forward regular intervals unless he was touching an obstacle. At which point he reverses 5 and moves Right 90 degree angle:

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

void setup()
{
}
void loop()
{
  sparki.servo(0);
         int touch= analogRead(8);
      sparki.print(touch);
      sparki.updateLCD();
      delay(1000);
      if(touch<100)
      {
       sparki.moveForward(25); 
      }
      else
      {
        sparki.moveBackward(5);
        sparki.moveRight(90);
      }
}
       

As you can tell this is a really simple code but it works effectively to give Sparki some degree of autonomy for now and is a great, practical use for a touch sensor 🙂

Of course by setting up each turn (when Sparki had encountered an obstacle) as a different variable, it would be possible to complete much more complex navigating programs with this touch sensor.

DSC_0468

I hope that Sparki’s exploration of touch sensors was helpful!? Let me know what you think 🙂

Touch sensor Part 1

So as you can probably tell from my emphasis on entertainment robots, communication and interaction with robotics is one of my favourite aspects and so when it came to purchasing additional sensors for Sparki I went straight for those that would increase the level of human physical interaction.

So I purchased a touch sensor.

416ogj2n4UL

This is pretty awesome in that it only has 3 pins and connects easily to Sparki’s expansion port.

GND to the GND port, VCC to the VCC port and one labelled SIG. Now the output can either be connected to one of Sparki’s analogue ports, or it can be connected to one of the digital ports, either way it still works fine.

Expansion-Port-Diagram
Sparki’s Expansion Port diagram from http://arcbotics.com/products/sparki/parts/expansion-port/

So what did I use this new sensor for??? Well two things mainly and you will notice this is part 1 so I will explain the other use in part 2.

My first aim was to be able to physically communicate and interact with Sparki using this new additional sensor and so I wrote a really simple looping code (that gets a bit boring!) in which touching Sparki’s sensor invokes one of three reactions, much like the use of touch sensor in children’s electronic toys. Here is the code:

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

void setup()
{
  sparki.servo(0);
}

void loop()
{
sparki.beep();
delay(250);
sparki.beep();
sparki.clearLCD();
sparki.drawString(10, 1, "Hello, human");
sparki.updateLCD();
delay(1000);
sparki.clearLCD();
sparki.drawString(10, 1, "Shake my hand");
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, "Nevermind");
sparki.updateLCD();
delay(3000);
      }
           for (int i = 0; i < 5; i=i+1)
  {
      sparki.clearLCD();
sparki.drawString(10, 1, "Let's shake again");
sparki.updateLCD();
delay(3000);
int shake=analogRead(8);
 sparki.print(shake);
      sparki.updateLCD();
      delay(1000);
       if(shake>100)
      {
      randomSeed(analogRead(1));
  int response = random(1, 10);
  sparki.clearLCD();
  sparki.print(response);
  sparki.updateLCD();
  delay(1000);
  if (response<=3)
  {
    sparki.clearLCD();
  sparki.drawString(20,2,"This is my happy song");
  sparki.updateLCD();
  delay(2000);
      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);
  }
  if ((response>3) && (response<=6))
  {
    sparki.clearLCD();
  sparki.drawString(10, 1, "Ok lets see who gets the highest number");
  sparki.updateLCD();
  delay(2000);
  sparki.clearLCD();
  sparki.drawString(10, 1, "1 2 3 4 5 6 7 8 9 10");
  sparki.updateLCD();
  delay(2000);
  sparki.clearLCD();
  sparki.drawString(10, 1, "Here is your number");
  sparki.updateLCD();
  delay(2000);
  sparki.clearLCD();
  randomSeed(analogRead(1));
  int response1 = random(1, 10);
  sparki.clearLCD();
  sparki.print(response1);
  sparki.updateLCD();
  delay(1000);
   sparki.clearLCD();
  sparki.drawString(10, 1, "This is my number");
  sparki.updateLCD();
  delay(1000);
   randomSeed(analogRead(1));
  int response2 = random(1, 10);
  sparki.clearLCD();
  sparki.print(response2);
  sparki.updateLCD();
  delay(1000);
  if (response1>response2)
  {
    sparki.clearLCD();
    sparki.drawString(20,2, "Well done your number was the highest");
    sparki.updateLCD();
    sparki.beep();
    delay(2000);
  }
  if (response1<response2)
  {
        sparki.clearLCD();
    sparki.drawString(20,2, "Yey! I won! Victory dance!");
    sparki.updateLCD();
    delay(1000);
    sparki.beep();
  sparki.moveRight(14);
  sparki.moveLeft(7);
  
  sparki.moveLeft(7);
  sparki.moveRight(14);
  sparki.moveLeft(7);
  sparki.beep();
  sparki.moveLeft(7);
  sparki.moveRight(14);
  
  }
  if (response1==response2)
  {
      sparki.clearLCD();
    sparki.drawString(20,2, "It was a draw!");
    sparki.updateLCD();
    delay(2000);
  }
}
if (response>6)
{
  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
      {
          sparki.clearLCD();
sparki.drawString(10,2, "Nevermind");
sparki.updateLCD();
delay(3000);
      }
  }
}

Obviously this code could be improved greatly but it gives a clear indication of my goals for Sparki. I will be using similar code to work on my conversation and entertainment robot ideas that you will see in a few weeks, particularly paired with the sound sensor I also purchased.

There is no video of this code (it would be pretty hard to film and use the touch sensor simultaneously) but here is a photo of the connected sensor 🙂

DSC_0427