Hello
I am beginner and need help about display . I got sensor data as +##.# by this code.
I hooked COM-11441 with I2C wiring. I cannot figure out how to make data that LED will indicates same way as Serial.print shows.
so The 4th to be + or - only, 3rd and 2nd to be number, a dot and 1 st to be number.
I changed - for xLED and %4d, but doesn’t do good. right now, it shows random numbers continuously with a dot at right spot.
Please help me out. Thanks
// Wire connection for i2c using ADXL345 with Redboard or Arduino Uno :
// RedBoard ↔ ADXL345 ↔ COM-11441
// Gnd - GND - GND
// 3.3v - VCC - VCC
// 3.3v - CS
// Analog 4 - SDA - SDA
// Analog 5 - SLC - SLC
// PULL DOWN RESISTOR TO BE 4.7K ~ 10K
#include <Wire.h>
#define DEVICE (0x53) // Device address as specified in data sheet for ADXL345
#define DISPLAY_ADDRESS1 (0x71) //This is the default address for COM-11441
char xLED [8]; // for Display 8 bit data
byte _buff[6];
char POWER_CTL = 0x2D; //Power Control Register for 345
char DATA_FORMAT = 0x31; // for 345
char DATAX0 = 0x32; //X-Axis Data 0 on 345
char DATAX1 = 0x33; //X-Axis Data 1 on 345
char DATAY0 = 0x34; //Y-Axis Data 0 on 345
char DATAY1 = 0x35; //Y-Axis Data 1 on 345
char DATAZ0 = 0x36; //Z-Axis Data 0 on 345
char DATAZ1 = 0x37; //Z-Axis Data 1 on 345
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output. Serial Monitor must match.
//Put the ADXL345 into +/- 4G range by writing the value 0x01 to the DATA_FORMAT register.
writeTo(DATA_FORMAT, 0x01);
//Put the ADXL345 into Measurement Mode by writing 0x08 to the POWER_CTL register.
writeTo(POWER_CTL, 0x08);
Wire.beginTransmission(DISPLAY_ADDRESS1); // for 7seg LED
Wire.write(0x79);
Wire.write(0x03);
delay(1000);
// Wire.write(0x76); // claer LED
// setDecimalsLED(0b00000100); // Sets digit 3 decimal on
Wire.endTransmission();
}
/*
void setDecimalsLED(byte decimals) // 3 Decimal call
{
Wire.beginTransmission(DISPLAY_ADDRESS1);
Wire.write(0x77);
Wire.write(decimals);
Wire.endTransmission();
}
- /
void loop()
{
readAccel(); // read the x/y/z tilt
delay(50);
//send7seg(); // send out x angle to 7 seg LED disply
//delay(10);
}
void readAccel()
{
uint8_t howManyBytesToRead = 6;
readFrom(DATAX0, howManyBytesToRead, _buff); //read the acceleration data from the ADXL345
// Start from DATAX0 adddress and read 6 bytes, put in buff
// each axis reading comes in 10 bit resolution, ie 2 bytes. Least Significat Byte first!!
// converting both bytes in to one int
int x = (((int)_buff[1]) << 8) | _buff[0];
int y = (((int)_buff[3]) << 8) | _buff[2];
delay(50);
// You need adjust offset for each sensor or adjust sensor pcb to get reading zero for both X & Y
x = x - 10;
y = y - 10;
// Print the results to the serial to check the value.
// Serial.print((float)x, 5);
//Serial.print(‘,’);
//Serial.println((float)y, 5);
//delay(100);
// per +/-4g mode, G = measurment value * (8/1024 = 0.0078125)
float xx = x * 0.0078125;
float yy = y * 0.0078125;
//Print the results to the serial to check the value.
//Serial.print((float)xx, 5);
//Serial.print(‘,’);
//Serial.println((float)yy, 5);
//delay(100);
// Calculate Pitch %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
float xangle = atan (xx / yy);
// convert from radian to angle
xangle *= 180.00;
xangle /= 3.14159; // This is final X angle data ###################
//Print the angle to the monitor.
Serial.print("Pitch X: "); // print header on monitor
Serial.println(xangle, 1); // print x axis data next of Pitch x:
//xLED = (xangle / 255); // make 7 bit data for LED Display
sprintf(xLED, “%4d”, xangle);
send7segLED(); // send out x angle to 7 seg LED disply
}
void writeTo(byte address, byte val)
{
Wire.beginTransmission(DEVICE); // start transmission to ADXL345 (0x53)
Wire.write(address); // send register address
Wire.write(val); // send value to write
Wire.endTransmission(); // end transmission
}
// Reads num bytes starting from address register on device in to _buff array
void readFrom(byte address, int num, byte _buff)
{
Wire.beginTransmission(DEVICE); // start transmission to ADXL345 (0x53)
Wire.write(address); // sends address to read from
Wire.endTransmission(); // end transmission
Wire.beginTransmission(DEVICE); // start transmission to device
Wire.requestFrom(DEVICE, num); // request 6 bytes from device (2 bytes per axis)
int i = 0;
while(Wire.available()) // device may send less than requested (abnormal)
{
_buff = Wire.read(); // receive a byte
- i++;*
- }*
- Wire.endTransmission(); // end transmission*
- }*
- void send7segLED()*
- {*
- Wire.beginTransmission(DISPLAY_ADDRESS1); // contact 7seg LED display (0x71)*
- Wire.write(xLED);*
- delay(10);*
- Wire.endTransmission();*
- }*