Entertainment bot improvements

So in an earlier post I shared my entertainment bot code, which included:

  • a version of robot snap
  • some snazzy dance moves
  • some random sentences (to appear conversational) displaying on the LCD
  • along with the traditional flashing LED’s
  • random bleeps and beeps

All in all, this code turns Sparki into the generic toy robot everybody wants to play with.

DSC_0464.JPG

Recently I have been enjoying creating the more practical codes for timers, but on returning to this more playful code I have had some improvements to make.

Check out the full, improved code below and let me know what you think πŸ™‚

 

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

void setup()
{
 sparki.servo(60);
 sparki.clearLCD();
 sparki.drawString(10, 1, "Hello human");
 sparki.updateLCD();
 delay(3000);
 sparki.beep();
 sparki.RGB(255,0,0);
 sparki.clearLCD();
 sparki.drawString(10, 1, "beginning search");
 sparki.updateLCD();
 delay(3000);
 sparki.beep();
 sparki.clearLCD();
 sparki.drawString(10, 1, "looking for human");
 sparki.updateLCD();
 delay(3000);
 sparki.ping();
 delay(1000);
 if (sparki.ping() <10)
 {
 sparki.moveForward(10);
 sparki.clearLCD();
sparki.drawString(10,2, "yey friends!");
sparki.updateLCD();
delay(3000);}
if (sparki.ping()>10)
{
 sparki.moveLeft(30);
 sparki.moveRight(30);
 sparki.beep(840, 400);
 delay(200);
 sparki.beep(640, 400);
 delay(200);
 sparki.beep(440, 400);
 delay(200);
 sparki.beep(340, 400);
}
 for (int i = 0; i < 5; i=i+1)
 {
dance();
delay(1000);
entertainment1();
delay(1000);
dance2();
delay(1000);
conversation();
delay(1000);
melody();
delay(1000);
robotSnap();
delay(1000);
rainbow();
delay(1000);
conversation();
delay(1000);
dance();
delay(1000);
entertainment1();
delay(1000);
melody();
delay(1000);
rockPaperScissors();
delay(1000);
dance2();
delay(1000);
rainbow();
delay(1000);
conversation();
delay(1000);

}
}
void dance()
{
 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);
}
void dance2()
{
 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);
}
 
void rainbow()
{
 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);
 }
void melody()
{
 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);
}
void entertainment1()
{
 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)))
 {rainbow();} 
 else
 {
 delay(1000);
 }
 if(((lightLeft>lightRight)&&(lightCenter>lightRight)))
 {
 melody(); 
 
 }
 else
 {
 delay(1000);
 }
}
void conversation()
{
 randomSeed(analogRead(1));
 int response = random(0, 15);
 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);
 dance();
 }
 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 if((response>=9) && (response <=12))
 {
 sparki.clearLCD();
sparki.drawString(10,2, "I will be kind to you when we robots take over the world");
sparki.updateLCD();
delay(3000);
dance2();
 }
 
