Yuchuan Shan – Skills Dev I
Made by Yuchuaun Shan
Made by Yuchuaun Shan
Making an LED blink & Making a connected LED
Created: November 8th, 2021
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
// 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
// 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
// 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
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().