Skills Dev I : Yashasvi

Made by ytulchiy · UNLISTED (SHOWN IN POOLS)

To create a simple led connection and control it .

Created: November 5th, 2020

0

First Sketch

Making an LED Blink

0
// first sketch
// declare a variable and load it with value
int ledPinRed = D2;

void setup(){
    // set D2 as output pin
    pinMode(ledPin, OUTPUT);
}

void loop(){
    //switch it on
    digitalWrite(ledPin, HIGH);
    // wait for a second
    delay(1000);
    // switch it off
    digitalWrite(ledPin, LOW);
    // wait for a second
    delay(1000);
    // repeat
}
Click to Expand
0

Exercise 1

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

0
// declare a variable and load it with value
int ledPin = D2;

void setup(){
    // set D2 as output pin
    pinMode(ledPin, OUTPUT);
}

void loop(){
    //switch it on
    digitalWrite(ledPin, HIGH);
    // wait for three seconds
    delay(3000);
    // switch it off
    digitalWrite(ledPin, LOW);
    // wait for three seconds
    delay(3000);
    // 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
// exercise 2
// declare a variable and load it with value
int ledPin = D2;

void setup(){
    // set D2 as output pin
    pinMode(ledPin, OUTPUT);
}   

void loop(){
    // loop to blink 5 time
    for (int i=1; i<6; i++){
        digitalWrite(ledPin, HIGH);
        delay(500);
        digitalWrite(ledPin, LOW);
        delay(500);
    }
    // create a 3 second delay
    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
// exercise 3
// declare a variable and load it with value
int ledPinRed = D2;
int ledPinBlue = D4;

void setup(){
    // set D2 as output pin
    pinMode(ledPinRed, OUTPUT);
    pinMode(ledPinBlue, OUTPUT);
}      
    
void loop(){
    // switch red it on
    digitalWrite(ledPinRed, HIGH);
    // wait for 1 second
    delay(1000);
    // switch it off
    digitalWrite(ledPinRed, LOW);
    // switch blue on
    digitalWrite(ledPinBlue, HIGH);
    // wait for 1 second
    delay(1000);
    // switch it off
    digitalWrite(ledPinBlue, LOW);
    // code will repeat 
}
Click to Expand
0

Making a Connected LED

Controlling LED using the console or app

0
//controlling LED from Cloud
// declare a variable and load it with value
int ledPin = D2;

void setup() {
    pinMode(ledPin, OUTPUT);
    digitalWrite(ledPin, 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(ledPin, state);
   return 1;
}
Click to Expand
0

Exercise 1

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

0
//Cloud Control: Exercise 1
// declare a variable and load it with value
int ledPin = D2;

void setup() {
    // set D2 as output pin
    pinMode(ledPin, OUTPUT);
    // led to be off at start
    digitalWrite(ledPin, LOW);
    //Register our Particle function here
    Particle.function("led", ledControl);
}

void loop() {
}

int ledControl(String command)
{
  int state = LOW;
  // blinking the number of times mentioned
  if(command == "HIGH"){
      // blink 3 times
      for (int i=1; i<4; 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

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
//Cloud Control: Exercise 2
// declare a variable and load it with value
int ledPin = D2;

void setup() {
    // set D2 as output pin
    pinMode(ledPin, OUTPUT);
    // led to be off at start
    digitalWrite(ledPin, LOW);
    //Register our Particle function here
    Particle.function("led", ledControl);
}

void loop() {
}

int ledControl(String command)
{
  int state = LOW;
  // string to integer
  int num = command.toInt();
  // blinking the number of times mentioned
  if(num > 0){
      for (int i=1; i<num+1; i++){
      digitalWrite(ledPin, HIGH);
      delay(500);
      digitalWrite(ledPin, LOW);
      delay(500);
  }
  }else{
	   return -1;
  }
  // write to the appropriate pin
  digitalWrite(ledPin, state);
  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
// Cloud Control: Exercise 3
// declare a variable and load it with value
int ledPinRed = D2;
int ledPinBlue = D5;

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

void loop() {
}

int ledControl(String command)
{
   int state = LOW;
   if(command =="Red"){
       digitalWrite(ledPinRed, HIGH);
       digitalWrite(ledPinBlue, LOW);
   }else if(command == "Blue"){
       digitalWrite(ledPinBlue, HIGH);
       digitalWrite(ledPinRed, LOW);
   }else{
	   return -1;
   }
   return 1;
}
Click to Expand
0
0
0

Video Documentation

URL : https://www.youtube.com/watch?v=9OZCvdn6R10&list=PLjxBoZC5ywquutqxmxwdcPleYCRpFp4Am&index=1

0
0
x
Share this Project

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


Courses

About

To create a simple led connection and control it .