Skill Dev I - Connected LEDs

Made by KRITTIKA BHATNAGAR

The goal of this project is to create a simple IoT device which includes an LED circuit and simple code to control the LED.

Created: November 20th, 2022

0

Intention

The intention was to create a simple IoT device. The physical circuit included LEDs and resistors and the code was written to control the LEDs based on defined input. 

0

Process

Setting up the circuit was straightforward. 

Writing the code for the initial exercise was fairly easy as the steps to write the code were given and I had to follow them. 

I struggled with writing the code for the next exercise (i.e. making the LED blink when the string input was "HIGH"). Within the for loop, to make the light blink, I was using the 'state' command instead of 'digitalWrite'.

i.e. 

 if(command == "HIGH"){

for( int i = 0; i <= 2; i++ )

{

state = HIGH;

delay( 1000 );

state = LOW;

delay( 1000 );

}

}else if(command == "LOW"){

state = LOW;

}else{

return -1;

}


This is wrong as the 'state' is an integer variable defined to convert and store the string command from the internet into an input that the particle device can understand. It is the 'digitalWrite' command that reads the integer input defined in 'state' and converts it into physical action in the circuit. 

Upon correcting my mistake, I was able to make the code work and the LED blink 3 times as per the requirement. 

-1

Reflection

I learned that knowing how to do something in theory is very different from putting it into practice.  

0
int ledPin = D2;



void setup()
{
   // Configure the pins to be outputs
   pinMode(ledPin, OUTPUT);

   // Initialize both the LEDs to be OFF
   digitalWrite(ledPin, LOW);
   
    //Register our Particle function here
   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 <= 2; i++ )
    
    {

	digitalWrite( ledPin, HIGH );
	delay( 1000 );
	digitalWrite( ledPin, LOW );
	delay( 1000 );
    
    }


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

   // write to the appropriate pin
   digitalWrite(ledPin, state);
   return 1;
}
Click to Expand
x
Share this Project

Courses

About

The goal of this project is to create a simple IoT device which includes an LED circuit and simple code to control the LED.