Skills Dev I - Lulin Shan

Made by Lulin Shan ·

Created: November 11th, 2021

0

Outcome

Following the guidance and tutorials, I was able to make simple circuits to create a blinking LED, then made a connected LED.

  

0

First Sketch: Making an LED Blink

Exercise 1

Modify the program to Blink on and off every 3 seconds.
The only thing I changed is the delay time. 

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 LED on
    delay(3000); //wait for 3 seconds
    
   // Now...Off
    digitalWrite( ledPin, LOW ); //turn LED off
    delay(3000); //wait for 3 seconds
    // rinse + repeat
}
Click to Expand
0
Making an LED Blink - Exercise 1
Lulin Shan - https://youtu.be/Y7x4PFCpN9o
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 used a for loop to achieve 5 times blink on and off. Learned from https://www.arduino.cc/reference/en/language/structure/control-structure/for/

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() {
    // loop to turn LED on 5 times
    for (int i= 0; i<5; i++){
    digitalWrite( ledPin, HIGH ); //turn LED on
    delay(500); //wait for 0.5 second
    digitalWrite( ledPin, LOW ); //turn LED off
    delay(500); //wait for 0.5 second
    }  
    delay(3000); // wait for 3 seconds
//rinse + repeat

}
Click to Expand
0
Making an LED Blink - Exercise 2
Lulin Shan - https://youtu.be/2zscZXIqioU
0

Exercise 3

Go back to the original program. Now add a second LED to the circuit.
Program the LED’s to alternate blinks i.e. when LED 1 turns on, LED 2 turns off, then when LED 2 turns on, LED 1 turns off.
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 = D2;
int ledPin2 = D5;

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

void loop() {
    digitalWrite( ledPin1, HIGH ); //turn LED1 on
    digitalWrite( ledPin2, LOW ); //turn LED2 off
    delay(2000); //wait for 2 second2
    digitalWrite( ledPin1, LOW ); //turn LED1 off
    digitalWrite( ledPin2, HIGH ); //turn LED2 on
    delay(2000); //wait for 2 second2
    // rinse + repeat
}
Click to Expand
0
Making an LED Blink - Exercise 3
Lulin Shan - https://youtu.be/QngH15aktnw
0

Making a Connected LED

Exercise 1

Modify the cloud function to blink the LED 3 times after it is called.
I added 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 == "HIGH"){
       for(int i = 0; i < 3; i++){
           digitalWrite(ledPin, HIGH);
           delay(500);
           digitalWrite(ledPin, LOW);
           delay(500);
       }
   }else if(command == "LOW"){ 
	   state = LOW;
   }else{
	   return -1;
   }

   // write to the appropriate pin
   digitalWrite(ledPin, state);
   return 1;
}
Click to Expand
0
Making a Connected LED - Exercise 1
Lulin Shan - https://youtu.be/6bPHCAQwrIM
0

Exercise 2

Modify the cloud function as follows:
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

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("ledb", ledBlink);
}
void loop()
{
   // Nothing to do here
}
int ledBlink(String cntInput)
{
   int cnt = atoi(cntInput);
   if  (cnt <= 0){
       return -1;
   }
   for(int i = 0; i < cnt; i++){
           digitalWrite(ledPin, HIGH);
           delay(500);
           digitalWrite(ledPin, LOW);
           delay(500);
       }
   // turn the LED off
   digitalWrite(ledPin, LOW);
   return 1;
}
Click to Expand
0
Making a Connected LED - Exercise 2
Lulin Shan - https://youtu.be/HwROlD2_CKs
0

Exercise 3

Go back to the original program. Now add a second LED to the circuit.
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
// name the pins
int ledPin1 = D2;
int ledPin2 = D4;

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

   // Initialize both the LEDs to be OFF
   digitalWrite(ledPin1, LOW);
   digitalWrite(ledPin2, LOW);
   
    //Register our Particle function here
   Particle.function("led1", ledControl1);
   Particle.function("led2", ledControl2);
}
void loop()
{
   // Nothing to do here
}
//control LED 1
int ledControl1(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(ledPin1, state);
   return 1;
}
//control LED 2
int ledControl2(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(ledPin2, state);
   return 1;
}
Click to Expand
0
Making a Connected LED - Exercise 3
Lulin Shan - https://youtu.be/ZB0LxqR4YtY
0

Process

It was very smooth to complete the first 3 exercises by following the instructions. It took me a bit of experiment with writing the code for passing the number of LED blinks, because I need to look up additional functions to make it work. 


Reflection

I found watching the class recording and having the detailed instructions in the tutorial really helpful. Through this exercise, I have learned how to build a simple circuit. It was fun to write the code and build a circuit and test on it. My next steps are to get more familiar with the programming language and learn the affordances of more Internet appliances.    

x
Share this Project

This project is only accessible by signed in users. Be considerate and think twice before sharing.


Courses

About

~