Skip to content

Commit 52051e0

Browse files
authored
Merge pull request #74 from brentru/time-cube-example
Zapier Example
2 parents f522bf1 + c05f0fd commit 52051e0

File tree

2 files changed

+282
-0
lines changed

2 files changed

+282
-0
lines changed
Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
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+
#include <Adafruit_NeoPixel.h>
27+
28+
// Prop-Maker Wing
29+
#define NEOPIXEL_PIN 2
30+
#define POWER_PIN 15
31+
#define RED_LED 13
32+
#define GREEN_LED 12
33+
#define BLUE_LED 14
34+
35+
// Used for Pizeo
36+
#define PIEZO_PIN 0
37+
38+
// NeoPixel Pin
39+
#define NEOPIXEL_PIN 5
40+
41+
// # of Pixels Attached
42+
#define NUM_PIXELS 8
43+
44+
// Used for software SPI
45+
#define LIS3DH_CLK 13
46+
#define LIS3DH_MISO 12
47+
#define LIS3DH_MOSI 11
48+
// Used for hardware & software SPI
49+
#define LIS3DH_CS 10
50+
51+
// Adafruit_LIS3DH Setup
52+
Adafruit_LIS3DH lis = Adafruit_LIS3DH();
53+
54+
// NeoPixel Setup
55+
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_PIXELS, NEOPIXEL_PIN, NEO_GRB + NEO_KHZ800);
56+
57+
// Set up the 'cubeTask' feed
58+
AdafruitIO_Feed *cubetask = io.feed("cubetask");
59+
60+
/* Time Tracking Cube States
61+
* 1: Cube Tilted Left
62+
* 2: Cube Tilted Right
63+
* 3: Cube Neutral, Top
64+
*/
65+
int cubeState = 0;
66+
67+
// Previous cube orientation state
68+
int prvCubeState = 0;
69+
70+
// Tasks (change these to what you're currently working on)
71+
String taskOne = "Write Learn Guide";
72+
String taskTwo = "Write Code";
73+
74+
// Adafruit IO sending delay, in seconds
75+
int sendDelay = 0.5;
76+
77+
// Time-Keeping
78+
unsigned long currentTime;
79+
unsigned long prevTime;
80+
int seconds = 0;
81+
int minutes = 0;
82+
83+
void setup()
84+
{
85+
// start the serial connection
86+
Serial.begin(9600);
87+
// wait for serial monitor to open
88+
while (!Serial)
89+
;
90+
Serial.println("Adafruit IO Time Tracking Cube");
91+
92+
// enabling RGB and NeoPixels on Prop-Maker Featherwing
93+
pinMode(POWER_PIN, OUTPUT);
94+
digitalWrite(POWER_PIN, LOW);
95+
pinMode(RED_LED, OUTPUT);
96+
pinMode(GREEN_LED, OUTPUT);
97+
pinMode(BLUE_LED, OUTPUT);
98+
99+
analogWrite(RED_LED, 0);
100+
analogWrite(GREEN_LED, 0);
101+
analogWrite(BLUE_LED, 0);
102+
103+
104+
// Initialize LIS3DH
105+
if (!lis.begin(0x18))
106+
{
107+
Serial.println("Couldnt start");
108+
while (1)
109+
;
110+
}
111+
Serial.println("LIS3DH found!");
112+
lis.setRange(LIS3DH_RANGE_4_G);
113+
114+
// Initialize NeoPixel Strip
115+
strip.begin();
116+
Serial.println("Pixels init'd");
117+
118+
// connect to io.adafruit.com
119+
Serial.print("Connecting to Adafruit IO");
120+
io.connect();
121+
122+
// wait for a connection
123+
while (io.status() < AIO_CONNECTED)
124+
{
125+
Serial.print(".");
126+
delay(500);
127+
}
128+
129+
// we are connected
130+
Serial.println();
131+
Serial.println(io.statusText());
132+
133+
}
134+
135+
void updateTime()
136+
{
137+
// grab the current time from millis()
138+
currentTime = millis() / 1000;
139+
seconds = currentTime - prevTime;
140+
// increase mins.
141+
if (seconds == 60)
142+
{
143+
prevTime = currentTime;
144+
minutes++;
145+
}
146+
}
147+
148+
void updatePixels(uint8_t red, uint8_t green, uint8_t blue)
149+
{
150+
// updates pixels on the prop-maker featherwing
151+
digitalWrite(POWER_PIN, HIGH);
152+
analogWrite(RED_LED, 0);
153+
analogWrite(GREEN_LED, 0);
154+
analogWrite(BLUE_LED, 0);
155+
for (int p=0; p<NUM_PIXELS; p++) {
156+
strip.setPixelColor(p, red, green, blue);
157+
}
158+
strip.show();
159+
}
160+
161+
void loop()
162+
{
163+
// io.run(); is required for all sketches.
164+
// it should always be present at the top of your loop
165+
// function. it keeps the client connected to
166+
// io.adafruit.com, and processes any incoming data.
167+
io.run();
168+
169+
// Update the timer
170+
updateTime();
171+
172+
// Get a normalized sensor reading
173+
sensors_event_t event;
174+
lis.getEvent(&event);
175+
176+
// Detect cube face orientation
177+
if (event.acceleration.x > 9 && event.acceleration.x < 10)
178+
{
179+
//Serial.println("Cube TILTED: Left");
180+
cubeState = 1;
181+
}
182+
else if (event.acceleration.x < -9)
183+
{
184+
//Serial.println("Cube TILTED: Right");
185+
cubeState = 2;
186+
}
187+
else if(event.acceleration.y < 0 && event.acceleration.y > -1)
188+
{
189+
cubeState = 3;
190+
}
191+
else
192+
{ // orientation not specified
193+
Serial.println("Cube Idle...");
194+
}
195+
196+
// return if the orientation hasn't changed
197+
if (cubeState == prvCubeState)
198+
return;
199+
200+
// Send to Adafruit IO based off of the orientation of the cube
201+
switch (cubeState)
202+
{
203+
case 1:
204+
Serial.println("Switching to Task 1");
205+
// update the neopixel strip
206+
updatePixels(50, 0, 0);
207+
// play a sound
208+
tone(PIEZO_PIN, 650, 300);
209+
Serial.print("Sending to Adafruit IO -> ");
210+
Serial.println(taskTwo);
211+
cubetask->save(taskTwo, minutes);
212+
// reset the timer
213+
minutes = 0;
214+
break;
215+
case 2:
216+
Serial.println("Switching to Task 2");
217+
// update the neopixel strip
218+
updatePixels(0, 50, 0);
219+
// play a sound
220+
tone(PIEZO_PIN, 850, 300);
221+
Serial.print("Sending to Adafruit IO -> ");
222+
Serial.println(taskOne);
223+
cubetask->save(taskOne, minutes);
224+
// reset the timer
225+
minutes = 0;
226+
break;
227+
case 3:
228+
updatePixels(0, 0, 50);
229+
tone(PIEZO_PIN, 950, 300);
230+
break;
231+
}
232+
233+
// save cube state
234+
prvCubeState = cubeState;
235+
236+
// Delay the send to Adafruit IO
237+
delay(sendDelay * 1000);
238+
}
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 "your_username"
6+
#define IO_KEY "your_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 "your_ssid"
18+
#define WIFI_PASS "your_pass"
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)