Skills Dev I_Xiaoran Zhang

Made by Xiaoran Zhang

Created: November 9th, 2020

0

Make an LED Blink

By following the instruction, I successfully connect a basic circuit and then wrote the code to make the led blink. Here is the circuit and code.

0
int ledPin = D2;

void setup()
{
 // We want to tell the Argon that we'll use
 // D2 as an output pin.    
    pinMode( ledPin, OUTPUT );
}

void loop() {
  // First... On
  digitalWrite(ledPin, HIGH);   // Turn ON the LED pins
  delay(1000);               // Wait for 1000mS = 1 second

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

}
Click to Expand
0
0

Exercise 1  

Modify the program to Blink on and off every 3 seconds. I only need to change the delay time to 3s.

0
int ledPin = D2;

void setup()
{
 // We want to tell the Argon that we'll use
 // D2 as an output pin.    
    pinMode( ledPin, OUTPUT );
}

void loop() {
  // First... On
  digitalWrite(ledPin, HIGH);   // Turn ON the LED pins
  delay(3000);               // Wait for 3000mS = 3 second

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

}
Click to Expand
0
0

Exercise 2

Change the program to blink on and off 5 times then stop for 3 seconds. Each blink should be 0.5s (a half second). I created a for loop to enable the led blink 5 times.

0
int ledPin = D2; 

void setup()
{
    // We want to tell the Argon that we'll use
    // D2 as an output pin.
    pinMode( ledPin, OUTPUT );
    
}

void loop()
{
    // First... On
    for (int i = 0; i < 5; i++) // for loop to enable led's 5 times blink
    {
        digitalWrite( ledPin, HIGH ); // Turn ON the LED pins
        delay( 500 );                 // Wait for 500mS = 0.5 second
       // Now... Off
        digitalWrite( ledPin, LOW ); // Turn OFF the LED pins
        delay( 500 );                // Wait for 500mS = 0.5 second
    }
   // Off and wait for 3 seconds
    digitalWrite( ledPin, LOW ); // Turn OFF the LED pins
    delay( 3000 );               // Wait for 3000mS = 3 second
    // rinse + repeat
}
Click to Expand
1
0

Exercise 3

Go back to the original program. Now add a second LED to the circuit.   I connected another led to the circuit and defined it as another ledPin to control the two leds' on/off state at the same time.

0
int ledPin1 = D1; int ledPin2 =D2;

void setup()
{
    // We want to tell the Argon that we'll use
    // D1 and D2 as an output pin.
    pinMode( ledPin1, OUTPUT );
    pinMode( ledPin2, OUTPUT );
    
}

void loop()
{
    // First... On
    digitalWrite( ledPin1, HIGH ); // Turn ON D1 LED pins
    delay( 1000 );                 // Wait for 1000mS = 1 second
    digitalWrite( ledPin1, LOW );  // Turn OFF D1 LED pins
    digitalWrite( ledPin2, HIGH ); // Turn ON D2 LED pins
    delay( 1000 );                 // Wait for 1000mS = 1 second
    digitalWrite( ledPin2, LOW );  // Turn OFF D2 LED pins
    // rinse + repeat 
}
Click to Expand
0
0

Making a Connected LED

Through the process, I am not only able to light the leds up, but control the on/off state of leds as well. Through the operating process, I can understand how to write the code to indicate the state of led being controled.

0
// name the pins
int ledPin = D2;

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

void loop()
{
   // Nothing to do here    
}

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

   // find out the state of the led
   if(command == "HIGH")
   {
	   state = HIGH;
   }
  
   else if(command == "LOW")
   { 
	   state = LOW;
   }
   
   else
   {
	   return -1;
   }
   
   // write to the appropriate pin
   digitalWrite(ledPin, state);
   return 1;
}
Click to Expand
0
0

Exercise 1

Modify the cloud function to blink the LED 3 times after it is called. The change to be made is adding the for loop to enable the led blink 3 times after called. 

0
// name the pins
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()
{
// Nothing to do here
}

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

   // find out the state of the led
   if(command == "BLINK")
   {
        for (int i = 0; i < 3; i++)     // For loop enable blink 3 times
        {
        digitalWrite( ledPin, HIGH );   // Turn ON the LED pins
        delay( 500 );                   // Wait for 500mS = 0.5 second
        digitalWrite( ledPin, LOW );    // Turn OFF the LED pins
        delay( 500 );                   // Wait for 500mS = 0.5 second
        }
   }
   
   else if(command == "NOBLINK")
   { 
	   state = LOW;
   }
   
   else
   {
	   return -1;
   }

   // write to the appropriate pin
   digitalWrite(ledPin, state);
   return 1;
}
Click to Expand
0
0

Exercise 2

Instead of passing a HIGH Or LOW string pass the number of times you would like it to blink. Set the function to blink that number of times. Finally once it has completed all of the blinking it should turn the LED off. I used the toInt function to translate string command to Int, and used the for loop to enable the led blink the same time as called.

0
// name the pins
int ledPin = D2;
int Val = 0;

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

void loop()
{
    
}

int ledControl(String cntblink)
{
   Val = cntblink.toInt(); //Use toInt function to turn string command into Int
   
   if(Val <= 0)
       {
        return -1;
       }

    else
    {
    for (int i = 0; i < Val; i++)
        {
        digitalWrite( ledPin, HIGH );
        delay( 500 );
        digitalWrite( ledPin, LOW );
        delay( 500 );
        }
    }
   
   // write to the appropriate pin
   digitalWrite(ledPin, LOW);
   return 1;
}
Click to Expand
0
0

Exercise 3

Change the program and cloud function to allow you to control both LEDs remotely. I defined another led as ledPin and code to control the state of two leds.

0
int ledPin1 = D1;
int ledPin2 = D2;

void setup()
{
    
    pinMode(ledPin1, OUTPUT);
    digitalWrite(ledPin1, LOW);
    pinMode(ledPin2, OUTPUT);
    digitalWrite(ledPin2, LOW);
    Particle.function("led", ledControl);
    
}

void loop()
{
    
}

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

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

   digitalWrite(ledPin1, state);
   digitalWrite(ledPin2, state);
   return 1;
}
Click to Expand
0
0

Process 

I finished the blink the leds exercises easily because I can find that I only need to make a change of the blink time and write simple for loops. I spent more time setting up different command to call the leds' state during the connected led exercises, because I need to look up additional functions to define the variable through the command. At last, I was able to understand each step of connecting the circuit and the meaning of code, which is very helpful for me to continue learning more skills.

0

Reflection

I am happy to see how it worked to connect user and the device together and how can the device respond to people's command. It helped me understand the theories and viewpoints in lecture and reasings better. 

x