Back to Parent

//FIRST ATTEMPT WITH COUNTER

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

void setup()
{

  pinMode( buttonPin , INPUT_PULLUP); 
  pinMode( ledPin , OUTPUT );
  fatFishFeeder.attach( A5 );
  Particle.function("servo", servoControl);
  Particle.variable ("servoPos", &servoPos, INT);

}

void loop()
{

   int buttonState = digitalRead( buttonPin );

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

    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