Is this better?
#include <serLCD.h>
#include <SoftwareSerial.h>
#include <LiquidCrystal.h>
#include <math.h>
int oldval=0; //sec value for the button
int state1=0; //state the button is in
int val = 0; //first value for the button
const int button = 7; //pin for the push button (for testing)
// these constants won't change:
const int xPin = 2; // X output of the accelerometer
const int yPin = 3; // Y output of the accelerometer
//Button Left And Right pins (for use later)
int right = 4;
int left = 5;
// Set pin to the LCD's rxPin
int pin = 1;
serLCD lcd(pin);
int Xraw, Yraw;
double xGForce, yGForce, Xangle, Yangle;
double saveY, saveX, state;
void setup()
{
// initialize serial communications:
Serial.begin(9600);
// initialize the pins connected to the accelerometer
// as inputs:
pinMode(xPin, INPUT);
pinMode(yPin, INPUT);
Serial.write(0xFE); //command flag
Serial.write(0x01); //clear command.
delay(10);
Serial.print("Lets Begin");
delay(1000);
}
void loop()
{
// lcd.clear();
Serial.write(0xFE); //command flag
Serial.write(0x01); //clear command.
delay(10);
// variables to read the pulse widths:
int pulseX, pulseY;
// variables to contain the resulting accelerations
int accelerationX, accelerationY;
// read pulse from x- and y-axes:
pulseX = pulseIn(xPin,HIGH);
pulseY = pulseIn(yPin,HIGH);
Xraw = pulseIn (xPin, HIGH);
Xraw = pulseIn (xPin, HIGH);
Yraw = pulseIn (yPin, HIGH);
Yraw = pulseIn (yPin, HIGH);
// Calculate G-force in Milli-g's.
xGForce = (( Xraw / 10 ) - 500) * 8;
yGForce = (( Yraw / 10 ) - 500) * 8;
// Calculate angle (radians) for both -x and -y axis.
Xangle = asin ( xGForce / 1000.0 );
Yangle = asin ( yGForce / 1000.0 );
// Convert radians to degrees.
Xangle = Xangle * (360 / (2*M_PI));
Yangle = Yangle * (360 / (2*M_PI));
//save angle when button is pushed, then compare to live reading send a zero once live reading
//is below saved angle
Serial.print("Y Angle: ");
Serial.print(Yangle);
val= digitalRead(button); //Read input
if ((val==HIGH) && (oldval==LOW))
{
test(saveY);
Serial.print(saveY);
}
else if (Yangle <= -10)
{
Serial.write(0xFE); //command flag
Serial.write(192); //position
Serial.print(saveY);
delay(150);
}
else
{
Serial.write(0xFE); //command flag
Serial.write(192); //position
Serial.print(saveY);
delay(150);
}
}
int test(int saveY)
{
Serial.write(0xFE); //command flag
Serial.write(192); //position
saveY=Yangle;
Serial.print(saveY);
delay(150);
return saveY;
}
EDIT: I was at first using the sparkfun MPU 6050 https://www.sparkfun.com/products/11028? but that quickly proved to be much bigger of a beast then i thought.
So i decided to use the accelerometer. They say its accurate up to 45 degrees and its easy to code. Needing this for a class i figured i could refine it as i start using it. But having it flat moving it across the table it does give me wild readings with the accel. Thnk you for pointing that out. I might be able to implement the accel into a new version where i would use the accel to compare speeds for the auto shut off.
What kind of easy programable chip do you guys recommend that i can use for this project? Or has anyone used the MPU 6050 with their arduino project? Also would still be interested in fixing my problem i have so i wont have it later in the future.