Skills Dev I : Yashasvi
Made by ytulchiy · UNLISTED (SHOWN IN POOLS)
Made by ytulchiy · UNLISTED (SHOWN IN POOLS)
To create a simple led connection and control it .
Created: November 5th, 2020
Making an LED Blink
// 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
// 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
// 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
// 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
//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
//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
//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
// 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