Skip to content

Commit 7b4f4e4

Browse files
committed
reverting BLE tutorial
1 parent 26661ea commit 7b4f4e4

File tree

1 file changed

+91
-103
lines changed
  • content/hardware/03.nano/boards/nano-33-ble/tutorials/imu-accelerometer

1 file changed

+91
-103
lines changed

content/hardware/03.nano/boards/nano-33-ble/tutorials/imu-accelerometer/content.md

Lines changed: 91 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ An accelerometer is an electromechanical device used to measure acceleration for
6868
In this example, we will use the accelerometer as a "level" that will provide information about the position of the board. With this application we will be able to read what the relative position of the board is as well as the degrees, by tilting the board up, down, left or right.
6969

7070

71+
7172
## Creating the Program
7273

7374
**1. Setting up**
7475

75-
7676
Let's start by opening the Arduino Cloud Editor, click on the **Libraries** tab and search for the **LSM9DS1** library. Then in **> Examples**, open the **SimpleAccelerometer** sketch and once it opens, rename it as **Accelerometer**.
7777

7878
![Finding the library in the Cloud Editor.](./assets/nano33BLE_01_include_library.png)
@@ -81,125 +81,109 @@ Let's start by opening the Arduino Cloud Editor, click on the **Libraries** tab
8181

8282
Now, connect the Arduino Nano 33 BLE to the computer and make sure that the Cloud Editor recognizes it, if so, the board and port should appear as shown in the image below. If they don't appear, follow the [instructions](https://create.arduino.cc/getting-started/plugin/welcome) to install the plugin that will allow the Editor to recognize your board.
8383

84-
Now, connect the Arduino Nano 33 BLE to the computer and make sure that the Cloud Editor recognizes it. If so, the board and port should appear as shown in the image below. If they don't appear, follow the [instructions](https://create.arduino.cc/getting-started/plugin/welcome) to install the plugin that will allow the Editor to recognize your board.
8584

8685
![Selecting the board.](assets/nano33BLE_01_board_port.png)
8786

88-
### 3. Writing the Code
87+
**3. Printing the relative position**
8988

90-
Now we will write the code to read the accelerometer data, calculate the tilt angles, and print the relative position of the board as we move it at different angles.
89+
Now we will need to modify the code on the example, in order to print the relative position of the board as we move it in different angles.
9190

92-
**Include the LSM9DS1 library at the top of your sketch:**
91+
Let's start by initializing the the x, y, z axes as `float` data types, and the `int degreesX = 0;` and `int degreesY = 0;` variables before the `setup()`.
9392

94-
```arduino
95-
#include <Arduino_LSM9DS1.h>
96-
```
93+
In the `setup()` we should **remove** the following lines of code:
9794

98-
**Initialize variables before the `setup()` function:**
9995

10096
```arduino
101-
#define MINIMUM_TILT 5 // Threshold for tilt detection in degrees
102-
#define WAIT_TIME 500 // How often to run the code (in milliseconds)
103-
104-
float x, y, z;
105-
int angleX = 0;
106-
int angleY = 0;
107-
unsigned long previousMillis = 0;
97+
Serial.println();
98+
Serial.println("Acceleration in G's");
99+
Serial.println("X\tY\tZ");
108100
```
109101

110-
**In the `setup()`, initialize the IMU and start serial communication:**
111-
112-
```arduino
113-
void setup() {
114-
Serial.begin(9600);
115-
while (!Serial);
116-
117-
if (!IMU.begin()) {
118-
Serial.println("Failed to initialize IMU!");
119-
while (1);
120-
}
121-
122-
Serial.print("Accelerometer sample rate = ");
123-
Serial.print(IMU.accelerationSampleRate());
124-
Serial.println(" Hz");
125-
}
126-
```
102+
Since the raw values of the three axes will not be required, we can remove the lines which will print these. Similarly, we should **remove** the following lines from the `loop()`:
127103

128-
**Write the `loop()` function to read accelerometer data and calculate tilt angles:**
129104

