|
| 1 | +/* |
| 2 | + Example for Serial Studio and Nano 33 IoT |
| 3 | + Reads a joystick on A0, A1, and D2 |
| 4 | + and the accelerometer and gyro on a Nano 33 IoT |
| 5 | + Serial Studio download: https://github.com/Serial-Studio/Serial-Studio/tags |
| 6 | + Tutorial: https://www.alex-spataru.com/blog/introducing-serial-studio |
| 7 | + Uses the following library: |
| 8 | + http://librarymanager/All#Arduino_LSM6DS3 |
| 9 | + The circuit: |
| 10 | + - Joystick H axis attached to pin A0 |
| 11 | + - Joystick V axis attached to pin A1 |
| 12 | + - Nano 33 IoT built-in LSM6DS6 IMU |
| 13 | +
|
| 14 | + Included in the project folder are 3 JSON files for Serial Studio: |
| 15 | + joystick.json, imu.json, combined.json |
| 16 | + Comment out the lines you don't need |
| 17 | +
|
| 18 | + created 1 Feb 2021 |
| 19 | + by Tom Igoe |
| 20 | +*/ |
| 21 | +#include <Arduino_LSM6DS3.h> |
| 22 | + |
| 23 | +// pin for the pushbutton: |
| 24 | +const int buttonPin = 2; |
| 25 | + |
| 26 | +void setup() { |
| 27 | + Serial.begin(9600); |
| 28 | + // stop until serial port is open: |
| 29 | + while (!Serial); |
| 30 | + // initialize the pushbutton: |
| 31 | + pinMode(buttonPin, INPUT_PULLUP); |
| 32 | + // initialise the IMU: |
| 33 | + if (!IMU.begin()) { |
| 34 | + Serial.println("Failed to initialize IMU!"); |
| 35 | + while (true); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +void loop() { |
| 40 | + // arrays for the accel and gyro readings: |
| 41 | + float accel[3]; |
| 42 | + float gyro[3]; |
| 43 | + |
| 44 | + // read joystick: h, v, button |
| 45 | + int horizontal = analogRead(A0); |
| 46 | + delay(1); |
| 47 | + int vertical = analogRead(A1); |
| 48 | + int buttonState = digitalRead(buttonPin); |
| 49 | + // read accelerometer: |
| 50 | + if (IMU.accelerationAvailable()) { |
| 51 | + IMU.readAcceleration(accel[0], accel[1], accel[2]); |
| 52 | + } |
| 53 | + // read gyroscope: |
| 54 | + if (IMU.gyroscopeAvailable()) { |
| 55 | + IMU.readGyroscope(gyro[0], gyro[1], gyro[2]); |
| 56 | + } |
| 57 | + // print them all: |
| 58 | + Serial.print("/*"); |
| 59 | + Serial.print("Joystick and IMU,"); |
| 60 | + Serial.print(horizontal); |
| 61 | + Serial.print(","); |
| 62 | + Serial.print(vertical); |
| 63 | + Serial.print(","); |
| 64 | + Serial.print(buttonState); |
| 65 | + for (int axis = 0; axis < 3; axis++) { |
| 66 | + Serial.print(","); |
| 67 | + Serial.print(accel[axis]); |
| 68 | + } |
| 69 | + for (int axis = 0; axis < 3; axis++) { |
| 70 | + Serial.print(","); |
| 71 | + Serial.print(gyro[axis]); |
| 72 | + } |
| 73 | + Serial.println("*/"); |
| 74 | +} |
0 commit comments