Sparki accelerometer shake game

So one of the things I haven’t really explored with Sparki is the accelerometer. But as I recently got my hands on a Sphero Mini, I’ve been enjoying learning about this sensor.

So the accelerometer works on a 3 axis sensor. X, Y and Z.  X measures the left/right acceleration, Y measures the forward/backward acceleration, and Z measures the up/down acceleration.

Here is a little game I made using the data from the X sensor:

 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(5000);
 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);
 
delay(10000);
}

Here is a link to a video of the code in action: https://www.youtube.com/watch?v=a2HxYJC1TaQ

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