Description of Code:
Displays current & max readings for x, y & z Accelerometer readings.
Sparki Components Utilised:
Accelerometer
Remote
Wheels
LCD
[code]/*******************************************
IR Remote
Sparki has a sensor that lets it receives
commands from the included remote control.
Try moving it around!
********************************************/
#include <Sparki.h> // include the sparki library
float x_min = 1023;
float x_max = 0;
float y_min = 1023;
float y_max = 0;
float z_min = 1023;
float z_max = 0;
void setup()
{
}
// /------^-----
// | |
// | 69 70 71 |
// | 68 64 67 |
// | 7 21 9 |
// | 22 25 13 |
// | 12 24 94 |
// | 8 28 90 |
// | 66 82 74 |
// ____________/
void loop()
{
int code = sparki.readIR();
//if(code != -1){
//sparki.print("Received code: ");
//sparki.println(code);
sparki.clearLCD();
//acelerometer:
/*
float x = sparki.accelX(); // measure the accelerometer x-axis
float y = sparki.accelY(); // measure the accelerometer y-axis
float z = sparki.accelZ() + 9.81; // measure the accelerometer z-axis. The +9.81 is to offset reading gravity.
*/
//magnetometer:
float x = sparki.magX(); // measure the magnetometer x-axis
float y = sparki.magY(); // measure the magnetometer y-axis
float z = sparki.magZ(); // measure the magnetometer z-axis.
if (x < x_min) x_min = x;
if (x > x_max) x_max = x;
if (y < y_min) y_min = y;
if (y > y_max) y_max = y;
if (z < z_min) z_min = z;
if (z > z_max) z_max = z;
// write the measurements to the screen
sparki.print(“Accel X: “);
sparki.println(x, 2);
sparki.print(“MinX:”);
sparki.print(x_min, 1);
sparki.print(” MaxX:”);
sparki.println(x_max, 2);
sparki.println("");
sparki.print(“Accel y: “);
sparki.println(y, 2);
sparki.print(“MinY:”);
sparki.print(y_min, 1);
sparki.print(” MaxY:”);
sparki.println(y_max, 2);
sparki.println("");
sparki.print(“Accel z: “);
sparki.println(z, 2);
sparki.print(“MinZ:”);
sparki.print(z_min, 1);
sparki.print(” MaxZ:”);
sparki.print(z_max, 2);
sparki.updateLCD(); // display all of the information written to the screen
delay(100);
//}
switch(code){
// Movement buttons
case 22: sparki.clearLCD();
delay(1000); break;
case 70: sparki.moveForward(); break;
case 21: sparki.moveBackward(); break;
case 71:
case 67: sparki.moveRight(); break;
case 69:
case 68: sparki.moveLeft(); break;
case 64: sparki.moveStop();
sparki.gripperStop();
break;
// Gripper Buttons
case 9: sparki.gripperOpen(); break;
case 7: sparki.gripperClose(); break;
// buzzer
case 13: sparki.beep(); break;
// Servo Buttons
case 12: sparki.servo(SERVO_LEFT); break;
case 24: sparki.servo(SERVO_CENTER); break;
case 94: sparki.servo(SERVO_RIGHT); break;
// RGB LED
case 90: sparki.RGB(RGB_OFF); break;
case 66: sparki.RGB(RGB_RED); break;
case 82: sparki.RGB(RGB_GREEN); break;
case 74: sparki.RGB(RGB_BLUE); break;
default:
break;
}
}
[/code]