Yuchuan Shan – Skills Dev I

Made by Yuchuaun Shan

Making an LED blink & Making a connected LED

Created: November 8th, 2021

0

Making an LED Blink

Exercise 1

Modify the program to Blink on and off every 3 seconds.

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

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

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

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

  // Exercise 2
  for (int i = 0; i < 5; i++) {
      digitalWrite(ledPin, HIGH);
      delay(500); 
      digitalWrite(ledPin, LOW);
      delay(500);  
  }
  
   delay(3000); 

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

0
int ledPinR = D2;
int ledPinB = D3;

void setup() {
  pinMode(ledPinR, OUTPUT);
  pinMode(ledPinB, OUTPUT);
}

void loop() {

  // Exercise 3
  digitalWrite(ledPinR, HIGH); 
  digitalWrite(ledPinB, LOW);
  delay(1000);              
  digitalWrite(ledPinB, HIGH);
  digitalWrite(ledPinR, LOW);
  delay(1000);            

}
Click to Expand
0

Making a Connected LED

Exercise 1

Modify the cloud function to blink the LED 3 times after it is called

0
// name the pins
int ledPin = D2;

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

   // Initialize the LED to be OFF
   digitalWrite(ledPin, LOW);
   
   //Register our Particle function here
   Particle.function("led", ledControl);
}


void loop() {

}


// Exercise 1
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(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
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 the LED to be OFF
   digitalWrite(ledPin, LOW);
   
   //Register our Particle function here
   Particle.function("led", ledControl);
}


void loop() {

}


// Exercise 2
int ledControl(String command)
{
    //convert input command to integer and store in counter
    int counter = command.toInt();
    
    if(counter > 0){
        for(int i = 0; i < counter; i++){
          digitalWrite(ledPin, HIGH);
          delay(1000);
          digitalWrite(ledPin, LOW);
          delay(1000); 
          }
    }else{
        digitalWrite(ledPin, LOW);
        return -1;
    }
      return 1;
}
Click to Expand
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

0
// name the pins
int ledPinR = D2;
int ledPinB = D3;

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

   // Initialize the LED to be OFF
   digitalWrite(ledPinR, LOW);
   digitalWrite(ledPinB, LOW);
   
   //Register our Particle function here
   Particle.function("ledR", ledControlR);
   Particle.function("ledB", ledControlB);

}


void loop() {

}


// Exercise 3

// function to control red LED
int ledControlR(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(ledPinR, state);
   return 1;
}

// function to control blue LED
int ledControlB(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(ledPinB, state);
   return 1;
}
Click to Expand
0

Reflection

Connecting a microcontroller to the cloud is new to me and this skill dev allowed me to learn how to use Particle.function() to take command from the internet. 

From trial and error, I also learned that the input command is a string type and in order to use it as an integer, it needs to be converted using toInt().

x
Share this Project

Courses

Focused on
About

Making an LED blink & Making a connected LED