TinyCircuits

accelerometer LED G-force display

accelerometer LED G-force display
« on: August 26, 2013, 05:37:28 PM »
Hello,
I'm trying to get the LEDs on my 16 LED tinyshield to light up and display roughly how many Gs my Accelerometer tinyshield is outputing...e.g. (If the accelerometer registers 2.5 Gs in the Y plane then 6 of the 16 LEDs would light up, 1 for every .25 Gs)

I'm new to programming arduinos so any help showing what the code would look like to get this to work would be very appreciated.

Re: accelerometer LED G-force display
« Reply #1 on: August 29, 2013, 04:22:39 AM »
Can you post the code that you have so far?

I might be able to help you modify it.

Re: accelerometer LED G-force display
« Reply #2 on: September 05, 2013, 01:24:54 PM »
So what I have so far is basically just the accelerometer demo code, then i deleted the Y,Z and temp Serial.print functions.

I don't know how to take the X coordinate G-force reading from the serial port and give the output to the LEDs on the 16 LED shield.
(I'm new to arduino programming,and really need this for a project of mine)

I would really appreciate any help/advice anyone could give.

Thanks,
Ben

Here's the code i have so far:
Code: [Select]
#include <Wire.h>

#define BMA250_I2CADDR      0x18
#define BMA250_RANGE        0x03   // 0x03 = 2g, 0x05 = 4g, 0x08 = 8g, 0x0C = 16g
#define BMA250_BW           0x08   // 7.81Hz (update time of 64ms)

int AccelX;
int AccelY;
int AccelZ;
float AccelTemperature;

void setup()
{
  Wire.begin();
  Serial.begin(9600);
  BMA250Init();
}


void loop()
{
  BMA250ReadAccel();

  // Print out the accelerometer data
  Serial.print("x: ");
  Serial.print(AccelX);
   
   delay(100);
}


void BMA250Init()
{
  // Setup the range measurement setting
  Wire.beginTransmission(BMA250_I2CADDR);
  Wire.write(0x0F);
  Wire.write(BMA250_RANGE);
  Wire.endTransmission();
 
  // Setup the bandwidth
  Wire.beginTransmission(BMA250_I2CADDR);
  Wire.write(0x10);
  Wire.write(BMA250_BW);
  Wire.endTransmission();
}


int BMA250ReadAccel()
{
  uint8_t ReadBuff[8];
 
  // Read the 7 data bytes from the BMA250
  Wire.beginTransmission(BMA250_I2CADDR);
  Wire.write(0x02);
  Wire.endTransmission();
  Wire.requestFrom(BMA250_I2CADDR,7);
 
  for(int i = 0; i < 7;i++)
  {
    ReadBuff[i] = Wire.read();
  }
 
  AccelX = ReadBuff[1] << 8;
  AccelX |= ReadBuff[0];
  AccelX >>= 6;
 
  AccelY = ReadBuff[3] << 8;
  AccelY |= ReadBuff[2];
  AccelY >>= 6;
 
  AccelZ = ReadBuff[5] << 8;
  AccelZ |= ReadBuff[4];
  AccelZ >>= 6; 

  AccelTemperature = (ReadBuff[6] * 0.5) + 24.0;
}

 
« Last Edit: September 06, 2013, 05:43:12 AM by calvinthedestroyer »

Re: accelerometer LED G-force display
« Reply #3 on: September 06, 2013, 05:55:09 AM »
In your loop, you will want to:

1> read what the AccelX is
2> use a select case statement to decide how many LEDs to light up
3> turn on those LEDs

Hope that helps

Re: accelerometer LED G-force display
« Reply #4 on: September 09, 2013, 09:29:53 AM »
Thanks, I'll give that a shot.