Games and free roam

So what do you need from an entertainment robot? Well, that’s easy: entertainment! So games are a great place to start. But I also like my robots to be autonomous so I can switch them on and leave them roaming around without my supervision/interaction.

So I put together this program, so you can switch between modes based on what you want. Using the touch sensor as a way to input your choice, based on what the LCD screen says.
#include <Sparki.h> // include the sparki library

void setup()
{

sparki.servo(90);

}

void freeRoam(){
while(sparki.ping()>20)
{
sparki.moveForward(
);
}
sparki.moveStop();
sparki.moveRight(90);
delay(1000);
int R=sparki.ping();
sparki.ping();
delay(2000);
sparki.clearLCD();
sparki.println(R);
sparki.updateLCD();
delay(1000);
sparki.moveLeft(180);
delay(1000);
int L = sparki.ping();
sparki.ping();
delay(2000);
sparki.clearLCD();
sparki.println(L);
sparki.updateLCD();
delay(1000);
sparki.moveRight(90);

if((R>10) && (R>=L))
{
sparki.moveRight(90);

}
if ((L>10) && (L>=R))
{
sparki.moveLeft(90);

}
if ((R<=10) && (L <= 10))
{
sparki.moveRight(180);
sparki.RGB(0,0,220);

}
}

void magic8ball()
{ sparki.clearLCD();
sparki.drawString(10,1,”Hello human”);
sparki.drawString(10,3,”Shake me to answer all”);
sparki.drawString(10,5,”your questions”);
sparki.updateLCD();
delay(5000);

float x = sparki.accelX();
float y = sparki.accelY();
float z = sparki.accelZ();

if ((y>2)||(y<=-2))
{
randomSeed(analogRead(1));
int response1 = random(1,4);
if (response1==1)
{
sparki.beep(440,100);
delay(500);
sparki.beep(440,100);
sparki.RGB(100,0,0);
sparki.clearLCD();
sparki.drawString(10,1,”Absolutely not!”);
sparki.updateLCD();
delay(2000);
}
else if(response1==2)
{
sparki.beep(240,100);
delay(500);
sparki.beep(240,100);
sparki.RGB(0,100,0);
sparki.clearLCD();
sparki.drawString(10,1,”Always!”);
sparki.updateLCD();
delay(2000);
}
else if(response1==3)
{
sparki.beep(640,100);
delay(500);
sparki.beep(640,100);
sparki.RGB(0,0,100);
sparki.clearLCD();
sparki.drawString(10,1,”It’s possible”);
sparki.updateLCD();
delay(2000);
}
else
{
sparki.beep(640,100);
delay(500);
sparki.beep(640,100);
sparki.RGB(0,0,100);
sparki.clearLCD();
sparki.drawString(10,1,”Maybe…”);
sparki.updateLCD();
delay(2000);
}}
}

void attackMode()
{

sparki.clearLCD();
sparki.drawString(10,2, “Attack mode”);
sparki.drawString(10,5,”activated”);
sparki.updateLCD();
delay(2000);
sparki.beep();
sparki.ping();
while(sparki.ping()>10)
{
sparki.moveRight();
sparki.clearLCD();
sparki.drawString(20,2, “Searching…”);
sparki.updateLCD();
delay (1000);
}
sparki.moveForward(10);
sparki.clearLCD();
sparki.drawString(20,2, “robot will catch you!”);
sparki.updateLCD();
delay (1000);
}
void shakeGame()
{
sparki.clearLCD();
sparki.drawString(10,1,”Hello human”);
sparki.drawString(10,3,”shake me when you see the”);
sparki.drawString(10,5,”GREEN LIGHT”);
sparki.updateLCD();
delay(3000);
int score=0;
int counter1=0;
while (counter1<10)
{
randomSeed(analogRead(1));
int response1 = random(1,4);
int counter=0;
while(counter<response1)
{
sparki.RGB(100,0,0); // Make the LED maximum Red
delay(1000);
sparki.RGB(0,0,100); // Make the LED maximum Blue
delay(1000);
counter=counter+1;
}
sparki.RGB(0,100,0); // Make the LED maximum Green
delay(1000);

if(sparki.accelX() <0)
{
score=score+1;
sparki.clearLCD();
sparki.print(score);
sparki.updateLCD();
delay(1000);
}
counter1=counter1+1;
}
sparki.RGB(RGB_OFF);
sparki.clearLCD();
sparki.drawString(10,1,”Well done!”);
sparki.drawString(10,3,”You scored”);
sparki.updateLCD();
delay(1000);
sparki.clearLCD();
sparki.print(score);
sparki.updateLCD();
delay(1000);
}

void loop(){
delay(5000);
int shake=analogRead(8);
sparki.clearLCD();
sparki.drawString(10,1,”Hello human”);
sparki.drawString(10,3,”touch for free roam”);
sparki.updateLCD();
delay(2000);
if (shake>100)
{
for (int z2 = 0; z2 < 10; z2=z2+1) {
freeRoam();
}
}
else{
sparki.clearLCD();
sparki.drawString(10,3,”touch for”);
sparki.drawString(10,5,”attack mode”);
sparki.updateLCD();
delay(3000);
analogRead(8);
if (analogRead(8)>100)
{
for (int z = 0; z < 3; z=z+1) {
attackMode();
}
}
else{
sparki.clearLCD();
sparki.drawString(10,3,”touch for”);
sparki.drawString(10,5,”magic 8″);
sparki.updateLCD();
delay(3000);
analogRead(8);
if (analogRead(8)>100)
{
for (int z1 = 0; z1 < 3; z1=z1+1) {
magic8ball();
}
}
else{
sparki.clearLCD();
sparki.drawString(10,3,”touch for”);
sparki.drawString(10,5,”shake game”);
sparki.updateLCD();
delay(3000);
analogRead(8);
if (analogRead(8)>100)
{
shakeGame();
}
}}}}

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