Hi all this is my first project and im struggling to get my leds to light when the value of the x and y axis are greater than -2 and less than or equal to -8 in a negative way.
There a 9 leds connected to my mega which all work ok and a MMA7455 accelerometer which works ok also.
Green led lights when board is level. There are 2 red leds on each axis positive and negative which light depending on the x y value. I'm strug;ing with this one on each of the negative axis.
Any help would be great.
if (( yVal > -2 ) && (yVal < -8))
if (( xVal > -2 ) && (xVal < -8))
#include <Wire.h> //Include the Wire library
#include <MMA_7455.h> //Include the MMA_7455 library
int redPinx1 = 51; // Red LED connected to digital pin 51 x axis
int redPinx2 = 49; // Red LED connected to digital pin 49 x axis 2
int greenPin = 47; // Green connected to digital pin 47
int redPinx3 = 45; // Red LED connected to digital pin 45 neg x axis
int redPinx4 = 43; // Red LED connected to digital pin 43 neg x axis
int redPiny1 = 41; // Green LED connected to digital pin 41 y axis
int redPiny2 = 39; // Red LED connected to digital pin 39 y axis
int redPiny3 = 37; // Red LED connected to digital pin 37 neg y axis
int redPiny4 = 35; // Red LED connected to digital pin 35 neg y axis
MMA_7455 accel = MMA_7455(); // Make MMA7455 object
char xVal, yVal, zVal; // Return value variables
void setup() {
pinMode(redPinx1, OUTPUT); // sets the digital pin as output
pinMode(redPinx2, OUTPUT); // sets the digital pin as output
pinMode(redPinx3, OUTPUT); // sets the digital pin as output
pinMode(redPinx4, OUTPUT); // sets the digital pin as output
pinMode(greenPin, OUTPUT); // sets the digital pin as output
pinMode(redPiny1, OUTPUT); // sets the digital pin as output
pinMode(redPiny2, OUTPUT); // sets the digital pin as output
pinMode(redPiny3, OUTPUT); // sets the digital pin as output
pinMode(redPiny4, OUTPUT); // sets the digital pin as output
Serial.begin(9600); // Use the Serial Monitor window at 9600 baud
// Set the g force sensitivity: 2=2g, 4=4g, 8-8g
accel.initSensitivity(8);
// Provide oiffset values so that sensor displays 0, 0, 63
// (or close to it) when positioned on a flat surface, all pins
// facing down
// Update the numbers with your own values from the MMA7455_CalibrateOffset sketch.
accel.calibrateOffset(-1, 10, -70);
}
void loop() {
// Get the X, Y, anx Z axis values from the device
xVal = accel.readAxis('x'); // Read X Axis
yVal = accel.readAxis('y'); // Read Y Axis
zVal = accel.readAxis('z'); // Read Z Axis
// Display them in the Serial Monitor window.
Serial.print("X: = ");
Serial.print(xVal, DEC);
Serial.print(" Y: = ");
Serial.print(yVal, DEC);
Serial.print(" Z: = ");
Serial.println(zVal, DEC);
delay(2000);
digitalWrite(redPinx1, LOW); // sets the Red LED off
digitalWrite(redPinx2, LOW); // sets the Red LED off
digitalWrite(redPinx3, LOW); // sets the Red LED off
digitalWrite(redPinx4, LOW); // sets the Red LED off
digitalWrite(redPiny1, LOW); // sets the Red LED off
digitalWrite(redPiny2, LOW); // sets the Red LED off
digitalWrite(redPiny3, LOW); // sets the Red LED off
digitalWrite(redPiny4, LOW); // sets the Red LED off
if (( xVal < 2 ) && ( xVal > -2 ) && ( yVal < 2) && ( yVal > -2 ))
{
digitalWrite(greenPin, HIGH); // set green led if x is less than 2 and greater than -1 and y is less than 2 and greater than -1
}
else
{
digitalWrite(greenPin, LOW); // sets the Green LED off
}
if (( xVal > 2 ) && ( xVal <= 8))
{
digitalWrite(redPinx1, HIGH); // sets the x axis Red LED on posative value
}
else if (( xVal > 8))
{
digitalWrite(redPinx2, HIGH); // sets the Red LED on posative value
digitalWrite(redPinx1, HIGH); // sets the x axis Red LED on posative value
}
if (( xVal > -2 ) && (xVal < -8))
{
digitalWrite(redPinx3, HIGH); // sets the Red LED on negative value
}
else if (( xVal < -15))
{
digitalWrite(redPinx4, HIGH); // sets the Red LED on negative value
digitalWrite(redPinx3, HIGH); // sets the Red LED on negative value
}
if (( yVal > 2 ) && ( yVal <= 8))
{
digitalWrite(redPiny1, HIGH); // sets the x axis Red LED on posative value
}
else if (( yVal > 8)) // sets y axis
{
digitalWrite(redPiny2, HIGH); // sets the Red LED on posative value
digitalWrite(redPiny1, HIGH); // sets the x axis Red LED on posative value
}
if (( yVal > -2 ) && (yVal < -8))
{
digitalWrite(redPiny3, HIGH); // sets the Red LED on negative value
}
else if (( yVal < -15))
{
digitalWrite(redPiny4, HIGH); // sets the Red LED on negative value
digitalWrite(redPiny3, HIGH); // sets the Red LED on negative value
}
}