Back to Parent

// GLOBALS
Servo myservo;
// create servo object using the built-in Particle Servo Library
int servoPosClose = 30;
int servoPosOpen = 8;
int servoDelay = 240;


int servoPin = D0;  // declare variable for servo
int pos = 0;        // variable to keep track of the servo's position
int open_time = 5000;
int remaining = NULL;

// Borders
int low_q = 250;
int mid_q = 500;
int hi_q = 750;
// checks to ensure that the borders only open once
int check_low = 0;
int check_mid = 0;
int check_hi = 0;

// Essentially a multiplier & check
int timer = 0;

void setup()
{

  Serial.begin(9600); //Start the Serial port @ 9600 baud
  // seen_transactions = [];    // Declare seen_transactions to empty every reset
  // seen_count = 0;
  myservo.attach(servoPin);  //Initialize the servo attached to pin D0
  myservo.write(servoPosClose);        //set servo to furthest position

Particle.subscribe("hook-response/budget", handleBudgetReceived, MY_DEVICES); // listening for data that p cloud grabbed
// this is the subscriptions
//"hook-response/budget" defined in particle console

}

void loop()
{

getData(); // int reamining value set here

delay(30000);

}


void getData()
{
	// Publish an event to trigger the webhook - tell p cloud to go get budget
  Particle.publish("budget", PRIVATE);
  Serial.println("run getdata()");
}

// This function will handle data received back from the webhook
void handleBudgetReceived(const char *event, const char *data) {
  // Handle the integration response


  String budgetRemaining =  String( data );
  remaining = budgetRemaining.toInt();

  Serial.println("amount remaining is");
  Serial.println(remaining);

  if (remaining <= hi_q && check_hi == 0) {
     check_hi = 1;
     Serial.println("budget less than high");
     releaseCoin();
     delay(servoDelay*1.5);
   }

   // tracking the 500 line
   if (remaining <= mid_q && check_mid == 0) {
     check_mid = 1;
     Serial.println("budget less than mid");
     releaseCoin();
     delay(servoDelay*1.5);
   }

   // tracking the 250 line
   if (remaining <= low_q && check_low == 0) {
     check_low = 1;
     Serial.println("budget less than low");
     releaseCoin();
     delay(servoDelay*1.5);
   }

   if (remaining <= 0) {
     timer ++;
     Serial.println("over budget");
     releaseCoin();
     delay(servoDelay*1.5);
   }

}


int releaseCoin() {
  myservo.write(servoPosClose);
  delay(servoDelay);
  myservo.write(servoPosOpen);
  delay(servoDelay);
  myservo.write(servoPosClose);

}
Click to Expand

Content Rating

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

0