|
| 1 | +// Adafruit IO Time Tracking Cube |
| 2 | +// Tutorial Link: https://learn.adafruit.com/time-tracking-cube |
| 3 | +// |
| 4 | +// Adafruit invests time and resources providing this open source code. |
| 5 | +// Please support Adafruit and open source hardware by purchasing |
| 6 | +// products from Adafruit! |
| 7 | +// |
| 8 | +// Written by Brent Rubell for Adafruit Industries |
| 9 | +// Copyright (c) 2019 Adafruit Industries |
| 10 | +// Licensed under the MIT license. |
| 11 | +// |
| 12 | +// All text above must be included in any redistribution. |
| 13 | + |
| 14 | +/************************** Configuration ***********************************/ |
| 15 | + |
| 16 | +// edit the config.h tab and enter your Adafruit IO credentials |
| 17 | +// and any additional configuration needed for WiFi, cellular, |
| 18 | +// or ethernet clients. |
| 19 | +#include "config.h" |
| 20 | + |
| 21 | +/************************ Example Starts Here *******************************/ |
| 22 | +#include <Wire.h> |
| 23 | +#include <SPI.h> |
| 24 | +#include <Adafruit_LIS3DH.h> |
| 25 | +#include <Adafruit_Sensor.h> |
| 26 | + |
| 27 | +// set DEBUG to True to view accel outputs |
| 28 | +#define DEBUG false |
| 29 | +// Used for Pizeo |
| 30 | +#define PIEZO_PIN 0 |
| 31 | +// Used for software SPI |
| 32 | +#define LIS3DH_CLK 13 |
| 33 | +#define LIS3DH_MISO 12 |
| 34 | +#define LIS3DH_MOSI 11 |
| 35 | +// Used for hardware & software SPI |
| 36 | +#define LIS3DH_CS 10 |
| 37 | + |
| 38 | +// I2C |
| 39 | +Adafruit_LIS3DH lis = Adafruit_LIS3DH(); |
| 40 | + |
| 41 | +// Set up the 'orientation' feed |
| 42 | +AdafruitIO_Feed *orientation = io.feed("orientation"); |
| 43 | + |
| 44 | +/* Time Tracking Cube States |
| 45 | + * 0: Neutral, Cube on Base |
| 46 | + * 1: Cube Tilted, Left on X-Axis |
| 47 | + * 2: Cube Tilted, Right on X-Axis |
| 48 | +*/ |
| 49 | +int cubeState = 0; |
| 50 | + |
| 51 | +// Previous cube orientation state |
| 52 | +int lastCubeState = 0; |
| 53 | + |
| 54 | +// Tasks |
| 55 | +String taskOne = "Write Learn Guide"; |
| 56 | +String taskTwo = "Write Code"; |
| 57 | + |
| 58 | +// Adafruit IO Sending Delay, in seconds |
| 59 | +int sendDelay = 0.5; |
| 60 | + |
| 61 | +void setup() |
| 62 | +{ |
| 63 | + // start the serial connection |
| 64 | + Serial.begin(9600); |
| 65 | + // wait for serial monitor to open |
| 66 | + while (!Serial) |
| 67 | + ; |
| 68 | + Serial.println("Adafruit IO Time Tracking Cube"); |
| 69 | + |
| 70 | + // Initialize LIS3DH |
| 71 | + if (!lis.begin(0x18)) |
| 72 | + { |
| 73 | + Serial.println("Couldnt start"); |
| 74 | + while (1) |
| 75 | + ; |
| 76 | + } |
| 77 | + Serial.println("LIS3DH found!"); |
| 78 | + lis.setRange(LIS3DH_RANGE_4_G); |
| 79 | + |
| 80 | + // connect to io.adafruit.com |
| 81 | + Serial.print("Connecting to Adafruit IO"); |
| 82 | + io.connect(); |
| 83 | + |
| 84 | + // wait for a connection |
| 85 | + while (io.status() < AIO_CONNECTED) |
| 86 | + { |
| 87 | + Serial.print("."); |
| 88 | + delay(500); |
| 89 | + } |
| 90 | + |
| 91 | + // we are connected |
| 92 | + Serial.println(); |
| 93 | + Serial.println(io.statusText()); |
| 94 | +} |
| 95 | + |
| 96 | +void loop() |
| 97 | +{ |
| 98 | + |
| 99 | + // io.run(); is required for all sketches. |
| 100 | + // it should always be present at the top of your loop |
| 101 | + // function. it keeps the client connected to |
| 102 | + // io.adafruit.com, and processes any incoming data. |
| 103 | + io.run(); |
| 104 | + |
| 105 | + // Get a normalized sensor reading |
| 106 | + sensors_event_t event; |
| 107 | + lis.getEvent(&event); |
| 108 | + |
| 109 | +// Optionally display the accelerometer values |
| 110 | +#ifdef DEBUG |
| 111 | + Serial.print("\t\tX: "); |
| 112 | + Serial.print(event.acceleration.x); |
| 113 | + Serial.print(" \tY: "); |
| 114 | + Serial.print(event.acceleration.y); |
| 115 | + Serial.print(" \tZ: "); |
| 116 | + Serial.print(event.acceleration.z); |
| 117 | + Serial.println(" m/s^2 "); |
| 118 | + Serial.println(); |
| 119 | +#endif |
| 120 | + |
| 121 | + // Detect Cube Face Orientation |
| 122 | + if (event.acceleration.x > 9 && event.acceleration.x < 10) |
| 123 | + { |
| 124 | + Serial.println("Cube TILTED: Left"); |
| 125 | + cubeState = 1; |
| 126 | + } |
| 127 | + else if (event.acceleration.x < -9) |
| 128 | + { |
| 129 | + Serial.println("Cube TILTED: Right"); |
| 130 | + cubeState = 2; |
| 131 | + } |
| 132 | + else |
| 133 | + { // orientation not found |
| 134 | + Serial.println("Undefined Orientation"); |
| 135 | + } |
| 136 | + |
| 137 | + // return if the value hasn't changed |
| 138 | + if (cubeState == lastCubeState) |
| 139 | + return; |
| 140 | + |
| 141 | + // Send to Adafruit IO based off of the State |
| 142 | + switch (cubeState) |
| 143 | + { |
| 144 | + case 1: |
| 145 | + Serial.println("Playing Task 1 Sound..."); |
| 146 | + tone(PIEZO_PIN, 650, 300); |
| 147 | + Serial.print("Sending to Adafruit IO -> "); |
| 148 | + Serial.println(taskOne); |
| 149 | + orientation->save(taskOne); |
| 150 | + break; |
| 151 | + case 2: |
| 152 | + Serial.println("Playing Sound 2 Sound..."); |
| 153 | + tone(PIEZO_PIN, 850, 300); |
| 154 | + Serial.print("Sending to Adafruit IO -> "); |
| 155 | + Serial.println(taskTwo); |
| 156 | + orientation->save(taskTwo); |
| 157 | + break; |
| 158 | + } |
| 159 | + |
| 160 | + // store last cube orientation state |
| 161 | + lastCubeState = cubeState; |
| 162 | + |
| 163 | + // Delay the send to Adafruit IO |
| 164 | + delay(sendDelay * 1000); |
| 165 | +} |
0 commit comments