Skills Dev I - Lulin Shan
Made by Lulin Shan ·
Made by Lulin Shan ·
Created: November 11th, 2021
Following the guidance and tutorials, I was able to make simple circuits to create a blinking LED, then made a connected LED.
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() {
// First... On
digitalWrite( ledPin, HIGH ); //turn LED on
delay(3000); //wait for 3 seconds
// Now...Off
digitalWrite( ledPin, LOW ); //turn LED off
delay(3000); //wait for 3 seconds
// rinse + repeat
}
Click to Expand
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() {
// loop to turn LED on 5 times
for (int i= 0; i<5; i++){
digitalWrite( ledPin, HIGH ); //turn LED on
delay(500); //wait for 0.5 second
digitalWrite( ledPin, LOW ); //turn LED off
delay(500); //wait for 0.5 second
}
delay(3000); // wait for 3 seconds
//rinse + repeat
}
Click to Expand
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.
I connected another led to the circuit and defined it as another ledPin to control the two LEDs on/off state at the same time.
int ledPin1 = D2;
int ledPin2 = D5;
void setup() {
// We want to tell the Argon that we'll use D2 and D5 as output pins.
pinMode( ledPin1, OUTPUT);
pinMode( ledPin2, OUTPUT);
}
void loop() {
digitalWrite( ledPin1, HIGH ); //turn LED1 on
digitalWrite( ledPin2, LOW ); //turn LED2 off
delay(2000); //wait for 2 second2
digitalWrite( ledPin1, LOW ); //turn LED1 off
digitalWrite( ledPin2, HIGH ); //turn LED2 on
delay(2000); //wait for 2 second2
// rinse + repeat
}
Click to Expand
// name the pins
int ledPin = D2;
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);
}
void loop()
{
// Nothing to do here
}
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(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
// name the pins
int ledPin = D2;
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("ledb", ledBlink);
}
void loop()
{
// Nothing to do here
}
int ledBlink(String cntInput)
{
int cnt = atoi(cntInput);
if (cnt <= 0){
return -1;
}
for(int i = 0; i < cnt; i++){
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
}
// turn the LED off
digitalWrite(ledPin, LOW);
return 1;
}
Click to Expand
// name the pins
int ledPin1 = D2;
int ledPin2 = D4;
void setup()
{
// Configure the pins to be outputs
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
// Initialize both the LEDs to be OFF
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
//Register our Particle function here
Particle.function("led1", ledControl1);
Particle.function("led2", ledControl2);
}
void loop()
{
// Nothing to do here
}
//control LED 1
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;
}
//control LED 2
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
It was very smooth to complete the first 3 exercises by following the instructions. It took me a bit of experiment with writing the code for passing the number of LED blinks, because I need to look up additional functions to make it work.
I found watching the class recording and having the detailed instructions in the tutorial really helpful. Through this exercise, I have learned how to build a simple circuit. It was fun to write the code and build a circuit and test on it. My next steps are to get more familiar with the programming language and learn the affordances of more Internet appliances.