Connected LED - Ben O

Made by Ben Oppenheim · UNLISTED (SHOWN IN POOLS)

This project documents exercise 3 of the "Connected LED" lab. The LED's are controllable through the particle interface and if you put in any integer greater than 0 the corresponding LED will flash that number of times. I am also including source code for the other tasks in this exercise. My biggest challenge was working through where in the code I needed to make changes to get the LED's to blink when it became connected to the cloud.

Created: October 29th, 2022

0

Intention

Write about the big ideas behind your project? What are the goals? Why did you make it? What are your motivations?

This project documents exercise 3 of the "Connected LED" lab. The LED's are controllable through the particle interface and if you put in any integer greater than 0 the corresponding LED will flash that number of times. I am also including source code for the other tasks in this exercise.

0

Process

Describe the process you underwent to reach the outcome (problems encountered, how you resolved them, as well as, experiments, hacks, tests, refinments, iterations, failures)

My biggest challenge was working through where in the code I needed to make changes to get the LED's to blink when it became connected to the cloud. I finally realized that I needed to nest my script in the result portion of the if statement and it became easy from there. To troubleshoot I made small changes to review how it affected the result and used that to inform my knowledge of how the code was working so that I could make final changes to get the desired outcome.

0

Reflection

Reflect on the process of making this project. What did you learn? What would you do differently?

I really enjoyed this exercise! The first part (without cloud connection) was straightforward enough to understand basic functionality. Once the cloud connection was added it became more complicated and I needed to understand how the function we wrote actually worked to make it's output. Once I understood that I was able to work through the tasks to get the desired outcome.

Next time I will spend more time upfront reviewing the code to understand exactly what each portion of it is doing. Then I will map out each step to have a better handle on what needs to happen next.

0
//Exercise 1: LED Blink 3 times

int ledPin = D2;
// int ledPin2 = D3;

void setup() {
  pinMode(ledPin, OUTPUT);
//   pinMode(ledPin2, OUTPUT);
  
  // Initialize both the LEDs to be OFF
   digitalWrite(ledPin, LOW);
//   digitalWrite(ledPin2, LOW);
   
   Particle.function("led", ledControl);
}

void loop() {
  
 
}

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

   // find out the state of the led
   if(command == "HIGH")
   {
	   for (int i = 0; i < 3; i++) 
        {
            digitalWrite(ledPin, HIGH); 
            delay(500);               
            digitalWrite(ledPin, LOW);   
            delay(500);
        }
	   state = HIGH;
   }
   else if(command == "LOW")
   { 
	   state = LOW;
   }
   else
   {
	   return -1;
   }

        // write appropriate command to LED
        digitalWrite(ledPin, state);
   
   return 1;
}
Click to Expand
0
//Connected LED Exercise 2: Make the LED blink # of times then turn off

int ledPin1 = D2;

void setup() {
  pinMode(ledPin1, OUTPUT);
  
  // Initialize both the LEDs to be OFF
   digitalWrite(ledPin1, LOW);
   
   Particle.function("Number of red led blinks", ledControl1);
}
void loop() {
  
 
}

int ledControl1(String command)
{
   int value = command.toInt();

   // find out the state of the led
   if(value > 0)
   {
	   for (int i = 0; i < value; i++) 
        {
            digitalWrite(ledPin1, HIGH); 
            delay(500);               
            digitalWrite(ledPin1, LOW);   
            delay(500);
        }
   }
   else
   {
	   return -1;
   }
   return 1;
}
Click to Expand
0
//Connected LED Exercise 3: Add second LED and control remotely (both blink the number of times you input)

int ledPin1 = D2;
int ledPin2 = D3;

void setup() {
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  
  // Initialize both the LEDs to be OFF
   digitalWrite(ledPin1, LOW);
   digitalWrite(ledPin2, LOW);
   
   Particle.function("Number of red led blinks", ledControl1);
   Particle.function("Number of orange led blinks", ledControl2);
}

void loop() {
  
 
}

//Note: Functions cannot happen simultaneously, when you execute ledControl2 while ledControl1 is happening, it pauses ledControl1 to run ledControl2 until that is comnplete
int ledControl1(String command)
{
   int value = command.toInt();

   // find out the state of the led
   if(value > 0)
   {
	   for (int i = 0; i < value; i++) 
        {
            digitalWrite(ledPin1, HIGH); 
            delay(500);               
            digitalWrite(ledPin1, LOW);   
            delay(500);
        }
   }
   else
   {
	   return -1;
   }
   return 1;
}
int ledControl2(String command)
{
   int value2 = command.toInt();

   // find out the state of the led
   if(value2 > 0)
   {
	   for (int i = 0; i < value2; i++) 
        {
            digitalWrite(ledPin2, HIGH); 
            delay(500);               
            digitalWrite(ledPin2, LOW);   
            delay(500);
        }
   }
   else
   {
	   return -1;
   }
   return 1;
}
Click to Expand
x
Share this Project

This project is only listed in this pool. Be considerate and think twice before sharing.


Courses

About

This project documents exercise 3 of the "Connected LED" lab. The LED's are controllable through the particle interface and if you put in any integer greater than 0 the corresponding LED will flash that number of times. I am also including source code for the other tasks in this exercise.

My biggest challenge was working through where in the code I needed to make changes to get the LED's to blink when it became connected to the cloud.