Back to Parent

/* CONSTANTS & VARIABLES
------------------------------------------------------*/

Servo myServo1;
Servo myServo2;
int servoPin1 = D0;
int servoPin2 = D1;
int posS1 = 0;
int posS2 = 0;
int day=0;
int month=0;
int year=0;
String sDay;
String sMonth;
String sYear;
String sBudget;
String inUser;
int currentSpend = 0;
float time1 = 0;
float distance=0;
float budget=0;
int days;
float cdays=0;
int n=0;
int x=0;
/* SETUP
------------------------------------------------------*/
void setup(){

Particle.function("Spreadsheet", handleSpreadsheet);// function that pulls in credit card transactions from spreadsheet online
Particle.variable("Spend",currentSpend);

Particle.function("inData",inData);// function that pulls in users inputted budget and starting date
Particle.variable("inUser",inUser);

myServo1.attach(servoPin1);
myServo2.attach(servoPin2);
}

/* LOOP
------------------------------------------------------*/
void loop(){

while (budget!=0){
while(((cdays+day)/days*180)<=180){
myServo1.write((cdays+day)/days*180); // Blue fan moves some percentage of 180 degrees depending on how many days have passed in the month
}
while  (((-currentSpend/budget)*180)<=180){
myServo2.write((-currentSpend/budget)*180);// Orange fan moves some percentage of 180 degrees depending on how much of your budget you have spent
}
cdays=cdays+0.5;// Display should only move twice a day, done to compensate for  inaccuracies seen in servo motor; wanted only significant changes to be recognized
Serial.println((-currentSpend/budget)*180);// degrees orange fan should be moving
delay(1000);// delay put in place to allow for checking to occur twice a day
myServo2.write(0);// Done to match budget fan movement
myServo1.write(0);//Either currentSpend or fans needed to be reset because currentSpend accounted for all spending and would move the fan accordingly each time, thus counting the spend more than once

if (cdays==days)// at the end of the month the display should reset
  {
    myServo1.write(-180);
    myServo2.write(-180);
    cdays=0;
    currentSpend=0;

  }
  
}
}

/* CLOUD FUNCTIONS
------------------------------------------------------*/
int handleSpreadsheet (String command) {
//Serial.print("Recieved=");
//Serial.println(command);
int spend = command.toInt();
//Serial.println(spend);


currentSpend = currentSpend+spend; //calculate total spend

return 1;
}


int inData( String data ){
inUser = data;

int n = inUser.indexOf(':');
sBudget= inUser.substring(0,n); // extracts budget from string retrieved from html page
budget= sBudget.toFloat();
sMonth=inUser.substring(n+1,n+3); // extracts day from string received from html page
month= sMonth.toInt();
sDay= inUser.substring(n+4,n+6);// extracts month from string received from html page
day=sDay.toInt();
sYear= inUser.substring(n+7,n+11);// extracts year from string received from html page
year= sYear.toInt();

// if statements below are used to determine how many days there are in the month the budget is set to
if (month==1 or month==3 or month==5 or month==7 or month==8 or month==10 or month==12){
  days=31;
}
if (month==4 or month==6 or month==9 or month==11){
  days=30;
}
if (month==2 and year%4==0){
  days=29;
}
else {days==28;}
cdays=day;
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0