Yifan Chen - skill dev 1

Made by yifanch2 ·

Created: November 8th, 2021

0

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() {
            digitalWrite(ledPin, HIGH);   
            delay(3000);               
            
            digitalWrite(ledPin, LOW);  
            delay(3000);   
}
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() {
    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 ledPin = D2;
int ledPin2 = D3;

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

void loop() {
    
    digitalWrite(ledPin, HIGH);
    digitalWrite(ledPin2, LOW);
    delay(500); 
    digitalWrite(ledPin, LOW);
    digitalWrite(ledPin2, HIGH);
    delay(500); 
}
Click to Expand
0

Exercise 4 / Exercise 5

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

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 blinkings it should turn the LED off

0
// name the pins
int ledPin = D2;
int blinks(String x);
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);
   
   Particle.function("bks", blinks);
}

void loop() {

}

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 if (command == "blink"){
      for (int x = 3; x>=0;x--)
      {
            digitalWrite(ledPin, HIGH);   
            delay(1000);               
            
            digitalWrite(ledPin, LOW);  
            delay(1000);              
      }
  }
   
  else{
	   return -1;
  }
   
      // write to the appropriate pin
  digitalWrite(ledPin, state);
  return 1;
}
   
 
int blinks(String blinkTimes)
{
    // int x = stoi(blinkTimes);
    int x = blinkTimes.toInt();
    int state = LOW;
    
    
    
    while(x>0)
    {
        digitalWrite(ledPin, HIGH);   
        delay(1000);               
            
        digitalWrite(ledPin, LOW);  
        delay(1000); 
        x--;
    }
    
    return 1;
}
Click to Expand
0

Exercise 6

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 ledPin = D2;
int ledPin2 = D3;
int blinks(String x);
void setup() {
// Configure the pins to be outputs
   pinMode(ledPin, OUTPUT);
   pinMode(ledPin2, OUTPUT);

   // Initialize both the LEDs to be OFF
   digitalWrite(ledPin, LOW);
   digitalWrite(ledPin2, LOW);
   
     //Register our Particle function here
   Particle.function("led", ledControl);
   
   Particle.function("bks", blinks);
   Particle.function("led2", ledControl2);
   
   Particle.function("bks2", blinks2);
}

void loop() {

}

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 if (command == "blink"){
      for (int x = 3; x>=0;x--)
      {
            digitalWrite(ledPin, HIGH);   
            delay(1000);               
            
            digitalWrite(ledPin, LOW);  
            delay(1000);              
      }
  }
   
  else{
	   return -1;
  }
   
      // write to the appropriate pin
  digitalWrite(ledPin, state);
  return 1;
}
   
 
int blinks(String blinkTimes)
{
    // int x = stoi(blinkTimes);
    int x = blinkTimes.toInt();
    int state = LOW;
    
    
    
    while(x>0)
    {
        digitalWrite(ledPin, HIGH);   
        delay(1000);               
            
        digitalWrite(ledPin, LOW);  
        delay(1000); 
        x--;
    }
    
    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 if (command == "blink"){
      for (int x = 3; x>=0;x--)
      {
            digitalWrite(ledPin2, HIGH);   
            delay(1000);               
            
            digitalWrite(ledPin2, LOW);  
            delay(1000);              
      }
  }
   
  else{
	   return -1;
  }
   
      // write to the appropriate pin
  digitalWrite(ledPin2, state);
  return 1;
}
   
 
int blinks2(String blinkTimes)
{
    // int x = stoi(blinkTimes);
    int x = blinkTimes.toInt();
    int state = LOW;
    
    
    
    while(x>0)
    {
        digitalWrite(ledPin2, HIGH);   
        delay(1000);               
            
        digitalWrite(ledPin2, LOW);  
        delay(1000); 
        x--;
    }
    
    return 1;
}
Click to Expand
0

Sometimes I will forget the code in the setup, finding it is hard to debug.

Also, the input of the function can only be an integer. 

x
Share this Project

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



About

~