TinyCircuits Forum
General Category => User Projects / Code Examples => Topic started by: Ironman11 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.
-
Can you post the code that you have so far?
I might be able to help you modify it.
-
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:
#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;
}
-
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
-
Thanks, I'll give that a shot.