-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransmitter Code.rtf
More file actions
84 lines (84 loc) · 2.88 KB
/
Copy pathTransmitter Code.rtf
File metadata and controls
84 lines (84 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
{\rtf1\ansi\ansicpg1252\deff0\deflang16393{\fonttbl{\f0\fnil\fcharset0 Calibri;}}
{\*\generator Msftedit 5.41.21.2510;}\viewkind4\uc1\pard\sa200\sl276\slmult1\lang9\f0\fs22 // tx\par
\par
#include <Wire.h> \par
#include <SPI.h>\par
#include <nRF24L01.h>\par
#include <RF24.h>\par
\par
#define CE_PIN 9\par
#define CSN_PIN 10\par
\par
#define ACC (0xA7>>1) //ADXL345 ACC address\par
#define A_TO_READ (6) //number of bytes we are going to read each time (two bytes for each axis)\par
\par
void initAcc() \{ //Turning on the ADXL345\par
writeTo(ACC, 0x2D, 1<<3);\par
writeTo(ACC, 0x31, 0x0B);\par
writeTo(ACC, 0x2C, 0x09); //by default the device is in +-2g range reading\par
\}\par
\par
void getAccelerometerData(int * result) \{\par
int regAddress = 0x32; //first axis-acceleration-data register on the ADXL345\par
byte buff[A_TO_READ];\par
readFrom(ACC, regAddress, A_TO_READ, buff); //read the acceleration data from the ADXL345\par
//each axis reading comes in 10 bit resolution, ie 2 bytes. Least Significat Byte first!!\par
//thus we are converting both bytes in to one int\par
result[0] = (((int)buff[1]) << 8) | buff[0];\par
result[1] = (((int)buff[3])<< 8) | buff[2];\par
result[2] = (((int)buff[5]) << 8) | buff[4];\par
\}\par
\par
// NOTE: the "LL" at the end of the constant is "LongLong" type\par
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe\par
\par
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio\par
\par
int trans[3]; // 3 element array holding transmitted readings\par
\par
void setup()\par
\{\par
Serial.begin(9600);\par
radio.begin();\par
radio.openWritingPipe(pipe);\par
Wire.begin();\par
initAcc();\par
\}\par
\par
void loop()\par
\{\par
getAccelerometerData(trans);\par
radio.write( trans, sizeof(trans) );\par
Serial.print(" X=");\par
Serial.print(trans[0]);\par
Serial.print(" Y=");\par
Serial.print(trans[1]);\par
Serial.print(" Z=");\par
Serial.println(trans[2]);\par
\}\par
\par
void writeTo(int DEVICE, byte address, byte val) \{\par
Wire.beginTransmission(DEVICE); //start transmission to ACC\par
Wire.write(address); // send register address\par
Wire.write(val); // send value to write\par
Wire.endTransmission(); //end transmission\par
\}\par
\par
//reads number of bytes starting from address register on ACC in to buff array\par
void readFrom(int DEVICE, byte address, int num, byte buff[]) \{\par
Wire.beginTransmission(DEVICE); //start transmission to ACC\par
Wire.write(address); //sends address to read from\par
Wire.endTransmission(); //end transmission\par
\par
Wire.beginTransmission(DEVICE); //start transmission to ACC\par
Wire.requestFrom(DEVICE, num); // request 6 bytes from ACC\par
int i = 0;\par
while(Wire.available()) //ACC may send less than requested (abnormal)\par
\{\par
buff[i] = Wire.read(); // receive a byte\par
i++;\par
\}\par
Wire.endTransmission(); //end transmission\par
\}\par
}