Skip to content

Instantly share code, notes, and snippets.

@houhr
Last active November 5, 2015 01:35
Show Gist options
  • Select an option

  • Save houhr/be5e88d058d1df1e2ea2 to your computer and use it in GitHub Desktop.

Select an option

Save houhr/be5e88d058d1df1e2ea2 to your computer and use it in GitHub Desktop.
#include <Parse.h>
#include <Wire.h>
#include "RTClib.h"
#include <Adafruit_NeoPixel.h>
#include <Bridge.h>
#include <Console.h>
#include <FileIO.h>
#include <HttpClient.h>
#include <Mailbox.h>
#include <Process.h>
#include <YunClient.h>
#include <YunServer.h>
#include <ArduinoJson.h>
// Use PIN 6 as output
#define NEOPIXELPIN 6
RTC_DS1307 rtc;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, NEOPIXELPIN, NEO_GRB + NEO_KHZ800);
int lightSensorPin = A2,
ultrasonicSensorPin = A1,
lightSensorValue = 0,
ultrasonicSensorValue = 0,
hourNum = 0,
minuteNum = 0,
secondNum = 0,
mode = 0,
r = 0,
g = 0,
b = 0;
boolean flagOne = false,
flagTwo = false;
uint32_t hourColor = strip.Color(255, 0, 0),
minuteColor = strip.Color(0, 0, 100),
hourDotColor = strip.Color(200, 200, 200);
const int BUFFER_SIZE = JSON_OBJECT_SIZE(5) + JSON_ARRAY_SIZE(0);
void rtcSetup() {
#ifdef AVR
Wire.begin();
#else
Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
#endif
rtc.begin();
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
void parseSetup() {
// Initialize digital pin 13 as an output.
pinMode(13, OUTPUT);
// Initialize Bridge
Bridge.begin();
// Initialize Serial
Serial.println("Parse Starter Project");
// Initialize Parse
Parse.begin("YOUR APPLICATION ID HERE", "YOUR CLIENT KEY HERE");
}
void setup() {
Serial.begin(9600);
rtcSetup();
strip.begin();
strip.show(); // Initialize all pixels to 'off'
parseSetup();
Parse.startPushService();
Serial.print("Push Installation ID:");
Serial.println(Parse.getInstallationId());
}
void showTime() {
DateTime now = rtc.now();
hourNum = now.hour();
minuteNum = now.minute();
secondNum = now.second();
for(int i = 0; i < minuteNum; i++) {
strip.setPixelColor(i, minuteColor);
}
setHourDot(15);
if (ultrasonicSensorValue <= 28) {
if(flagOne == false) {
flagOne = true;
} else {
setHourDot(5);
}
} else {
flagOne = false;
}
Serial.println(ultrasonicSensorValue);
Serial.println(flagOne);
Serial.println("----------");
if (hourNum % 12 == 0) {
strip.setPixelColor(59, hourColor);
} else {
strip.setPixelColor(hourNum % 12 * 5 - 1, hourColor);
}
strip.show();
delay(300);
strip.setPixelColor(minuteNum - 1, strip.Color(0, 0, 0));
strip.show();
delay(700);
turnOffAll();
strip.show();
}
void calculateBrightness() {
lightSensorValue = analogRead(lightSensorPin);
lightSensorValue = map(lightSensorValue, 0, 1024, 1, 255);
strip.setBrightness(lightSensorValue);
}
void checkPush() {
Serial.print("push checked");
if (Parse.pushAvailable()) {
ParsePush push = Parse.nextPush();
String message = push.getJSONBody();
Serial.print("New push message content: ");
Serial.println(message);
char json[message.length()+1];
message.toCharArray(json, message.length()+1);
StaticJsonBuffer<BUFFER_SIZE> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(json);
if(root.success()) {
mode = int(root["mode"]);
switch(mode) {
case 2:
rainbowCycle(5);
break;
case 3:
theaterChase(strip.Color(127, 127, 127), 50); // White
theaterChase(strip.Color(127, 0, 0), 50); // Red
theaterChase(strip.Color( 0, 0, 127), 50); // Blue
break;
default:
r = int(root["r"]);
g = int(root["g"]);
b = int(root["b"]);
minuteColor = strip.Color(r, g, b);
break;
}
}
push.close();
}
}
void loop() {
mode = 1;
checkPush();
ultrasonicSensorValue = analogRead(ultrasonicSensorPin);
calculateBrightness();
showTime();
if (minuteNum == 59 && secondNum == 59) {
rotate();
}
}
void turnOffAll() {
for (int i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, 0);
}
}
void setHourDot(int distance) {
for (int i = distance - 1; i < strip.numPixels(); i += distance) {
strip.setPixelColor(i, hourDotColor);
}
}
void rotate() {
turnOffAll();
for (int i = 0; i < strip.numPixels() + 10; i++) {
strip.setPixelColor(i, hourColor);
for (int j = 1; j < strip.numPixels(); j++) {
if (j < 11 ) {
strip.setPixelColor(i - j, strip.Color(250 - 25 * j, 0, 0));
} else {
strip.setPixelColor(i - j, strip.Color(0, 0, 0));
}
}
strip.show();
delay(14.2857143);
}
}
// ------------------Effects--------------------
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
} else {
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
}
// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
int i, j;
for(j=256*3; j>=0; j--) { // 5 cycles of all colors on wheel
for(i=0; i< strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
}
strip.show();
delay(wait);
}
}
//Theatre-style crawling lights.
void theaterChase(uint32_t c, uint8_t wait) {
for (int j=0; j<10; j++) { //do 10 cycles of chasing
for (int q=0; q < 3; q++) {
for (int i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, c); //turn every third pixel on
}
strip.show();
delay(wait);
for (int i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, 0); //turn every third pixel off
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment