//SECOND ATTEMPT WITH COUNTER
int ledPin = D1;
Servo fatFishFeeder;
int buttonPin = D0;
int servoPin = A5;
int servoPos = 5;
long timeBeforeSomethingHappens = 2 * 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 )
{
startCounter();
digitalWrite( ledPin, HIGH);
// fatFishFeeder.write(180);
// Particle.publish("Fat_Fish_Fed", "Fed");
// delay(2000);
// fatFishFeeder.write(5);
}
else
{
digitalWrite( ledPin, LOW);
//fatFishFeeder.write(5);
}
if (checkIfCOunterIsFInished() )
{
digitalWrite( ledPin, LOW);
fatFishFeeder.write(180);
//Particle.publish("Fat_Fish_Fed", "Fed");
delay(2000);
fatFishFeeder.write(5);
counterRunning = false;
// I have to add another code where it stops here and not go back up to the loop
// because right now it kept on going back up to line 31 and turning down
// delay(2000);
}
}
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!
You must login before you can post a comment. .