Skill Dev I

Made by Emily Liu · UNLISTED (SHOWN IN POOLS)

LED blink

Created: November 18th, 2021

0

Exercise 1: blink every 3 seconds 

0
int ledPin = D3;
void setup() {
   pinMode(ledPin, OUTPUT);
   
}
void loop() {
    
    digitalWrite(ledPin, HIGH);
    delay(3000);
    
    digitalWrite(ledPin, LOW);
    delay(3000);
    
 
}
Click to Expand
0
blink1
Emily Liu - https://youtu.be/mP_Uwo3Le0E
0

Exercise 2: blink every half second 5 times, pause for 3 seconds, start again

0
int ledPin = D3;

void setup() {

   pinMode(ledPin, OUTPUT);
   
}

void loop() {
    
    for(int i=0; i<5; i+=1){ //int starts at 0, increases by 1 at each loop, until it is at 5
        
        digitalWrite(ledPin, HIGH);
        delay(500);
        
        digitalWrite(ledPin, LOW);
        delay(500);
    }
    
    delay(3000); //waits 3 seconds
 

}
Click to Expand
0
blink2
Emily Liu - https://youtu.be/vYQbH4Cdo3U
0

Exercise 3: 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 led1 = D3;
int led2 = D2;

//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.


void setup() {
   pinMode(led1, OUTPUT);
   pinMode(led2, OUTPUT);
   
}
void loop() {
    
    digitalWrite(led1, HIGH);
    delay(500);
    digitalWrite(led1, LOW);
    
    digitalWrite(led2, HIGH);
    delay(500);
    digitalWrite(led2, LOW);

}
Click to Expand
0
blink3
Emily Liu - https://youtu.be/dVUcGO9f1CM
0

CLOUD Exercise 1: Modify the cloud function to blink the LED 3 times after it is called

0
int led1 = D3;
void setup() {
  pinMode(led1, OUTPUT);
  Particle.function("ThreeBlinks", threeBlink);
}


int threeBlink(String a){
    digitalWrite(led1, HIGH);
    delay(1000);
    digitalWrite(led1, LOW);
    delay(1000);
    
    digitalWrite(led1, HIGH);
    delay(1000);
    digitalWrite(led1, LOW);
    delay(1000);

    digitalWrite(led1, HIGH);
    delay(1000);
    digitalWrite(led1, LOW);
    delay(1000);
    return 1;
}
Click to Expand
0

CLOUD 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
int led1 = D3;
void setup() {
  pinMode(led1, OUTPUT);
  Particle.function("ThreeBlinks", threeBlink);
  Particle.function("nBlinks", nBlinks);
}


int threeBlink(String a){
    digitalWrite(led1, HIGH);
    delay(1000);
    digitalWrite(led1, LOW);
    delay(1000);
    
    digitalWrite(led1, HIGH);
    delay(1000);
    digitalWrite(led1, LOW);
    delay(1000);

    digitalWrite(led1, HIGH);
    delay(1000);
    digitalWrite(led1, LOW);
    delay(1000);
    return 1;
}

int nBlinks(String a){
    int n =  a.toInt();
    for(int i = 0; i<n; i++){
    digitalWrite(led1, HIGH);
    delay(1000);
    digitalWrite(led1, LOW);
    delay(1000); 
    }

    return 1;
}
Click to Expand
0

CLOUD 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
int ledPin1 = D2;
int ledPin2 = D3;

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

   // Initialize both the LEDs to be OFF
   digitalWrite(ledPin1, LOW);
   
   pinMode(ledPin2, OUTPUT);
   digitalWrite(ledPin2, LOW);
   Particle.function("led1", ledControl1);
   Particle.function("led2", ledControl2);
}

void loop() {
     // Nothing to do here

}

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;
}

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
x
Share this Project

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


Courses

About

LED blink