Skills Dev 1 - wting
Made by Wei-Ni Ting
Made by Wei-Ni Ting
Created: November 16th, 2020
Modify the program to Blink on and off every 3 seconds.
//exercise 2: blink on and off 5 times then stop for 3 sec
int ledPin = D2;
void setup() {
pinMode (ledPin, OUTPUT);
}
void loop(){
for (int i = 0; i < 5; i++){ //repeat 0.5sec blinking for 5 times
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
}
digitalWrite(ledPin, LOW); //stop for 3 sec
delay(3000);
}
Click to Expand
//exercise 3: add a second LED to the circuit and alternate blinks
int led1 = D2;
int led2 = D3;
void setup() {
pinMode (led1, OUTPUT);
pinMode (led2, OUTPUT);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
}
void loop(){
digitalWrite(led1, HIGH); //turn on LED 1
delay(500);
digitalWrite(led1, LOW); //turn off LED 1
delay(500);
digitalWrite(led2, HIGH); //turn on LED 2
delay(500);
digitalWrite (led2, LOW); //turn off LED 2
delay (500);
//repeat
}
Click to Expand
//connected LED: exercise 1, blink LED 3 times after it is called
int led1 = D2;
void setup() {
//Register Particle function here
Particle.function("led", ledControl);
pinMode (led1, OUTPUT);
digitalWrite(led1, LOW); //LED 1 initial state to be off
}
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(led1, HIGH);
delay(500);
digitalWrite(led1, LOW);
delay(500);
}
}else if (command == "LOW"){
state = LOW;
}else{
return -1;
}
//write to the appropriate pin
digitalWrite(led1, state);
return 1;
}
Click to Expand
//connected LED: exercise 2, set the function to blink that number of times
int led1 = D2;
int value = 0;
void setup() {
//Register Particle function here
Particle.function("led", ledControl);
pinMode (led1, OUTPUT);
digitalWrite(led1, LOW); //LED 1 initial state to be off
}
void loop(){
//nothing to do here
}
int ledControl(String command)
{
int state = LOW;
value = command.toInt();
if(value>0){
for (int i = 0; i < value ; i++){
digitalWrite(led1, HIGH);
delay(500);
digitalWrite(led1, LOW);
delay(500);
}
}
else{
return -1;
}
//write to the appropriate pin
digitalWrite(led1, state);
return 1;
}
Click to Expand
//connectedled:exercise 3: add a second LED to the circuit, control both LEDs remotely
int led1 = D2;
int led2 = D3;
int value = 0;
void setup() {
//Register Particle function here
Particle.function("led", ledControl);
pinMode (led1, OUTPUT);
digitalWrite(led1, LOW); //LED 1 initial state to be off
pinMode (led2, OUTPUT);
digitalWrite(led2, LOW); //LED 2 initial state to be off
}
void loop(){
//nothing to do here
}
int ledControl (String command)
{
int state = LOW;
//find out the state of the led
if (command == "1") {
digitalWrite(led1, HIGH);
}
if (command == "2"){
digitalWrite(led2, HIGH);
}
if (command == "0"){
state = LOW;
}
if (command == "-1"){
digitalWrite(led1, LOW);
}
if (command == "-2"){
digitalWrite(led2, LOW);
}else{
return -1;
}
//write to the appropriate pin
digitalWrite(led1, state);
digitalWrite(led2, state);
return 1;
}
Click to Expand
For the "making an LED blink" part of the exercise, I think it is quite straightforward by following the instructions. It took a bit of experiments and thinking through what should be changed in coding in the different exercises, as well as the circuit, but it helped me understand the coding and circuits better.
"Making a Connected LED" took longer for me to figure out the coding. I had trouble with exercise 3 in connected LED particularly. I tried several ways in the coding to set up 2 functions on console; however, I wasn't able to control one LED through one function; whenever I try to control one LED through console, both LED will light up. In the end, it only worked when I use one function, and set up different "numbers" corresponding to different actions for particular LED, ex. type in 1 to turn on LED 1; type in 2 to turn on LED 2". I think there should be a better way of controlling the different LED remotely; with more experiments and knowledge in coding, I will probably be more familiar with it.