Physiotherapy Helper (Arduino Adxl345 Matrix)
by murphypoon in Circuits > Arduino
2108 Views, 9 Favorites, 0 Comments
Physiotherapy Helper (Arduino Adxl345 Matrix)
Physiotherapy Helper
This tools is really help me for Physiotherapy. Specially, moving range and position, So, i can follow doctor instruction to do exercise and step by step to increase and decrease the action.
--------------------------------------------------------------------------------------------
Caution :
- It is not approved or referenced by any Physiotherapy Doctor.
- It is just for my Physiotherapy only.
- Please consult your doctor for any necessary help.
--------------------------------------------------------------------------------------------
Parts :
1. (1) Old watch band
2. (1) ADXL345 Accelerometer
3. (1) Arduino
4. (1) Matrix 8x8 with MAX7219
5. (1) Button with 10K resister
For information on installing libraries, see: http://www.arduino.cc/en/Guide/Libraries
Detail Please refer : http://www.imediabank.com
Connect Together
Connect -ADXL345 and Arduino
--- I2C :
- SDA : A4 (Digispark D0)
- SCL : A5 (Digispark D2)
- SDO : GND
- CS : V++
Connect Matrix 8x8 and Arduino :
- CLK : PIN 11
- CS : PIN 10
- DIN : PIN 12
Connect Button and Arduino :
- Button 1 : PIN 2
- Button 2 : 10K to GND
- Button 3 : V++
Put ADXL345 into Old Watch Band
Function and Programming
Function
- Press Control Button 3 seconds to change Mode
Mode 0 : Show X Only
Mode 1 : Show Y Only
Mode 2 : Show X and Y on Matrix by Red Point
<p>/*<br> * iMediaBank 201605
*
* ONLY for ADXL345 XYZ Sensor
*
* Connect -ADXL345 and Arduino
* I2C :
SDA : A4 (Digispark D0)
SCL : A5 (Digispark D2)
SDO : GND
CS : V++
*
*
* Connect Matrix 8x8 and Arduino :
* CLK : PIN 11
* CS : PIN 10
* DIN : PIN 12
*
*
*/</p><p>#include <wire.h>
#include "LedControl.h"
#define DEVICE (0x53) //ADXL345 device address
#define TO_READ (6) //num of bytes we are going to read each time (two bytes for each axis)
byte buff[TO_READ] ; //6 bytes buffer for saving data read from the device
char str[512]; //string buffer to transform data before sending it to the serial port
int regAddress = 0x32; //first axis-acceleration-data register on the ADXL345
int x, y, z; //three axis acceleration data
double roll = 0.00, pitch = 0.00; //Roll & Pitch are the angles which rotate by the axis X and y
//in the sequence of R(x-y-z),more info visit
// https://www.dfrobot.com/wiki/index.php?title=How_...></p><p>// --------------------------- Matrix
int matrix = 1;
LedControl lc=LedControl(12,11,10,matrix);
/* we always wait a bit between updates of the display */
unsigned long delaytime=100;</p><p>// ----------------------------- Control Button
int ctrlBtn = 2; // Control Button
int dispMode = 2; // Display Mode (x, y, z, xyz and freePoint)</p><p>// -------------------------------- Reset Matrix
void resetMatrix() {
for (int k=0;k<matrix; k++)="" {="" ="" lc.shutdown(k,false);="" *="" set="" the="" brightness="" to="" a="" medium="" values="" lc.setintensity(k,8);="" and="" clear="" display="" lc.cleardisplay(k);="" } ="" }="" ------------------------------="" reset="" matrix="" end<="" p=""></matrix;></p><p>// ------------------------------- Setup
void setup() {
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(115200); // start serial for output
//Turning on the ADXL345
writeTo(DEVICE, 0x2D, 0);
writeTo(DEVICE, 0x2D, 16);
writeTo(DEVICE, 0x2D, 8);</p><p> resetMatrix();</p><p>}</p><p>const int XYZSize = 3;
byte XYZ[XYZSize][8]={
{B00100100, B00011000, B00100100, B00000000, B11111111, B00000000, B11111111, B00000000}, // X
{B01010000, B01010000, B01010100, B01010011, B01010100, B01010000, B01010000, B01010000}, // Y
//{B01001000, B01011000, B01101000, B00000000, B00000000, B00111111, B00111111, B00000000}, // Z
//{B10011111, B01011111, B00000000, B10000111, B10000111, B00000000, B10001111, B11001111}, //XYZ
{B00000000, B01000000, B00000000, B00011100, B00011000, B00010100, B00000010, B00000001}}; // Free Point</p><p>// -------------------------- writeMatrix
void writeMatrix() {
for (int j=0; j<xyzsize;j++) {="" ="" for="" (int="" i="0;" i<8;="" i++)="" lc.setrow(0,i,xyz[j][7-i]);="" lc.setcolumn(0,i,xyz[j][7-i]);="" delay(delaytime);="" }="" }<="" p=""></xyzsize;j++)></p><p>} // ---------------------------- writeMatrix END</p><p>// -------------------------- Display Matrix
void displayMatrix(int matrixIndex) {
for (int i=0; i<8; i++) {
lc.setRow(0,i,XYZ[matrixIndex][7-i]);
//lc.setColumn(0,i,XYZ[matrixIndex][7-i]);
//delay(delaytime);
}
} // ---------------------------- display Matrix END</p><p>// -------------------------- Signle
int oldx = 0, oldy = 0;
void single(int x, int y) {
if ((oldx != x) || (oldy != y))
lc.setLed(0, oldx, oldy,false);
lc.setLed(0,x,y,true);</p><p> oldx = x;
oldy = y;
} //-------------------------------- Single END</p><p>// ----------------------------------------- LOOP
void loop() {</p><p> if (digitalRead(ctrlBtn) == false) {
readFrom(DEVICE, regAddress, TO_READ, buff); //read the acceleration data from the ADXL345
//each axis reading comes in 10 bit resolution, ie 2 bytes. Least Significat Byte first!!
//thus we are converting both bytes in to one int
x = (((int)buff[1]) << 8) | buff[0];
y = (((int)buff[3])<< 8) | buff[2];
z = (((int)buff[5]) << 8) | buff[4];
//we send the x y z values as a string to the serial port
Serial.print("The acceleration info of x, y, z are:");
sprintf(str, " %d %d %d", x, y, z);
Serial.print(str);
Serial.write(10);</p><p> int xx = map(x, 280, -245, 0, 7);
int yy = map(y, 280, -245, 7, 0);
sprintf(str, " %d / %d : %d", xx, yy, dispMode);
Serial.println(str);
</p><p> switch (dispMode) {
case 0 : // X
displayMatrix(dispMode);
single(2, xx);
break;
case 1 : // Y
displayMatrix(dispMode);
single(yy, 2);
break;
case 2 : // Free Point
single(yy, xx);
break;
default: single(yy, xx); break;
}
/*
//Roll & Pitch calculate
RP_calculate();
Serial.print("Roll:"); Serial.println( roll );
Serial.print("Pitch:"); Serial.println( pitch );
Serial.println("");
//It appears that delay is needed in order not to clog the port
*/
delay(100);
} else {
// -------------------------- Control Mode
dispMode++;
if (dispMode >= 3) dispMode = 0;
Serial.println(dispMode);
displayMatrix(dispMode);
delay(3000);
lc.clearDisplay(0);
}</p><p>} //------------------------------ LOOP end </p><p>//---------------- ADXL345 Functions
//Writes val to address register on device
void writeTo(int device, byte address, byte val) {
Wire.beginTransmission(device); //start transmission to device
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(int device, byte address, int num, byte buff[]) {
Wire.beginTransmission(device); //start transmission to device
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
int i = 0;
while(Wire.available()) //device may send less than requested (abnormal)
{
buff[i] = Wire.read(); // receive a byte
i++;
}
Wire.endTransmission(); //end transmission
}
//calculate the Roll&Pitch
void RP_calculate(){
double x_Buff = float(x);
double y_Buff = float(y);
double z_Buff = float(z);
roll = atan2(y_Buff , z_Buff) * 57.3;
pitch = atan2((- x_Buff) , sqrt(y_Buff * y_Buff + z_Buff * z_Buff)) * 57.3;
}</p>For information on installing libraries, see: http://www.arduino.cc/en/Guide/Libraries
Detail Please refer : http://www.imediabank.com