Isabel - Blinking LED

Made by Isabel Fleck

Developing a simple blinking led to a programmed rhythm as an introduction to IoT.

Created: October 31st, 2024

0

Intention

Developing a simple blinking led to a programmed rhythm as an introduction to IoT. I wanted to develop an understanding on connected a simple LED appliance to a cloud so that it can be controlled remotely. Also building skills in connecting LEDS and controlling two to create patterns.

0

Process

Following along with the simple internet appliance document, I began with connecting a single LED to blink based on code in my particle build, then adding a second to learn how to control two. Once this was functioning, I connected the LED to cloud so that it could be controlled across my particle console sandbox. Here I also began with one LED to respond at HIGH or LOW being called, added a blinking pattern, and tried again with two LEDS. However when trying to control two, I came across an issue where when called on HIGH, the lights would blink but only for one cycle. Since I wanted this to last longer as a consistent loop until LOW was called to stop it, I turned to GPT for help in understanding a solution to edit my code. Here I learned about using a global boolean variable that can be used to control the behavior of my LEDS in my code later under the loop with an if else statement.

0
// Blinking LED
// Connecting Practice and controlling LEDS directly



// 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() {
  // 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
}

// loop() runs over and over again, as quickly as it can execute.
void loop() {
// Basics Demo
  // First... On
//   digitalWrite(led1, HIGH);   // Turn ON the LED pins
//   delay(1000);               // Wait for 1000mS = 1 second

//   // Now... Off
//   digitalWrite(led1, LOW);   // Turn OFF the LED pins
//   delay(1000);               // Wait for 1000mS = 1 second
  // rinse + repeat

  // The core of your code will likely live here.

  // Example: Publish event to cloud every 10 seconds. Uncomment the next 3 lines to try it!
  // Log.info("Sending Hello World to the cloud!");
  // Particle.publish("Hello world!");
  // delay( 10 * 1000 ); // milliseconds and blocking - see docs for more info!
  
  
  
// Exercise 1
//   digitalWrite(led1, HIGH);   // Turn ON the LED pins
//   delay(3000);               // Wait for 3000mS = 3 second

//   // Now... Off
//   digitalWrite(led1, LOW);   // Turn OFF the LED pins
//   delay(3000);               // Wait for 3000mS = 3 second
  
  
  
//Exercise 2
//   digitalWrite(led1, HIGH);   // Turn ON the LED pins
//   delay(500);               // Wait for 500mS = .5 second

//   // Now... Off
//   digitalWrite(led1, LOW);   // Turn OFF the LED pins
//   delay(500);               // Wait for 500mS = .5 second

//   digitalWrite(led1, HIGH);   // Turn ON the LED pins
//   delay(500);               // Wait for 500mS = .5 second

//   // Now... Off
//   digitalWrite(led1, LOW);   // Turn OFF the LED pins
//   delay(500);               // Wait for 500mS = .5 second

//   digitalWrite(led1, HIGH);   // Turn ON the LED pins
//   delay(500);               // Wait for 500mS = .5 second

//   // Now... Off
//   digitalWrite(led1, LOW);   // Turn OFF the LED pins
//   delay(500);               // Wait for 500mS = .5 second

//   digitalWrite(led1, HIGH);   // Turn ON the LED pins
//   delay(500);               // Wait for 500mS = .5 second

//   // Now... Off
//   digitalWrite(led1, LOW);   // Turn OFF the LED pins
//   delay(500);               // Wait for 500mS = .5 second

//   digitalWrite(led1, HIGH);   // Turn ON the LED pins
//   delay(500);               // Wait for 500mS = .5 second

//   // Now... Off
//   digitalWrite(led1, LOW);   // Turn OFF the LED pins
//   delay(3000);               // Wait for 500mS = 3 second
  
  
  
// Exercise 3
 // First... On
  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

}
Click to Expand
0
// 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
0

Outcome

In the end, I was able to successfully have my two LEDs blink in a consistent pattern back and forth when HIGH was called, and turn off when LOW was called. I've linked images to my code along the way as well as a video of the final working product.

0
// GPT HELP
    // Since I was able to get the lights blinking back and forth in the Connected_LED app, I realized that this would only make them blink through a single loop.
    // I wanted these LEDs to blink back and forth in a loop when HIGH was called, and stop when LOW was called
    // With GPTs help, I was able to figure out that a global boolean variable would be needed, so that this controls the LEDS by setting true or false statements on their behavior

//Explanation
    // Global Variable blinking: This boolean variable controls whether the LEDs are blinking or not.
    // loop() Function: If blinking is true, the loop() function alternates the LEDs on and off with a 500 ms delay
    // ledControl Function: When HIGH is passed, blinking is set to true, so the LEDs start blinking. When LOW is passed, blinking is set to false, stopping the blinking loop.
    
    // Now, when I call HIGH, the LEDs will continuously blink back and forth until I call LOW.



// 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);

// Define LED pins
int led1 = D2; 
int led2 = D3;

// Define a global variable to control the blinking state
bool blinking = false;

// setup() runs once, when the device is first turned on
void setup() {
    // Register our Particle function here
    Particle.function("led", ledControl);

    // Set the LED pins as outputs
    pinMode(led1, OUTPUT);
    pinMode(led2, OUTPUT);

    // Initialize both LEDs to be OFF
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
}

// loop() runs over and over again, as quickly as it can execute.
void loop() {
    // If blinking is true, alternate the LEDs
    if (blinking) {
        digitalWrite(led1, HIGH);    // Turn ON LED1
        digitalWrite(led2, LOW);     // Turn OFF LED2
        delay(500);                  // Wait for 500ms

        digitalWrite(led1, LOW);     // Turn OFF LED1
        digitalWrite(led2, HIGH);    // Turn ON LED2
        delay(500);                  // Wait for 500ms
    } else {
        // Make sure LEDs are off if blinking is false
        digitalWrite(led1, LOW);
        digitalWrite(led2, LOW);
    }
}

// Function to control LEDs via cloud command
int ledControl(String command) {
    if (command == "HIGH") {
        blinking = true; // Start blinking when HIGH is called
    } else if (command == "LOW") {
        blinking = false; // Stop blinking when LOW is called
    } else {
        return -1; // Invalid command
    }
    return 1;
}
Click to Expand
0

Reflection

Throughout this exercise, I enjoyed learning about the way in which LEDS connect and respond across the board to remote connection. While I am completely new to learning about using a Photon2 let alone working with IoT, I think that I will continue to strengthen my understanding around the physical components and how they connect and what purposes they serve as I continue to make future projects. While my understanding of the physical components is not the strongest, I feel that with repetition, I will better recall their purposes and how to use them, eventually creating my own projects. As for the programming portion, I think this project really strengthened my understanding on how the board connects to the internet and ways in which I am able to manipulate the board not only directly but across the internet as well. This to me was especially interesting, as I have only had minimal experience with Arduino, where there was only ever direct contact. I'm excited to see where else I can apply these skills in my other projects as well.

0
Isabel - Blinking Practice Exercise
Isabel Fleck - https://youtu.be/-e-YMhLavec
x
Share this Project


About

Developing a simple blinking led to a programmed rhythm as an introduction to IoT.