Skip to content

Commit 6b063b0

Browse files
author
brentru
committed
add time cube example
1 parent f522bf1 commit 6b063b0

2 files changed

Lines changed: 209 additions & 0 deletions

File tree

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
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+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/************************ Adafruit IO Config *******************************/
2+
3+
// visit io.adafruit.com if you need to create an account,
4+
// or if you need your Adafruit IO key.
5+
#define IO_USERNAME "USER"
6+
#define IO_KEY "KEY"
7+
8+
/******************************* WIFI **************************************/
9+
10+
// the AdafruitIO_WiFi client will work with the following boards:
11+
// - HUZZAH ESP8266 Breakout -> https://www.adafruit.com/products/2471
12+
// - Feather HUZZAH ESP8266 -> https://www.adafruit.com/products/2821
13+
// - Feather HUZZAH ESP32 -> https://www.adafruit.com/product/3405
14+
// - Feather M0 WiFi -> https://www.adafruit.com/products/3010
15+
// - Feather WICED -> https://www.adafruit.com/products/3056
16+
17+
#define WIFI_SSID "WIFISSID"
18+
#define WIFI_PASS "WIFIPASS"
19+
20+
// comment out the following two lines if you are using fona or ethernet
21+
#include "AdafruitIO_WiFi.h"
22+
AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS);
23+
24+
25+
/******************************* FONA **************************************/
26+
27+
// the AdafruitIO_FONA client will work with the following boards:
28+
// - Feather 32u4 FONA -> https://www.adafruit.com/product/3027
29+
30+
// uncomment the following two lines for 32u4 FONA,
31+
// and comment out the AdafruitIO_WiFi client in the WIFI section
32+
// #include "AdafruitIO_FONA.h"
33+
// AdafruitIO_FONA io(IO_USERNAME, IO_KEY);
34+
35+
36+
/**************************** ETHERNET ************************************/
37+
38+
// the AdafruitIO_Ethernet client will work with the following boards:
39+
// - Ethernet FeatherWing -> https://www.adafruit.com/products/3201
40+
41+
// uncomment the following two lines for ethernet,
42+
// and comment out the AdafruitIO_WiFi client in the WIFI section
43+
// #include "AdafruitIO_Ethernet.h"
44+
// AdafruitIO_Ethernet io(IO_USERNAME, IO_KEY);

0 commit comments

Comments
 (0)