Skills Dev I: A Simple Internet Appliance
Made by Xuan Peng
Made by Xuan Peng
Created: November 2nd, 2023
//Exercise 1.2
int led = D2;
void setup() {
pinMode( led, OUTPUT );
}
void loop() {
for( int i = 0; i < 5; i++ ) {
//loop for 5 times
digitalWrite( led, HIGH );
delay( 500 );
//Delay 0.5 second
digitalWrite( led, LOW );
delay( 500 );
}
digitalWrite( led, LOW);
delay( 3000);
//stop for 3 seconds
}
Click to Expand
//Exercise 1.3
int led1 = D2;
int led2 = D0;
//define 2 led
void setup() {
pinMode( led1, OUTPUT );
pinMode( led2, OUTPUT );
}
void loop() {
digitalWrite( led1, HIGH );
digitalWrite( led2, LOW);
delay( 3000 );
digitalWrite( led1, LOW );
digitalWrite( led2, HIGH );
delay( 3000 );
//the 2 led work in opposite way
}
Click to Expand
//Function Exercise 1
// name the pins
int ledPin = D2;
void setup() {
//Register our Particle function here
Particle.function("led", ledControl);
// Configure the pins to be outputs
pinMode(ledPin, OUTPUT);
// Initialize both the LEDs to be OFF
digitalWrite(ledPin, LOW);
}
void loop() {
}
int ledControl(String command)
{
int state = LOW;
// Make it blink!
if(command == "BLINK"){
for (int i = 0; i<3; i++){
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);}
//other states
}else if (command == "HIGH"){
state = HIGH;
}else if (command == "LOW"){
state = LOW;
}else{
return -1;
}
// write to the appropriate pin
digitalWrite(ledPin, state);
return 1;
}
Click to Expand
//Function Exercise 2
// name the pins
int ledPin = D2;
void setup() {
//Register our Particle function here
Particle.function("led", ledControl);
// Configure the pins to be outputs
pinMode(ledPin, OUTPUT);
// Initialize both the LEDs to be OFF
digitalWrite(ledPin, LOW);
}
void loop() {
}
int ledControl(String command)
{
int state = LOW;
int val = command.toInt( );
// Converts a valid String to an integer
for (int i = 0; i< val ; i++){
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);}
// write to the appropriate pin
digitalWrite(ledPin, state);
return 1;
}
Click to Expand
//fuction exercise 3
int ledPin1 = D1;
int ledPin2 = D2;
void setup() {
pinMode( ledPin1, OUTPUT );
pinMode( ledPin2, OUTPUT );
digitalWrite( ledPin1, LOW );
digitalWrite( ledPin2, LOW );
//LED is initially turned off.
Particle.function( "led", ledControl );
//register function
}
int ledControl( String command ) {
int state = LOW;
if( command == "CALL" ) {
for ( int i = 0; i < 5; i++ ) {
digitalWrite( ledPin1, HIGH );
digitalWrite( ledPin2, HIGH );
delay( 300 );
digitalWrite(ledPin1, LOW );
digitalWrite(ledPin2, LOW );
delay( 300 );
}
}
else if( command == "CALL1" ) {
for ( int i = 0; i < 5; i++ ) {
digitalWrite( ledPin1, HIGH );
delay( 300 );
digitalWrite(ledPin1, LOW );
delay( 300 );
}
}
else if( command == "CALL2" ) {
for ( int i = 0; i < 5; i++ ) {
digitalWrite( ledPin2, HIGH );
delay( 300 );
digitalWrite(ledPin2, LOW );
delay( 300 );
}
}
else {
return -1;
}
digitalWrite( ledPin1, state );
digitalWrite( ledPin2, state );
return 1;
}
void loop() {
}
Click to Expand
A hands-on introductory course exploring the Internet of Things and connected product experiences.
~