Back to Parent

int legPin = A0;
int headPin = A1;
int redPin = A4;
int greenPin = D0;
int bluePin = D1;
int myredPin = A5;
int mygreenPin = A6;
int mybluePin = A7;
int legbeam;
int headbeam;
int avatarState;

void setup()
{
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  pinMode(myredPin, OUTPUT);
  pinMode(mygreenPin, OUTPUT);
  pinMode(mybluePin, OUTPUT);

  pinMode(legPin, INPUT);
  pinMode(headPin, INPUT);

  Spark.variable("legbeam", &legbeam, INT);
  Spark.variable("headbeam", &headbeam, INT);

  Spark.subscribe("ChrisHolliday", handlePositionChange );
}

void loop()
{

  //delay( 1000 );

  legbeam = analogRead(legPin);
  headbeam = analogRead(headPin);

  //avatarState = 0;
  // State variable key
  // sit = 1
  // lay = 2
  // stand = 3

  if (legbeam>100 && headbeam<100 && avatarState != 1)
  {
      // Red
      announceSit();
      avatarState = 1;
  }else if (legbeam>100 && headbeam>100 && avatarState != 2)
  {
      // Red + Blue
      announceLay();
      avatarState = 2;
  }else if (legbeam<100 && headbeam<100 && avatarState != 3)
  {
      // Green
      announceStand();
      avatarState = 3;
  }

  delay(2500);

}

void announceSit()
{
  Spark.publish("StephenKrotseng", "SIT", 60, PUBLIC);

  // Set to red
  analogWrite(myredPin, 0);
  analogWrite(mybluePin, 255);
  analogWrite(mygreenPin, 255);
}

void announceLay()
{
  Spark.publish("StephenKrotseng", "LAY", 60, PUBLIC);

  // Set to purple
  analogWrite(myredPin, 0);
  analogWrite(mybluePin, 0);
  analogWrite(mygreenPin, 255);
}

void announceStand()
{
  Spark.publish("StephenKrotseng", "STAND", 60, PUBLIC);

  // Set to green
  analogWrite(myredPin, 255);
  analogWrite(mybluePin, 255);
  analogWrite(mygreenPin, 0);
}

void handlePositionChange( const char * event, const char *data  )
{
    if( strcmp( data, "SIT" ) == 0 ){
      RGBSIT();
      return;
    }

    if( strcmp( data, "LAY" ) == 0 ){
      RGBLAY();
      return;
    }

    if( strcmp( data, "STAND" ) == 0 ){
      RGBSTAND();
      return;
    }
}

void RGBSIT()
{
  analogWrite(redPin, 0);
  analogWrite(bluePin, 255);
  analogWrite(greenPin, 255);
}

void RGBLAY()
{
  analogWrite(redPin, 0);
  analogWrite(bluePin, 0);
  analogWrite(greenPin, 255);
}

void RGBSTAND()
{
  analogWrite(redPin, 255);
  analogWrite(bluePin, 255);
  analogWrite(greenPin, 0);
}
Click to Expand

Content Rating

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

0