Back to Parent

int rPinWish = A0;
int gPinWish = D0;
int bPinWish = D1;

int rPinSub = A1;
int gPinSub = A4;
int bPinSub = A5;

int rPinPrice = D2;
int gPinPrice = A6;
int bPinPrice = D3;

// first column is Wish, second column is Sub, third column is Price
int r[3] = {0, 0, LOW};
int g[3] = {0, 0, 0};
int b[3] = {0, 0, LOW};

int wishCount = 0;
int subCount = 0;
int priceCount = 0;

int delayTime = 500;

void setup() {
  pinMode(rPinWish, OUTPUT);
  pinMode(gPinWish, OUTPUT);
  pinMode(bPinWish, OUTPUT);
  pinMode(rPinSub, OUTPUT);
  pinMode(gPinSub, OUTPUT);
  pinMode(bPinSub, OUTPUT);
  pinMode(rPinPrice, OUTPUT);
  pinMode(gPinPrice, OUTPUT);
  pinMode(bPinPrice, OUTPUT);

  resetLED(0); //0 is wish
  resetLED(1); //1 is sub
  resetLED(2); //2 is price

  Spark.function("wishlist", wishlistPulse);
  Spark.function("subscription", subscriptionPulse);
  Spark.function("price", pricePulse);
}

void loop() {
  //code below is for testing purposes
  /*wishlistPulse("HIGH");
  delay(5000);
  wishlistPulse("HIGH");
  wishlistPulse("LOW");
  delay(5000);
  wishlistPulse("LOW");
  delay(2000);

  subscriptionPulse("HIGH");
  delay(5000);
  subscriptionPulse("HIGH");
  subscriptionPulse("LOW");
  delay(5000);
  subscriptionPulse("LOW");
  delay(2000);

  pricePulse("HIGH");
  delay(5000);
  pricePulse("HIGH");
  pricePulse("LOW");
  delay(5000);
  pricePulse("LOW");
  delay(2000);*/
}

int wishlistPulse(String command) {
  if (command == "HIGH") {
    wishCount++;
    r[0] = 0;
    g[0] = 255;
    b[0] = 0;
    pulseLED(0);
    //resetLED(0);
  }
  else if (command == "LOW")
  {
    if (wishCount > 0)
    {
      wishCount--;
    }

    if (wishCount == 0) {
      r[0] = 0;
      g[0] = 0;
      b[0] = 0;
      resetLED(0);
    }
  }

  return 1;
}

int subscriptionPulse(String command) {
  if (command == "HIGH") {
    subCount++;
    r[1] = 255;
    g[1] = 0;
    b[1] = 0;
    pulseLED(1);
    //resetLED(1);
  }
  else if (command == "LOW")
  {
    if (subCount > 0)
    {
      subCount--;
    }

    if (subCount == 0) {
      r[1] = 0;
      g[1] = 0;
      b[1] = 0;
      resetLED(1);
    }
  }

  return 1;
}

int pricePulse(String command) {
  if (command == "HIGH") {
    priceCount++;
    r[2] = HIGH;
    g[2] = 0;
    b[2] = HIGH;
    pulseLED(2);
    //resetLED(2);
  }
  else if (command == "LOW")
  {
    if (priceCount > 0)
    {
      priceCount--;
    }

    if (priceCount == 0) {
      r[2] = LOW;
      g[2] = 0;
      b[2] = LOW;
      resetLED(2);
    }
  }

  return 1;
}

void resetLED(int i) {
  switch (i) {
    case 0:
      analogWrite(rPinWish, r[i]);
      analogWrite(gPinWish, g[i]);
      analogWrite(bPinWish, b[i]);
      break;
    case 1:
      analogWrite(rPinSub, r[i]);
      analogWrite(gPinSub, g[i]);
      analogWrite(bPinSub, b[i]);
      break;
    case 2:
      digitalWrite(rPinPrice, r[i]);
      analogWrite(gPinPrice, g[i]);
      digitalWrite(bPinPrice, b[i]);
  }
}

void pulseLED(int i) {
  switch (i) {
  case 0:
    analogWrite(rPinWish, r[i]);
    analogWrite(gPinWish, g[i]);
    analogWrite(bPinWish, b[i]);
    delay(delayTime);

    analogWrite(rPinWish, r[i]+255);
    analogWrite(gPinWish, g[i]);
    analogWrite(bPinWish, b[i]+255);
    delay(delayTime);

    analogWrite(rPinWish, r[i]);
    analogWrite(gPinWish, g[i]);
    analogWrite(bPinWish, b[i]);

    break;
  case 1:
    analogWrite(rPinSub, r[i]);
    analogWrite(gPinSub, g[i]);
    analogWrite(bPinSub, b[i]);
    delay(delayTime);

    analogWrite(rPinSub, r[i]);
    analogWrite(gPinSub, g[i]+255);
    analogWrite(bPinSub, b[i]+255);
    delay(delayTime);

    analogWrite(rPinSub, r[i]);
    analogWrite(gPinSub, g[i]);
    analogWrite(bPinSub, b[i]);

    break;
  case 2:
    digitalWrite(rPinPrice, r[i]);
    analogWrite(gPinPrice, g[i]);
    digitalWrite(bPinPrice, b[i]);
    delay(delayTime);

    digitalWrite(rPinPrice, r[i]);
    analogWrite(gPinPrice, g[i]+255);
    digitalWrite(bPinPrice, b[i]);
    delay(delayTime);

    digitalWrite(rPinPrice, r[i]);
    analogWrite(gPinPrice, g[i]);
    digitalWrite(bPinPrice, b[i]);
  }
}
Click to Expand

Content Rating

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

0