Back to Parent

// We will be using D2 to control our LED
int ledPin = D2;

// Our button wired to D0
int buttonPin = D3;

bool doButton = false;

void setup()
{

  pinMode( buttonPin , INPUT_PULLUP); // sets pin as input

  // We also want to use the LED

  pinMode( ledPin , OUTPUT ); // sets pin as output
  
  Particle.function("spinFan", spinFan);
  Particle.subscribe( "buttonPressed", handleButtonPressed );

}

void loop()
{
   // find out if the button is pushed
   // or not by reading from it.
   int buttonState = digitalRead( buttonPin );


  if( buttonState == LOW )
  {
    // turn the LED On
    digitalWrite( ledPin, HIGH);
    Particle.publish("buttonPressed2");
    delay(1000);
  }else{
    // otherwise
    // turn the LED Off
    digitalWrite( ledPin, LOW);

  }

 if( doButton == true ){
        digitalWrite( ledPin, HIGH);
        delay(1000);
        doButton = false;
    }

}


void handleButtonPressed( const char *event, const char *data)
{
   doButton = true;
}

int spinFan(String command)
{
   digitalWrite( ledPin, HIGH);
   int runTime = command.toInt() * 1000;
   delay(runTime);;
   return 1;
}
Click to Expand

Content Rating

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

0