Post by mwclarkson on Feb 8, 2014 11:59:44 GMT -8
For my first project that wasn't copied and pasted from the excamera site I wrote a Magic 8-Ball simulator.
I'm sure it's not the most efficient, and I've only got 2 randomly selected phrases for now but the hard work is all done.
I'm proud that I managed to master the accelerometer sensor, though, and a timer stops you activating the routine by moving the Gameduino slowly.
#include <SPI.h>
#include <GD2.h>
int maxX,maxY,maxZ;
int minX, minY, minZ;
int rangeX,rangeY,rangeZ;
int timer;
void setup()
{
Serial.begin(9600);
timer = 0;
GD.begin();
}
void loop()
{
long randNumber;
GD.Clear();
// Every 10 steps, reset the min and max values so only a sudden shake is registered
timer++;
if (timer > 10) {
maxX = minX = maxY = minY = maxZ = minZ = 0;
timer = 0;
}
// Get accelerometer readings and update the max and min values
GD.get_inputs();
int x,y,z;
GD.get_accel(x,y,z);
if (x > maxX) { maxX = x; }
if (x < minX) { minX = x; }
if (y > maxY) { maxY = y; }
if (y < minY) { minY = y; }
if (z > maxZ) { maxZ = z; }
if (z < minZ) { minZ = z; }
rangeX = maxX - minX;
rangeY = maxY - minY;
rangeZ = maxZ - minZ;
// If a sudden shake is detected, pick a random message
if ((rangeX > 300) or (rangeY > 300) or (rangeZ > 300)) {
maxX = minX = maxY = minY = maxZ = minZ = 0;
GD.Clear();
GD.cmd_text(240,136,28,OPT_CENTER,"Interrogating mystical energies");
GD.swap();
delay(3000);
GD.Clear();
// Only two options right now but trivial to expand
randNumber = random(2);
if (randNumber == 0) {
GD.cmd_text(240,136,31,OPT_CENTER,"The answer is no!");
} else {
GD.cmd_text(240,136,31,OPT_CENTER,"Absomalutely");
}
GD.swap();
delay(3000);
timer = 0;
} else {
GD.cmd_text(240,136,28,OPT_CENTER,"Shake the magic 8-ball");
GD.swap();
}
}
I'm sure it's not the most efficient, and I've only got 2 randomly selected phrases for now but the hard work is all done.
I'm proud that I managed to master the accelerometer sensor, though, and a timer stops you activating the routine by moving the Gameduino slowly.
#include <EEPROM.h>#include <SPI.h>
#include <GD2.h>
int maxX,maxY,maxZ;
int minX, minY, minZ;
int rangeX,rangeY,rangeZ;
int timer;
void setup()
{
Serial.begin(9600);
timer = 0;
GD.begin();
}
void loop()
{
long randNumber;
GD.Clear();
// Every 10 steps, reset the min and max values so only a sudden shake is registered
timer++;
if (timer > 10) {
maxX = minX = maxY = minY = maxZ = minZ = 0;
timer = 0;
}
// Get accelerometer readings and update the max and min values
GD.get_inputs();
int x,y,z;
GD.get_accel(x,y,z);
if (x > maxX) { maxX = x; }
if (x < minX) { minX = x; }
if (y > maxY) { maxY = y; }
if (y < minY) { minY = y; }
if (z > maxZ) { maxZ = z; }
if (z < minZ) { minZ = z; }
rangeX = maxX - minX;
rangeY = maxY - minY;
rangeZ = maxZ - minZ;
// If a sudden shake is detected, pick a random message
if ((rangeX > 300) or (rangeY > 300) or (rangeZ > 300)) {
maxX = minX = maxY = minY = maxZ = minZ = 0;
GD.Clear();
GD.cmd_text(240,136,28,OPT_CENTER,"Interrogating mystical energies");
GD.swap();
delay(3000);
GD.Clear();
// Only two options right now but trivial to expand
randNumber = random(2);
if (randNumber == 0) {
GD.cmd_text(240,136,31,OPT_CENTER,"The answer is no!");
} else {
GD.cmd_text(240,136,31,OPT_CENTER,"Absomalutely");
}
GD.swap();
delay(3000);
timer = 0;
} else {
GD.cmd_text(240,136,28,OPT_CENTER,"Shake the magic 8-ball");
GD.swap();
}
}