Here is the Code…..Enjoy
const int rgbRed = 12;
const int rgbGreen = 13;
const int rgbBlue = 14;
const int zAxis = A8;
const int yAxis = A7;
const int xAxis = A5;
float gravitationalConstant = 192;
float xAxisError = 425;
float yAxisError = 408;
float zAxisError = 520;
float zAxisValue;
float yAxisValue;
float xAxisValue;
void setup() {
// put your setup code here, to run once:
pinMode(rgbRed, OUTPUT);
pinMode(rgbGreen, OUTPUT);
pinMode(rgbBlue, OUTPUT);
pinMode(zAxis, INPUT);
pinMode(yAxis, INPUT);
pinMode(xAxis, INPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
zAxisValue = analogRead(zAxis);
yAxisValue = analogRead(yAxis);
xAxisValue = analogRead(xAxis);
xAxisValue = round((xAxisValue – xAxisError) / 192);
yAxisValue = round((yAxisValue – yAxisError) / 192);
zAxisValue = round((zAxisValue – zAxisError) / 192);
Serial.print(“x value is: “);
Serial.print(xAxisValue);
Serial.print(” y value is: “);
Serial.print(yAxisValue);
Serial.print(” z value is: “);
Serial.println(zAxisValue);
delay(250);
if(xAxisValue == 1 || xAxisValue == -1){
digitalWrite(rgbRed, HIGH);
digitalWrite(rgbGreen, LOW);
digitalWrite(rgbBlue, LOW);
} else if(yAxisValue == 1 || yAxisValue == -1){
digitalWrite(rgbRed, LOW);
digitalWrite(rgbGreen, HIGH);
digitalWrite(rgbBlue, LOW);
} else if(zAxisValue == 1 || zAxisValue == -1){
digitalWrite(rgbRed, LOW);
digitalWrite(rgbGreen, LOW);
digitalWrite(rgbBlue, HIGH);
} else {
digitalWrite(rgbRed, LOW);
digitalWrite(rgbGreen, LOW);
digitalWrite(rgbBlue, LOW);
}
}