else if((response>=9) && (response <=12))
{
 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 
 {
 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();
 }
}
void robotSnap()
{
 sparki.clearLCD();
 sparki.drawString(20,2,"Lets play robot snap");
 sparki.updateLCD();
 delay(3000);
 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);
 if(sparki.lightCenter()>sparki.lightRight())
 
 {
 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);
 if(sparki.lightCenter()>sparki.lightRight())
 {
 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);
 if (sparki.lightCenter()>sparki.lightRight())
 {
 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);
 }
 }
 }
 }
 void rockPaperScissors()
 {
 sparki.clearLCD();
sparki.drawString(20,2,"Let's play rock paper scissors?");
sparki.updateLCD();
delay(1000);
sparki.clearLCD();
sparki.drawString(20,2,"Choose RIGHT for yes and LEFT for no");
sparki.updateLCD();
delay(5000);
if (sparki.lightRight()<sparki.lightLeft())
 {
 randomSeed(analogRead(1));
 int response = random(1, 4);
 if (response==1)
 {sparki.clearLCD();
 sparki.drawString(10, 1, "Here is your randomly generated choice");
 sparki.updateLCD();
 delay(2000);
 sparki.clearLCD();
 randomSeed(analogRead(1));
 int response1a = random(1, 10);
 if (response1a<=4)
 {
 sparki.clearLCD();
 sparki.print("rock");
 sparki.updateLCD();
 delay(1000);
 }
 else if ((response1a>4) && (response1a<7))
 {
 sparki.clearLCD();
 sparki.print("paper");
 sparki.updateLCD();
 delay(1000);
 }
 else if (response1a<=7)
 {sparki.clearLCD();
 sparki.print("scissors");
 sparki.updateLCD();
 delay(1000);
 }
 
 sparki.clearLCD();
 sparki.drawString(10, 1, "This is my choice");
 sparki.updateLCD();
 delay(1000);
 randomSeed(analogRead(1));
 int response2b = random(1, 3);
 
 if (response2b==1)
 {
 sparki.clearLCD();
 sparki.print("rock");
 sparki.updateLCD();
 delay(1000);
 }
 else if (response2b==2)
 {
 sparki.clearLCD();
 sparki.print("paper");
 sparki.updateLCD();
 delay(1000);
 }
 else if (response2b==3)
 {sparki.clearLCD();
 sparki.print("scissors");
 sparki.updateLCD();
 delay(1000);
 }
 if ((response1a<=4) && (response2b==3))
 {
 sparki.clearLCD();
 sparki.drawString(20,2, "Well done you won");
 sparki.updateLCD();
 sparki.beep();
 delay(2000);
 }
 else if ((response1a<=4) && (response2b==2))
 {
 sparki.clearLCD();
 sparki.drawString(20,2, "Yey! I won!");
 sparki.updateLCD();
 delay(1000);
 }
 else if ((response1a<=4) && (response2b==1))
 {
 sparki.clearLCD();
 sparki.drawString(20,2, "It was a draw!");
 sparki.updateLCD();
 delay(2000);
 }
 if (((response1a>4) && (response1a<7) && (response2b==1)))
 {
 sparki.clearLCD();
 sparki.drawString(20,2, "Well done you won");
 sparki.updateLCD();
 sparki.beep();
 delay(2000);
 }
 else if (((response1a>4) && (response1a<7) && (response2b==3)))
 {
 sparki.clearLCD();
 sparki.drawString(20,2, "Yey! I won!");
 sparki.updateLCD();
 delay(1000);
 }
 else if (((response1a>4) && (response1a<7) && (response2b==2)))
 {
 sparki.clearLCD();
 sparki.drawString(20,2, "It was a draw!");
 sparki.updateLCD();
 delay(2000);
 }
 if ((response1a>=7) && (response2b==2))
 {
 sparki.clearLCD();
 sparki.drawString(20,2, "Well done you won");
 sparki.updateLCD();
 sparki.beep();
 delay(2000);
 }
 else if ((response1a>=7) && (response2b==1))
 {
 sparki.clearLCD();
 sparki.drawString(20,2, "Yey! I won!");
 sparki.updateLCD();
 delay(1000);
 }
 else if ((response1a>=7) && (response2b==3))
 {
 sparki.clearLCD();
 sparki.drawString(20,2, "It was a draw!");
 sparki.updateLCD();
 delay(2000);
 }
}
 }
 }
 void loop()
 {}
 
Advertisements

2 thoughts on “Entertainment bot improvements

  1. This is so, so, soooo incredibly awesome! Completely loving it!

    Was taking a look at this bit of code, thinking it could be abstracted to its own method:

    sparki.clearLCD();
    sparki.drawString(10, 1, “Hello human”);
    sparki.updateLCD();

    Looks like the LCD is cleared every time before drawString is called (followed by updateLCD)? So maybe you could wrap it all up in a nice package perhaps:

    sparki.servo(60);
    writeToLCD(10, 1, “Hello human”);
    delay(3000);


    // Method definition *** Sorry, have my C# head on here! ***
    void writeToLCD(int xPixel, int yLine, string textToDisplay)
    {
    sparki.clearLCD();
    // Had to read up on the drawString signature, so hopefully this is correct
    sparki.drawString(xPixel, yLine, textToDisplay);
    sparki.updateLCD();
    }

    This is, of course coming from someone who has done absolutely zero Arduino/robotics-based stuff so feel free to ignore.

    Big thumbs up anyway and keep up the good work!

    Liked by 1 person

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