Back to Parent

//FINISHED CODE

int ledPin = D1;
Servo fatFishFeeder;
int buttonPin = D0;
int servoPin = A5;
int servoPos = 5;
long timeBeforeSomethingHappens = 24 * 60 * 60 * 1000;
bool counterRunning = false;
long counterStartedAt = 0;
//bool isTriggered = false;

void setup()
{
  pinMode( buttonPin , INPUT_PULLUP);
  pinMode( ledPin , OUTPUT );
  fatFishFeeder.attach( A5 );
  Particle.function("servo", servoControl);
  Particle.variable ("servoPos", &servoPos, INT);
  bool count = checkIfCOunterIsFInished();
  Particle.variable("counter",&count);
}

void loop()
{
    int buttonState = digitalRead( buttonPin );

    if( buttonState == LOW )
    {
      if(!counterRunning)
      startCounter();
      digitalWrite( ledPin, HIGH);
    }
    else
    {
      digitalWrite( ledPin, LOW);
      fatFishFeeder.write(5);
    }

     if (checkIfCOunterIsFInished())
    {
       digitalWrite( ledPin, HIGH);
       fatFishFeeder.write(180);
       Particle.publish("Fat_Fish_Fed", "Fed");
       delay(2000);
       fatFishFeeder.write(5);
       counterRunning = false;
    }

    if( buttonState == HIGH )
    {
      counterRunning = false;
      digitalWrite( ledPin, LOW);
      fatFishFeeder.write(5);
    }

}

int servoControl(String command)
{
  int newPos = command.toInt();
  servoPos = constrain( newPos, 5 , 180);
  fatFishFeeder.write( servoPos );
  return 1;
}

void startCounter()
{
  counterRunning = true;
  counterStartedAt = millis();
}

bool checkIfCOunterIsFInished()
{
  if (counterRunning == false){
    return false;
  }
  if (counterStartedAt + timeBeforeSomethingHappens < millis() )
  {
    return true;
  }
    return false;
}
Click to Expand

Content Rating

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

0