Back to Parent

// Connecting LED to Cloud
// Practice with controlling simple LED patterns with remote control using HIGH and LOW to start or stop pattern



// Include Particle Device OS APIs
#include "Particle.h"

// Let Device OS manage the connection to the Particle Cloud
SYSTEM_MODE(AUTOMATIC);

// Show system, cloud connectivity, and application logs over USB
// View logs with CLI using 'particle serial monitor --follow'
SerialLogHandler logHandler(LOG_LEVEL_INFO);

// First make a variable
// This is the shorthand that will be used throughout the program
int led1 = D2; // Instead of writing D0 over and over again, we will use led1
// I will need to wire an led to this one to see it blink
int led2 = D3;

// setup() runs once, when the device is first turned on
void setup() {
    //Register our Particle function here
    Particle.function("led", ledControl);
  // We want to tell the Photon that we'll use
  // D2 as an output pin.
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);

  // Put initialization like pinMode and begin functions here
  // Initialize both the LEDs to be OFF
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
}

// loop() runs over and over again, as quickly as it can execute.
void loop() {
    //nothing
}

int ledControl(String command)
{
   int state = LOW;

   // find out the state of the led
   if(command == "HIGH"){
       // LED 1 Blink 3 times test
    //   digitalWrite(led1, HIGH);   // Turn ON the LED1 pins
    //   delay(500);
    //   digitalWrite(led1, LOW);   // Turn OFF the LED1 pins
    //   delay(500);
    //   digitalWrite(led1, HIGH);   // Turn ON the LED1 pins
    //   delay(500);
    //   digitalWrite(led1, LOW);   // Turn OFF the LED1 pins
    //   delay(500);
    //   digitalWrite(led1, HIGH);   // Turn ON the LED1 pins
    //   delay(500);
    //   digitalWrite(led1, LOW);   // Turn OFF the LED1 pins
    
    // LED 1 & 2 blink back and forth
    digitalWrite(led1, HIGH);   // Turn ON the LED1 pins
    delay(1000);               // Wait for 2000mS = 1 second

    // Now... Off
    digitalWrite(led1, LOW);   // Turn OFF the LED1 pins
    delay(0);               // Wait for 0mS = 0 second
  
    digitalWrite(led2, HIGH);   // Turn ON the LED2 pins
    delay(1000);               // Wait for 1000mS = 1 second

     // Now... Off
     digitalWrite(led2, LOW);   // Turn OFF the LED1 pins
     delay(0);               // Wait for 0mS = 0 second

   }else if(command == "LOW"){ 
	   state = LOW;
   }else{
	   return -1;
   }

   // write to the appropriate pin
   digitalWrite(led1, state);
   return 1;
   
   digitalWrite(led2, state);
   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