- Mon Mar 02, 2015 8:43 pm
#180186
Hey guys, electronics novice here but I am having a go at it. Firstly yes i have googled. Lots... and lots. Hopefully this is an easy fix but this is what I want to achieve.
The job is for a metal fabrication workshop for their sheet metal "folder". I want to measure the angle of the fold by attaching an ADXL345. You can probably buy these pre made but I wanted to have a go myself and well.... I'm stuck.
I'll be using ADXL345 from SparkFun https://www.sparkfun.com/products/9836
And I will be displaying the calculated angle on an Adafruit 1.2" 7 segment display. It needs to be easy to read from a distance, hence the large display. https://www.adafruit.com/product/1270
I'm currently using an Arduino Uno for prototyping but the finished product I'll be using a 5v pro mini (already have one). Yes I know the ADXL345 is 3.3v, i'll be using LLC.
The ADXL will be approx 1.5m from the power supply so I am hoping voltage drop won't affect my readings too much.
The problem is (with my current code) it hangs after a few seconds of operation and needs to be power cycled to make it start up again.
Here's a sample of Serial Monitor during those few seconds
Finally here is my code. Any help you can offer is greatly appreciated.
Thanks!
The job is for a metal fabrication workshop for their sheet metal "folder". I want to measure the angle of the fold by attaching an ADXL345. You can probably buy these pre made but I wanted to have a go myself and well.... I'm stuck.
I'll be using ADXL345 from SparkFun https://www.sparkfun.com/products/9836
And I will be displaying the calculated angle on an Adafruit 1.2" 7 segment display. It needs to be easy to read from a distance, hence the large display. https://www.adafruit.com/product/1270
I'm currently using an Arduino Uno for prototyping but the finished product I'll be using a 5v pro mini (already have one). Yes I know the ADXL345 is 3.3v, i'll be using LLC.
The ADXL will be approx 1.5m from the power supply so I am hoping voltage drop won't affect my readings too much.
The problem is (with my current code) it hangs after a few seconds of operation and needs to be power cycled to make it start up again.
Here's a sample of Serial Monitor during those few seconds
Code: Select all
Yes I have covered the basics. Connections are solid. I2C working (have used I2C scanner and both devices appear). 14.00 -9.00 9.00 0.00-18.00 27.00 9.00 18.00 -18.00 18.00 18.00 36.00 23.00
Finally here is my code. Any help you can offer is greatly appreciated.
Thanks!
Code: Select all
#include <Wire.h>
#include <math.h>
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"
#define X0 0x32
#define X1 0x33
#define Y0 0x34
#define Y1 0x35
#define Z0 0x36
#define Z1 0x37
Adafruit_7segment matrix = Adafruit_7segment();
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
// Activate the 7-segment i2c
matrix.begin(0x70);
//Turning on the ADXL345
Wire.beginTransmission(0x1D); // transmit to ADXL345
Wire.write(0x2D); // POWER_CTL
Wire.write(0x09); // measurement mode, 4hz wakeup
Wire.endTransmission(); // stop transmitting
Serial.begin(9600);
}
int num_samples = 10; // take the average of multiple samples
void loop()
{
float sum = 0.0f;
for (int i=0; i < num_samples; i++)
{
float z = getZ(); // +z is up from the top of the pcb
float y = getY(); // +y is lengthwise to the right
float angle = atan2(z, y) * 180.0f / M_PI; // angle will be between -360 and 360 degrees
sum += angle;
delay(15);
}
float average = round(sum / num_samples);
matrix.printFloat(average);
matrix.writeDisplay();
Serial.print(average);
}
byte requestByte(char dir)
{
Wire.beginTransmission(0x1D); // transmit to ADXL345
Wire.write(dir);
Wire.endTransmission(); // stop transmitting
Wire.requestFrom(0x1D, 1); // request 1 byte from ADXL345
while(Wire.available())
{
return(Wire.read()); //
}
}
int getX()
{
int val = requestByte(X0);
val += requestByte(X1) << 8;
return val;
}
int getY()
{
int val = requestByte(Y0);
val += requestByte(Y1) << 8;
return val;
}
int getZ()
{
int val = requestByte(Z0);
val += requestByte(Z1) << 8;
return val;
}