So using the new sound sensor and the new touch sensor added to Sparki I wrote a pretty basic code to turn Sparki into an interactive gamebot…where the games are based on interaction with Sparki and info he receives via his various sensors.
This is a really fun little program and its cool to think about how stuff like this actually gets used in the real world in various interactive toys for children. Poochi for example was my first robotic pet, as well as the notorious furby!
So anyway here is the code:
#include <Sparki.h> // include the sparki library void setup() { } void loop() { sparki.clearLCD(); sparki.drawString(20,2,"Would you like to play a game?"); sparki.updateLCD(); delay(1000); int sound1 = analogRead(0); delay (2000); sparki.clearLCD(); // print out the value you read: sparki.print(sound1); sparki.updateLCD(); delay (1000); int sound2=analogRead(0); delay(2000); sparki.print(sound2); sparki.updateLCD(); delay (1000); int sound3 = analogRead(0); delay (2000); sparki.clearLCD(); // print out the value you read: sparki.print(sound3); sparki.updateLCD(); delay (1000); if((sound2> 1.26) || (sound3>1.26)) { randomSeed(analogRead(1)); int response = random(1, 16); if (response<=3) { sparki.clearLCD(); sparki.drawString(20,2, "Ok let's play rock, paper, scissors"); sparki.updateLCD(); delay(2000); sparki.clearLCD(); sparki.drawString(20,2, "Here is your random choice"); sparki.updateLCD(); delay(2000); randomSeed(analogRead(1)); int response1a = random(1, 10); if (response1a<=4) { sparki.clearLCD(); sparki.drawString(20,2, "rock"); sparki.updateLCD(); delay(1000); } if ((response1a>4)&&(response1a<=7)) { sparki.clearLCD(); sparki.drawString(20,2, "paper"); sparki.updateLCD(); delay(1000); } if (response1a>7) { sparki.clearLCD(); sparki.drawString(20,2, "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); } } 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, "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) && (response<=12)) { sparki.clearLCD(); sparki.drawString(20,2, "Let's play robot or human!"); sparki.updateLCD(); delay(2000); sparki.clearLCD(); sparki.drawString(20,2, "Robot catches human"); sparki.updateLCD(); delay(2000); sparki.clearLCD(); sparki.drawString(20,2, "Human tries to escape robot!"); sparki.updateLCD(); delay(2000); sparki.beep(); randomSeed(analogRead(1)); int responserh = random(1, 10); if (responserh<=5) { sparki.clearLCD(); sparki.drawString(20,2, "I will be robot"); sparki.updateLCD(); delay(2000); sparki.beep(); for (int i = 0; i < 5; i=i+1) { sparki.servo(0); sparki.ping(); delay(2000); if(sparki.ping()<10) { sparki.moveForward(10); sparki.clearLCD(); sparki.drawString(20,2, "robot will catch you!"); sparki.updateLCD(); delay (1000); } else { sparki.moveRight(90); sparki.clearLCD(); sparki.drawString(20,2, "Where is that silly human??"); sparki.updateLCD(); } 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 human"); sparki.updateLCD(); delay(2000); sparki.beep(); for (int i = 0; i < 5; i=i+1) { sparki.servo(0); sparki.ping(); delay(2000); if(sparki.ping()>10) { sparki.moveForward(10); sparki.clearLCD(); sparki.drawString(20,2, "Yey! escape!"); sparki.updateLCD(); delay(1000); } else { sparki.moveRight(90); 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); } } } if (response>12) { 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); } } } } } }