My project is to make LEDs light up when my accelerometer hits a few specific orientations.
My current code creates conflicting commands, which ultimately causes my LEDs to dim rather than turn off.
I am using the X-axis and Y-axis.
I have three criteria.
OFF when the accelerometer is in any orientation other than what is listed.
ON When the X-axis is tilted up 90-ish degrees (X=560-631)
ON When the Y-axis is rolled left 90-ish degrees (Y=394-426)
The trouble comes when it tries to satisfy those criteria. In order for the LEDs to light when the X-axis is satisfied, the Y-axis reading will then violate the other code I have written for the Y-axis illumination.
Example of conflict: The LED will only light if the X-axis is at X=560-631, but to achieve that reading the Y-axis reaches Y=470-515 which my next piece of code was told to only light when the Y-axis satisfies Y=394-426.
What is the best way to solve this?
Maybe I could write something like "if this.. ignore this.." How would I write that in code language?
MY CODE
int Xval;
int Yval;
int led1 = 11;
int led2 = 10;
int led3 = 9;
int led4 = 3;
int accPin = A3;
int accPin2 = A2;
void setup(){
Serial.begin(9600);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
delay(1000);
Xval = analogRead(accPin);
Yval = analogRead(accPin2);
}
void loop(){
Xval = analogRead(accPin);
digitalWrite(led1, HIGH);
digitalWrite(led4, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led2, HIGH);
if (Xval < 560){
digitalWrite(led1, LOW);
digitalWrite(led4, LOW);
digitalWrite(led3, LOW);
digitalWrite(led2, LOW);
};
if (Xval > 598){
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led1, HIGH);
digitalWrite(led4, HIGH);
};
if (Xval > 631){
digitalWrite(led1, LOW);
digitalWrite(led4, LOW);
digitalWrite(led3, LOW);
digitalWrite(led2, LOW);
};
//y entries
Yval = analogRead(accPin2);
if (Yval < 394){
digitalWrite(led1, LOW);
digitalWrite(led4, LOW);
digitalWrite(led3, LOW);
digitalWrite(led2, LOW);
};
if (Yval > 394){
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led1, HIGH);
digitalWrite(led4, HIGH);
};
if (Yval > 426){
digitalWrite(led1, LOW);
digitalWrite(led4, LOW);
digitalWrite(led3, LOW);
digitalWrite(led2, LOW);
};
}