-
Notifications
You must be signed in to change notification settings - Fork 0
/
04.7-kiihtyvyyden_mittaaminen.ino
122 lines (100 loc) · 3.45 KB
/
04.7-kiihtyvyyden_mittaaminen.ino
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <SPI.h> // Including SPI-library and defining the four pins of accelerometer
#define SS 10
#define MOSI 11
#define MISO 12
#define SCK 13
#define SCALE 0.00007019 // LIS331 measures +-24 g so the scale is 48 G's in total (G = 9,81 m/s^2). Accelerations are measured in packages of 16 bits so the scale is: 48/2^16 = 0.00007019
double xAcc, yAcc, zAcc; // Three variables of type double. These store the values of accelerations measured in x-, y- and z-directions
double xMax, yMax, zMax; // These variables store the maximum values of each direction (x, y and z)
void setup() {
pinMode(6, OUTPUT);
Serial.begin(9600);
while (!Serial);
SPI_SETUP();
Accelerometer_Setup();
}
void loop() {
ReadAccVal(); //Calling the ReadAccVall-function
//Printing the accelerations in x-, y- and z-directions.
Serial.print("X="); //First, the string "X=" is printed and then the value of variable "xAcc", accurate to one decimal
Serial.print(xAcc, 1);
Serial.print(" Y=");
Serial.print(yAcc, 1);
Serial.print(" Z=");
Serial.print(zAcc, 1);
Serial.print("\t");
Serial.print("X=");
Serial.print(xMax, 1);
Serial.print(" yMax=");
Serial.print(yMax, 1);
Serial.print(" zMax=");
Serial.println(zMax, 1);
//magicWand();
}
void magicWand(){
int pitch = (xAcc + yAcc + zAcc) * 4;
tone(6, pitch);
}
void ReadAccVal(void){
byte xAddressByteL = 0x28; // Low Byte of X value (the first data register)
byte readBit = B10000000; // Bit 0 (MSB) HIGH means read register
byte incrementBit = B01000000; // Bit 1 HIGH means keep incrementing registers
byte dataByte = xAddressByteL | readBit | incrementBit;
byte b0 = 0x0; // An empty byte
digitalWrite(SS, LOW); //Setting SS to low to communicate with accelerometer
SPI.transfer(dataByte);
//Sending empty byte (b0) to accelerometer and receiving bytes that indicate accelerations of each direction.
byte xL = SPI.transfer(b0); // get the low byte of X data
byte xH = SPI.transfer(b0); // get the high byte of X data
byte yL = SPI.transfer(b0); // get the low byte of Y data
byte yH = SPI.transfer(b0); // get the high byte of Y data
byte zL = SPI.transfer(b0); // get the low byte of Z data
byte zH = SPI.transfer(b0); // get the high byte of Z data
digitalWrite(SS, HIGH); //Setting SS to low to communicate with accelerometer
int xVal = (xL | (xH << 8)); // Doing a bitshift so that "xVal" is "xH" and "xL" combined, a number of sixteen bits.
int yVal = (yL | (yH << 8));
int zVal = (zL | (zH << 8));
xAcc = xVal * SCALE; // Transforming the 16 bit -values to G's.
yAcc = yVal * SCALE;
zAcc = zVal * SCALE;
if(xAcc > xMax){
xMax = xAcc;
}
if(yAcc > yMax){
yMax = yAcc;
}
if(zAcc > zMax){
zMax = zAcc;
}
}
void SPI_SETUP()
{
pinMode(SS, OUTPUT);
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0);
SPI.setClockDivider(SPI_CLOCK_DIV8);
}
void Accelerometer_Setup(void)
{
byte addressByte = 0x21;
byte ctrlRegByte = 0x00;
digitalWrite(SS, LOW);
SPI.transfer(addressByte);
SPI.transfer(ctrlRegByte);
digitalWrite(SS, HIGH);
delay(100);
addressByte = 0x22;
ctrlRegByte = 0x00;
digitalWrite(SS, LOW);
SPI.transfer(addressByte);
SPI.transfer(ctrlRegByte);
digitalWrite(SS, HIGH);
delay(100);
addressByte = 0x20;
ctrlRegByte = 0x47;
digitalWrite(SS, LOW);
SPI.transfer(addressByte);
SPI.transfer(ctrlRegByte);
digitalWrite(SS, HIGH);
}