I’ve written a sketch (first one) that measures movement with an accelerometer and will turn a phone on and off based on that movement. I’m looking for a constructive critique of the code. Maybe places where it can be cleaned up or made a little more efficient.
/* -Accelerometer Based Motion Detection System-
This system measures motion with a 2-axis accelerometer and
if motion is detected within a certain threshold, a digital signal
will be sent to switch a phone on or off. Analog values at rest are
typically 511. If there is a fluctuation of +or- 3 then it is safe
to assume there is movement.
-Code by: SJS Enterprises -
-Version 0.01beta-
*/
int switchPin = 4; //pin that turns phone on and off
int xVal = 0; //first x-axis reading in for loop
int yVal = 0; //first y-axis reading in for loop
int xVal2 = 0; //second x-axis reading in for loop
int yVal2 = 0; //second y-axis reading in for loop
int x = 4; //analog input pin
int y = 5; //analog input pin
int xValHigh1 = 0;
int yValHigh1 = 0;
int xValLow1 = 0;
int yValLow1 = 0;
int xValHigh;
int xValLow;
int yValHigh;
int yValLow;
boolean phoneOn = FALSE;//is phone on
boolean movement = FALSE;
//turn on the power to phone when system is initially energized
void power(){
digitalWrite(switchPin, HIGH);
delay(3000);
digitalWrite(switchPin, LOW);
xVal = analogRead(x);
yVal = analogRead(y);
phoneOn = TRUE;
delay(3000);
}
void setup(){
pinMode(4,OUTPUT);
power();
}
void loop(){
//set initial high and low values
xValLow = 1023;
xValHigh = 0;
yValLow = 1023;
yValHigh = 0;
/*this 'for' loop grabs accelerometer readings every second for
five minutes and stores the highest and lowest values that were
received during that time
*/
for (int i=0; i<300; i++){
xVal = analogRead(x);//first x-axis reading
yVal = analogRead(y);//fist y-axis reading
delay(1000);//pause for 1 second before taking another axis reading
xVal2 = analogRead(x);//second x-axis reading
yVal2 = analogRead(y);//second y-axis reading
xValLow1 = min(xVal,xVal2);//set minimum value of two analog readings
xValHigh1 = max(xVal,xVal2);//set maximum value of two analog readings
yValLow1 = min(yVal,yVal2);//set minimum value of two analog readings
yValHigh1 = max(yVal,yVal2);//set maximum value of two analog readings
xValLow = min(xValLow,xValLow1);//lowest x-axis value during loop
xValHigh = max(xValHigh,xValHigh1);//highest x-axis value during loop
yValLow = min(yValLow,yValLow1);//lowest y-axis value during loop
yValHigh = max(yValHigh,yValHigh1);//highest y-axis value during loop
}
//is there movement within threshold on either x or y axis
if ((xValHigh-xValLow)>5) || ((yValHigh-yValLow)>5){
movement = TRUE;
}
else {
movement = FALSE;
}
//if there is movement and the phone is off then turn it on
if (movement == TRUE && phoneOn == FALSE){
digitalWrite(switchPin,HIGH);
delay(3000);
digitalWrite(switchPin,LOW);
phoneOn=TRUE;
}
//if there is no movement and phone is on then turn it off
if (movement == FALSE && phoneOn == TRUE){
digitalWrite(switchPin,HIGH);
delay(3000);
digitalWrite(switchPin,LOW);
phoneOn=FALSE;
}