130105
```arduino
131-
void loop() {
132-
unsigned long currentMillis = millis();
106+
Serial.print(x);
107+
Serial.print('\t');
108+
Serial.print(y);
109+
Serial.print('\t');
110+
Serial.println(z);
111+
```
133112

134-
if (IMU.accelerationAvailable() && (currentMillis - previousMillis >= WAIT_TIME)) {
135-
previousMillis = currentMillis;
113+
Instead, in the `loop()` we tell the sensor to begin reading the values for the three axes. In this example we will not be using the readings from the Z axis as it is not required for this application to function, therefore you could remove it.
136114

137-
IMU.readAcceleration(x, y, z);
115+
After the `IMU.readAcceleration` initialization, we add four `if` statements for the board's different positions. The statements will calculate the direction in which the board will be tilting towards, as well as provide the axe's degree values.
138116

139-
// Calculate tilt angles in degrees
140-
angleX = atan2(x, sqrt(y * y + z * z)) * 180 / PI;
141-
angleY = atan2(y, sqrt(x * x + z * z)) * 180 / PI;
142-
143-
// Determine the tilting direction based on angleX and angleY
144-
if (angleX > MINIMUM_TILT) { // Tilting up
145-
Serial.print("Tilting up ");
146-
Serial.print(angleX);
147-
Serial.println(" degrees");
148-
} else if (angleX < -MINIMUM_TILT) { // Tilting down
149-
Serial.print("Tilting down ");
150-
Serial.print(-angleX);
151-
Serial.println(" degrees");
117+
```arduino
118+
if(x > 0.1){
119+
x = 100*x;
120+
degreesX = map(x, 0, 97, 0, 90);
121+
Serial.print("Tilting up ");
122+
Serial.print(degreesX);
123+
Serial.println(" degrees");
152124
}
153-
154-
if (angleY > MINIMUM_TILT) { // Tilting left
155-
Serial.print("Tilting left ");
156-
Serial.print(angleY);
157-
Serial.println(" degrees");
158-
} else if (angleY < -MINIMUM_TILT) { // Tilting right
159-
Serial.print("Tilting right ");
160-
Serial.print(-angleY);
161-
Serial.println(" degrees");
125+
if(x < -0.1){
126+
x = 100*x;
127+
degreesX = map(x, 0, -100, 0, 90);
128+
Serial.print("Tilting down ");
129+
Serial.print(degreesX);
130+
Serial.println(" degrees");
162131
}
163-
}
164-
}
132+
if(y > 0.1){
133+
y = 100*y;
134+
degreesY = map(y, 0, 97, 0, 90);
135+
Serial.print("Tilting left ");
136+
Serial.print(degreesY);
137+
Serial.println(" degrees");
138+
}
139+
if(y < -0.1){
140+
y = 100*y;
141+
degreesY = map(y, 0, -100, 0, 90);
142+
Serial.print("Tilting right ");
143+
Serial.print(degreesY);
144+
Serial.println(" degrees");
145+
}
146+
165147
```
166148

167-
In this code, we use trigonometric functions to calculate the tilt angles from the accelerometer data. The `MINIMUM_TILT` variable is used to ignore small movements below a certain threshold.
149+
Lastly, we `Serial.print` the results value and add a `delay(1000);`.
150+
151+
>**Note:** For the following code to properly work, the board's facing direction and inclination during the initialization of the code, need to be specific. More information will be shared on the "testing it out" section.
168152
169-
> **Note:** For the following code to work properly, the board's facing direction and inclination during the initialization of the code need to be specific. More information will be shared in the "Testing It Out" section.
170153

171-
**Complete Code**
172154

173-
If you choose to skip the code-building section, the complete code can be found below:
174155

156+
**4. Complete code**
157+
158+
If you choose to skip the code building section, the complete code can be found below:
175159
```arduino
176160
/*
177161
Arduino LSM9DS1 - Accelerometer Application
178162
179163
This example reads the acceleration values as relative direction and degrees,
180-
from the LSM9DS1 sensor and prints them to the Serial Monitor.
164+
from the LSM9DS1 sensor and prints them to the Serial Monitor or Serial Plotter.
181165
182166
The circuit:
183167
- Arduino Nano 33 BLE
184168
185-
Created by Pedro Lima
169+
Created by Riccardo Rizzo
170+
171+
Modified by Jose García
172+
27 Nov 2020
186173
187174
This example code is in the public domain.
188175
*/
189176
190177
#include <Arduino_LSM9DS1.h>
191178
192-
#define MINIMUM_TILT 5 // Threshold for tilt detection in degrees
193-
#define WAIT_TIME 500 // How often to run the code (in milliseconds)
194-
195179
float x, y, z;
196-
int angleX = 0;
197-
int angleY = 0;
198-
unsigned long previousMillis = 0;
180+
int degreesX = 0;
181+
int degreesY = 0;
199182
200183
void setup() {
201184
Serial.begin(9600);
202185
while (!Serial);
186+
Serial.println("Started");
203187
204188
if (!IMU.begin()) {
205189
Serial.println("Failed to initialize IMU!");
@@ -208,43 +192,47 @@ void setup() {
208192
209193
Serial.print("Accelerometer sample rate = ");
210194
Serial.print(IMU.accelerationSampleRate());
211-
Serial.println(" Hz");
195+
Serial.println("Hz");
212196
}
213197
214198
void loop() {
215-
unsigned long currentMillis = millis();
216-
217-
if (IMU.accelerationAvailable() && (currentMillis - previousMillis >= WAIT_TIME)) {
218-
previousMillis = currentMillis;
219199
200+
if (IMU.accelerationAvailable()) {
220201
IMU.readAcceleration(x, y, z);
221202
222-
// Calculate tilt angles in degrees
223-
angleX = atan2(x, sqrt(y * y + z * z)) * 180 / PI;
224-
angleY = atan2(y, sqrt(x * x + z * z)) * 180 / PI;
225-
226-
// Determine the tilting direction based on angleX and angleY
227-
if (angleX > MINIMUM_TILT) { // Tilting up
228-
Serial.print("Tilting up ");
229-
Serial.print(angleX);
230-
Serial.println(" degrees");
231-
} else if (angleX < -MINIMUM_TILT) { // Tilting down
232-
Serial.print("Tilting down ");
233-
Serial.print(-angleX);
234-
Serial.println(" degrees");
235-
}
203+
}
236204
237-
if (angleY > MINIMUM_TILT) { // Tilting left
238-
Serial.print("Tilting left ");
239-
Serial.print(angleY);
240-
Serial.println(" degrees");
241-
} else if (angleY < -MINIMUM_TILT) { // Tilting right
242-
Serial.print("Tilting right ");
243-
Serial.print(-angleY);
244-
Serial.println(" degrees");
245-
}
205+
if (x > 0.1) {
206+
x = 100 * x;
207+
degreesX = map(x, 0, 97, 0, 90);
208+
Serial.print("Tilting up ");
209+
Serial.print(degreesX);
210+
Serial.println(" degrees");
211+
}
212+
if (x < -0.1) {
213+
x = 100 * x;
214+
degreesX = map(x, 0, -100, 0, 90);
215+
Serial.print("Tilting down ");
216+
Serial.print(degreesX);
217+
Serial.println(" degrees");
218+
}
219+
if (y > 0.1) {
220+
y = 100 * y;
221+
degreesY = map(y, 0, 97, 0, 90);
222+
Serial.print("Tilting left ");
223+
Serial.print(degreesY);
224+
Serial.println(" degrees");
246225
}
226+
if (y < -0.1) {
227+
y = 100 * y;
228+
degreesY = map(y, 0, -100, 0, 90);
229+
Serial.print("Tilting right ");
230+
Serial.print(degreesY);
231+
Serial.println(" degrees");
232+
}
233+
delay(1000);
247234
}
235+
248236
```
249237

250238

0 commit comments

Comments
 (0)