Pages: [1]   Go Down
Author Topic: Coding questions relating to accelerometer  (Read 338 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
 
  axisValue = analogRead(xAxis);   
  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
 
  axisValue = analogRead(yAxis);   
  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
 
  axisValue = analogRead(zAxis);   
  Serial.print(",Z=");
  Serial.println(axisValue);
 
  delay(200);                 
}



Logged

Offline Offline
Newbie
*
Karma: 1
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.


Code:

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;
  
     }
}

« Last Edit: June 10, 2013, 12:57:10 am by Doddy » Logged

Offline Offline
Newbie
*
Karma: 1
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:-


Code:

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));
}
« Last Edit: June 10, 2013, 01:03:16 am by Doddy » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you for your reply , and your time in writing to me.

I'm trying really hard to understand what it means but, or how I would use that in my code, or where I would put it in my code.
I'm only a noob and 12 years old.
It's really hard to find how to do this stuff.
Would there be a way of saying "if pins 1,2,3,4 etc are LOW then make pin 12 HIGH?

Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 114
Posts: 4257
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Would there be a way of saying "if pins 1,2,3,4 etc are LOW then make pin 12 HIGH?
Code:
if ( (digitalRead(1) == LOW) && (digitalRead(2) == LOW) && (digitalRead(3) == LOW) )
{
   digitalWrite(12, HIGH);
}

NOTE - using pin 1 is not a good idea because it is used for serial communication on many Arduino boards.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Pages: [1]   Go Up
Jump to: