 # Coding questions relating to accelerometer

Hi. Im very new to Arduino and have a question relating to a project I am attempting.
I am making 2 Axis digital level using an AM3X accelerometer and Uno.

I have 5 led’s in an X. Up, down, left and right, and centre.
I can get the directional led’s to work, but not the centre one.

I dont know how to specify a range of numbers for the centreLed to go to HIGH, or if that is even how I would approach it.
I have these numbers being read off my accelerometer, and want the centre led to go HIGH when within these, or go LOW when outside them, so between Xaxis 330 and 340, and Yaxis 350 and 360.

I hope I am not too confusing with my question, but I am a noob.

Thanks heaps in advance, I would really appreciate some help from the gurus here.

Scott

const int forwardThreshold = 340;
const int backwardThreshold = 330;
const int leftThreshold = 360;
const int rightThreshold = 350;

My code is such

int xAxis = A0;
int yAxis = A1;
int zAxis = A2;
int axisValue = 0;
int centreLed = 13;
int forwardLed = 2;
int backwardLed = 3;
int leftLed = 4;
int rightLed = 5;
const int forwardThreshold = 340;
const int backwardThreshold = 330;
const int leftThreshold = 360;
const int rightThreshold = 350;

void setup() {
Serial.begin(38400);
Serial.println(“Starting up”);
pinMode(forwardLed, OUTPUT);
pinMode(backwardLed, OUTPUT);
pinMode(leftLed, OUTPUT);
pinMode(rightLed, OUTPUT);
pinMode(centreLed, OUTPUT);
}

void loop() {

// X axis

Serial.print(“X=”);
Serial.print(axisValue);

// centre led

// Forward LED

if (axisValue > forwardThreshold) {
digitalWrite(forwardLed, HIGH);
}
else {
digitalWrite(forwardLed, LOW);
}

// Backward LED

if (axisValue < backwardThreshold) {
digitalWrite(backwardLed, HIGH);
}
else {
digitalWrite(backwardLed, LOW);
}

// Y Axis

Serial.print(",Y=");
Serial.print(axisValue);

// Left LED

if (axisValue > leftThreshold) {
digitalWrite(leftLed, HIGH);
}
else {
digitalWrite(leftLed, LOW);
}

// Right LED

if (axisValue < rightThreshold) {
digitalWrite(rightLed, HIGH);
}
else {
digitalWrite(rightLed, LOW);
}

// Z Axis

Serial.print(",Z=");
Serial.println(axisValue);

delay(200);
}

Untested code warning.
Semi-pseudo code warning.

Firstly, rather than hide your evaluation amongst the control branches I’d evaluate and the state of the two axis early (shown below in left_right and forward_backward). It makes the code easier to read, test and maintain.

I think what you seem to be struggling with is checking when a value is between two limits, which is demonstrated below.

``````typedef left_right_t {LEFT, CENTRE_LR, RIGHT};
typedef forward_backward_t {FORWARD, CENTER_FB, BACKWARD};

void loop()
{
left_right_t : left_right;
forward_backward_t: forward_backward;

// Get X, Y (anareads etc...)

if (x < leftThreshhold)
left_right = LEFT;
else if ((x >= leftThreshhold )  && (x <= rightThreshhold))
left_right = CENTRE_LR;
else
left_right = RIGHT;

if (y < forwardThreshhold)
forward_backward = FORWARD;
else if ((y >= forwardThreshhold )  && (y <= backwardThreshhold))
forward_backward = CENTRE_FB;
else
forward_backward = BACKWARDS;

// Extinguish the centre LED
// Extinguish the forward and backward LED
switch (forward_backward)
{
case FORWARDS:
// Light the forwards LED
break;
case BACKWARDS:
// Light the backwards LED
break;
case CENTRE_FB:
// Light the centre LED
}

// Extinguish the left and centre LED .... DO NOT EXTINGUISH THE CENTRE!
switch (left_right)
{
case LEFT:
// Light the LEFT LED
break;
case RIGHT:
// Light the RIGHT LED
break;
case CENTRE_FB:
// Light the CENTRE LED
break;

}
}
``````

Actually, I don’t like the switch statements. You could evaluate each LED on the logical states of the left_right, forward_backward, replacing switch statements with:-

``````typedef left_right_t {LEFT, CENTRE_LR, RIGHT};
typedef forward_backward_t {FORWARD, CENTER_FB, BACKWARD};

void loop()
{
left_right_t : left_right;
forward_backward_t: forward_backward;

// Get X, Y (anareads etc...)

if (x < leftThreshhold)
left_right = LEFT;
else if ((x >= leftThreshhold )  && (x <= rightThreshhold))
left_right = CENTRE_LR;
else
left_right = RIGHT;

if (y < forwardThreshhold)
forward_backward = FORWARD;
else if ((y >= forwardThreshhold )  && (y <= backwardThreshhold))
forward_backward = CENTRE_FB;
else
forward_backward = BACKWARDS;

digitalWrite(ForwardLed, (forward_backward == FORWARD));
digitalWrite(BackwardLed, (forward_backward == BACKWARD));
digitalWrite(LeftLed, (left_right == LEFT));
digitalWrite(RightLed, (left_right == RIGHT));
digitalWrite(CentreLed, ((left_right == CENTRE_LR) || (forward_backward == CENTRE_FB));
}
``````

``````if ( (digitalRead(1) == LOW) && (digitalRead(2) == LOW) && (digitalRead(3) == LOW) )