// This #include statement was automatically added by the Particle IDE.
#include <Stepper.h>
// This #include statement was automatically added by the Particle IDE.
#include <neopixel.h>
// Include Particle Device OS APIs
#include "Particle.h"
// Let Device OS manage the connection to the Particle Cloud
SYSTEM_MODE(AUTOMATIC);
// IMPORTANT: Set pixel COUNT, PIN and TYPE
// MOSI pin MO
#define PIXEL_PIN SPI
#define PIXEL_COUNT 8
#define PIXEL_TYPE WS2812B
// motor controls
#define IN1 2
#define IN2 3
#define IN3 4
#define IN4 5
// Show system, cloud connectivity, and application logs over USB
// View logs with CLI using 'particle serial monitor --follow'
SerialLogHandler logHandler(LOG_LEVEL_INFO);
Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
int maxStep=-5000;
int initmaxStep = 2048;
int lastStep = 0;
// Pins entered in sequence IN1-IN3-IN2-IN4 for proper step sequence
Stepper myStepper(initmaxStep, IN1, IN3 , IN2, IN4);
int oldTi=0;
int newTi=0;
// setup() runs once, when the device is first turned on
//Button Stuff
int butPin = 7;
int oldBut = 1;
int newBut = 1;
void setup() {
// Put initialization like pinMode and begin functions here
pinMode(1,OUTPUT);
pinMode(butPin, INPUT_PULLUP);
strip.begin();
strip.show();
Serial.begin(9600);
for(int i=0;i<8;i++){
strip.setPixelColor(i,255,255,255);
}
strip.show();
while(Particle.disconnected()){}
Serial.println("Particle connected to cloud!");
myStepper.setSpeed(17);
//myStepper.step(-5000);
oldTi=(int) Time.now();
//Serial.println("Working!");
newTi=(int) Time.now();
//delay(60000);
//changeStuff(-1);
/*int result = -1 * -2000;
Serial.print("This Is The Result : ");
Serial.print(result);*/
}
// loop() runs over and over again, as quickly as it can execute.
bool offBut=false;
void loop() {
newBut=digitalRead(7);
if(newBut==0 && oldBut==1){
offBut=true;
changeStuff(-1);
}
oldBut=newBut;
//changeStuff(-1);
// The core of your code will likely live here.
//Particle.publish("Babies",getTime());
//Particle.subscribe("hook-response/Babies", singleValueHandler);
//changeStuff(1);
//delay(120000);
//Serial.println("Old Time: ");
//Serial.print(oldTi);
//Serial.println("New Time: ");
//Serial.print(newTi);
// 5 minute time check
if((newTi - oldTi)>=30){
if(offBut==false){
Particle.publish("Babies",getTime());
Particle.subscribe("hook-response/Babies", singleValueHandler);
}
//Serial.println(" Five Munites!!! ");
oldTi=newTi;
}
Serial.println("How many second passed: ");
Serial.print(newTi-oldTi);
newTi=(int) Time.now();
delay(1000);
}
void singleValueHandler(const char *event, const char *data)
{
String stock = data;
double hee = stock.toFloat();
Serial.print(" This is the market percent change: ");
Serial.println(hee);
changeStuff(hee);
}
void changeStuff(double val){
delay(500);
Serial.print("made it to change stuff");
double average = 1;
double changeD = val/average;
int changeI = changeD*100;
if(changeI>100){
changeI=100;
}
if(changeI<-100){
changeI=-100;
}
Serial.print("Change percent: ");
Serial.print(changeD);
Serial.print("Int Change: ");
Serial.print(changeI);
Serial.print("MaxStep: ");
Serial.print(maxStep);
if(changeI>0){
int ste =(maxStep*changeI);
Serial.print("Steps: ");
Serial.print(ste);
ste=ste/100;
Serial.print("ActualSteps: ");
Serial.print(ste);
Serial.print("Current Step: ");
Serial.print((-1*lastStep));
Serial.print("MoveStepsRelation: ");
int stepRelation=ste+(-1*lastStep);
Serial.print(stepRelation);
myStepper.step(stepRelation);
Serial.print("Reached");
delay(500);
lastStep=ste;
for(int i=0;i<8;i++){
strip.setPixelColor(i,255,255,255);
}
strip.show();
}else{
int lightc = ((255*changeI)/100);
Serial.print("Blue and Green color: ");
Serial.print(lightc);
//decrease B and G in RGB
for(int i=0;i<8;i++){
strip.setPixelColor(i,255,lightc,lightc);
}
strip.show();
//set stepper to origin
Serial.print("current step on: ");
Serial.print(lastStep);
Serial.print("move step to set to origin: ");
//What the line bellow should be: int test = (-1*lastStep);
int test = (-1*lastStep);
if(lastStep!=0){
Serial.print(test);
myStepper.step(test);
lastStep=0;
}
Serial.println("This is last Step");
Serial.print(test);
}
delay(500);
}
String getTime() {
Time.zone(-6);
int year = Time.year();
int month = Time.month();
int day = Time.day();
String dayS = String(day);
String monthS = String(month);
if(day<10){
dayS= "0"+String(day);
}
if(month<10){
monthS= "0"+String(month);
}
String timeForm = String(year)+"-"+monthS+"-"+dayS;
timeForm = "{\"minDate\":\"" + timeForm + "\"}";
Serial.print(timeForm);
return timeForm;
}
Comments