Created
October 2, 2015 19:57
-
-
Save cmsunu28/6527df2f6d6c1ebf1694 to your computer and use it in GitHub Desktop.
arc reactor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This #include statement was automatically added by the Particle IDE. | |
#include "InternetButton/InternetButton.h" | |
#include "math.h" | |
// SYSTEM_MODE(SEMI_AUTOMATIC); | |
long startTime2 = 0; | |
int alreadyDown2 = 0; | |
InternetButton b = InternetButton(); | |
int state=0; // sets state of the reactor | |
// 0: breathing quietly | |
// 1: received notice of repulsor firing | |
// 2: currently firing with repulsor | |
// 3: turned off | |
float pi = 3.1415927; // this is pi. | |
int dischargeStart; // will mark the point when we start firing with repulsor | |
int rBase = 30; // base brightness of red LED | |
int gBase = 30; // base brightness of green LED | |
int bBase = 60; // base brightness of blue LED | |
int breathTime = 6000; // milliseconds in a period for sinusoidal brightness function in brightnessCalc() | |
int rAmp = 20; // amplitude of the red LED | |
int gAmp = 20; // amplitude of the green LED | |
int bAmp = 50; // amplitude of the blue LED | |
int fireTime = 200; // milliseconds passing while firing repulsor | |
void lightResponse(const char *event, const char *data) { | |
state=1; // light response just sets the state to 1, indicating that a repulsor has been fired. | |
} | |
int brightnessCalc(int baseBrightness, int amplitude, int period) { | |
// we will execute a sinusoidal function to determine the brightness of the LED | |
// start by finding where we are in the period | |
int t = millis() % period; | |
// this modulo will tell you where to be in the wave function | |
// The period of this wave is 2pi, so we will take the sin of t*2*pi/waveperiod | |
// and use the result to calibrate the brightness of the button | |
float brightnessFactor = sin(2*pi*t/period); | |
// calibrate the brightness of this LED based on the amplitude and baseBrightness | |
// baseBrightness is the value of the LED at sin(0) | |
// amplitude cannot be more than (255-baseBrightness)/2 | |
int brightnessValue = brightnessFactor*amplitude + baseBrightness; | |
return brightnessValue; | |
} | |
int dischargeCalc(int baseBrightness, int maxBrightness, int period) { // this does a similar thing to brightnessCalc, but instead of a sinusoidal function it is linear | |
int t = millis() % period; // find where we are in the function | |
float brightnessSlope = (maxBrightness-baseBrightness)/period; // calculate the slope | |
int brightnessValue=baseBrightness+brightnessSlope*t; // calibrate the brightness based on the slope and time | |
return brightnessValue; | |
} | |
// this is so you can turn the RGB Led on your core off if you just want to see the pretty lights from your arc reactor. | |
int rgbOnOff(String command) { | |
if (command=="off") { | |
RGB.control(true); | |
RGB.color(0,0,0); | |
return 0; | |
} | |
else if (command=="on") { | |
RGB.control(false); | |
return 1; | |
} | |
else { | |
return -1; | |
} | |
} | |
//this allows you to turn the arc reactor on and off | |
int arcOnOff(String command) { | |
if (command=="on") { | |
state=0; | |
return 1; | |
} | |
else if (command=="off") { | |
state=3; | |
return 0; | |
} | |
else { | |
return -1; | |
} | |
} | |
void setup() { // this function runs once when the device turns on or is reset | |
b.begin(); | |
Particle.function("rgb",rgbOnOff); // define a function called rgb that can be called from the cloud, referring to rgbOnOff() | |
Particle.function("arc",arcOnOff); // define a function called arc that can be called from the cloud, referring to arcOnOff() | |
// subscribe to the "fire_repulsor" event (see repulsor.ino) and call lightResponse() when the event occurs: | |
Spark.subscribe("fire_repulsor", lightResponse); | |
//Particle.subscribe("fire_repulsor", lightResponse, MY_DEVICES); // <---- uncomment this line and comment the above line to subscribe to private event instead | |
} | |
void loop() { // this function runs continuously, over and over again, until the end of time | |
if(b.buttonOn(1)){ | |
if(alreadyDown2 == 1){ | |
if(millis() - startTime2 > 2000){ | |
Spark.connect(); | |
} | |
} | |
else { | |
alreadyDown2 = 1; | |
startTime2 = millis(); | |
} | |
delay(10); | |
} | |
else { | |
alreadyDown2 = 0; | |
} | |
if (state==0) { | |
// pulse in a basic breathing motion whenever nothing is happening | |
// be slightly blue | |
b.allLedsOn(brightnessCalc(rBase,rAmp,breathTime),brightnessCalc(gBase,gAmp,breathTime),brightnessCalc(bBase,bAmp,breathTime)); | |
} | |
else if (state==1) { // just started discharge for the first time, a transient state triggered by subscribed event | |
dischargeStart=millis(); | |
state=2; | |
} | |
else if (state==2) { // currently discharging | |
if (millis()<(dischargeStart+500)) { // still discharging | |
// glow crazy bright for a second | |
b.allLedsOn(dischargeCalc(rBase, 255, fireTime),dischargeCalc(gBase, 255, fireTime),dischargeCalc(bBase, 0, fireTime)); | |
} | |
else { // no longer discharging | |
state=0; | |
} | |
} | |
else if (state==3) { | |
b.allLedsOff(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment