Back to Parent

//Declaring servo control variables and servo motors
int servoPin = A4;
Servo mouthServo;
int servoPos = 10;

//Status Indicator LED
int ledPin7 = D7;

//Variables for count of receipts and time of last receipt
int receiptCount = 0;
int receiptTime = 0;

//Variables for LED control
int redPin = D0;
int bluegreenPin = D1;
int redValue = 255;
int bluegreenValue = 255;

//Threshold values and decrication time variable
int highCount = 3;
int midCount = 2;
int timeLimit = 120000; //86400000 mils per day

void setup()
{
  //Pinmode for indicator LED
  pinMode(ledPin7, OUTPUT);

  //Servo control setup functions and variables, with debugging
  mouthServo.attach(A4);
  //Particle.function("ServoSet", servoControl);
  //Particle.variable("ServoPos", servoPos);

  //Particle functions and variables for interacting with email receipts
  Particle.function("Receipt", receiptReceive);
  Particle.variable("Receipts", receiptCount);

  //Functions and variables for setting the warning and high thresholds
  Particle.variable("highCount", highCount);
  Particle.variable("midCount", midCount);
  Particle.function("SetThres", setThresholds);
  Particle.variable("timeLimit", timeLimit);
  Particle.function("setTime", setTime);

  //RGB Pinmodes, variables, and initializing RGB LED outputs
  //pinMode(redPin, OUTPUT);
  pinMode(redPin, OUTPUT);
  pinMode(bluegreenPin, OUTPUT);

  //Debugging for LED values and controls
  //Particle.function("led", ledControl);
  //Particle.variable("redValue", redValue);
  //Particle.variable("bgValue", bluegreenValue);

  //Write initial values for RGB LEDs
  analogWrite(redPin, redValue);
  analogWrite(bluegreenPin, bluegreenValue);
}
void loop()
{
  checkTime(); //Check if sufficient time has passed to decrease the receiptcount variable
  checkCount(); //Check the receiptcount and set servo and LED values
  delay (100);
}

//Control function for servo
int servoControl(int command)
{
  int newPos = command;
  servoPos = constrain(newPos, 10, 170); //Adjust values for range of servo motion
  mouthServo.write(servoPos);
  return 1;
}

int receiptReceive(String command)
{
  receiptCount = receiptCount + 1;  //Incriment count if function is called by IFTTT
  receiptTime = millis();           //Overwrite receipt time with time of function call
  digitalWrite(ledPin7, HIGH);
  delay(1000);
  digitalWrite(ledPin7, LOW);
  delay(1000);
  return receiptCount;
}

int checkTime()
{
  if (receiptCount >= 1 && receiptTime+timeLimit <= millis()) //If there is a count, and if timeLimit has passed since last receipt or deprication, decrease count and reset time
  {
    receiptCount = receiptCount-1;
    receiptTime = millis();
  }
}

int checkCount()
{
  if (receiptCount >= highCount) //If count is equal to or higher than the highCount, then red and frown
  {
    servoControl(10);
    ledControl("0,255");
  }
  else if (receiptCount <= highCount && receiptCount >= midCount) //If at midCount, then pink and smile
  {
    servoControl(180);
    ledControl("128,255");
  }
  else //otherwise teal and smile
  {
    servoControl(180);
    ledControl("255,0");
  }
}

int ledControl(String command) //Receives two values as string to set LED values
{
  String colors[2];
  colors[0]="";
  colors[1]="";

  int index=0;
  for (int i=0; i<command.length(); i++)
  {
    if(index < 2)
    {
      char c= command.charAt(i);
      colors[index] += c;
      if (c == ',') index++;
    }
  }
  redValue = colors[0].toInt();
  bluegreenValue = colors[1].toInt();

  analogWrite(redPin, redValue);
  analogWrite(bluegreenPin, bluegreenValue);

  return 1;
}

int setThresholds(String command) //function for setting the threshold values. Accepts midCount then highCount
{
  String thresholds[2];
  thresholds[0]="";
  thresholds[1]="";

  int index=0;
  for (int i=0; i<command.length(); i++)
  {
    if(index < 2)
    {
      char c= command.charAt(i);
      thresholds[index] += c;
      if (c == ',') index++;
    }
  }
  midCount = thresholds[0].toInt();
  highCount = thresholds[1].toInt();

  return 1;
}

int setTime(String command) //function for setting the cooldown timing. Value in seconds
{
  timeLimit = command.toInt();
  return 1;
}
Click to Expand

Content Rating

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

0