A Simple Internet Appliance
Made by Yuhan Wu
Made by Yuhan Wu
In this exercise, we’re going to create the first circuit, create a short program and control some components with code.
Created: November 1st, 2023
Get to learn about Particle and get hands on the circuit.
int led = D2;
void setup() {
pinMode( led, OUTPUT );
// D2 is the out put pin
}
void loop() {
for( int i = 0; i < 5; i++ ) {
//loop for 5 times
digitalWrite( led, HIGH );
delay( 500 );
digitalWrite( led, LOW );
delay( 500 );
//each blink is 1/2 second
}
digitalWrite( led, LOW );
delay( 3000 );
//stop for 3 seconds after 5 blinks
}
Click to Expand
//Setup variables
int led = D2;
//orange LED
int led1 = D3;
//green LED
//it is obvious that the green LED is much lower in brightness
void setup() {
pinMode( led, OUTPUT );
pinMode( led1, OUTPUT );
}
void loop() {
digitalWrite( led, HIGH );
digitalWrite( led1, LOW );
delay( 1000 );
digitalWrite( led1, HIGH );
digitalWrite( led, LOW );
delay( 1000 );
//turning one of them on and another off together at the same time
}
Click to Expand
int ledPin = D2;
void setup() {
pinMode( ledPin, OUTPUT );
digitalWrite( ledPin, 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 < 3; i++ ) {
digitalWrite( ledPin, HIGH );
delay( 500 );
digitalWrite(ledPin, LOW );
delay( 500 );
}
}
else if( command == "HIGH" ) {
state = HIGH;
} else if( command == "LOW" ) {
state = LOW;
} else {
return -1;
}
digitalWrite( ledPin, state );
return 1;
}
void loop() {
}
Click to Expand
int ledPin = D2;
void setup() {
pinMode( ledPin, OUTPUT );
digitalWrite( ledPin, LOW );
//LED is initially turned off.
Particle.function( "led", ledControl );
//register function
}
int ledControl( String command ) {
int state = LOW;
int val = command.toInt( );
//toInt mentioned in arduino's website
//https://www.arduino.cc/reference/en/language/variables/data-types/string/functions/toint/
for( int i=0; i < val; i++ ){
digitalWrite( ledPin, HIGH );
delay( 500 );
digitalWrite(ledPin, LOW );
delay( 500 );
}
digitalWrite( ledPin, state );
return 1;
}
void loop() {
}
Click to Expand
int ledPin1 = D2;
int ledPin2 = D1;
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 < 3; i++ ) {
digitalWrite( ledPin1, HIGH );
digitalWrite( ledPin2, HIGH );
delay( 500 );
digitalWrite(ledPin1, LOW );
digitalWrite(ledPin2, LOW );
delay( 500 );
}
}
else if( command == "CALL1" ) {
for ( int i = 0; i < 3; i++ ) {
digitalWrite( ledPin1, HIGH );
delay( 500 );
digitalWrite(ledPin1, LOW );
delay( 500 );
}
}
else if( command == "CALL2" ) {
for ( int i = 0; i < 3; i++ ) {
digitalWrite( ledPin2, HIGH );
delay( 500 );
digitalWrite(ledPin2, LOW );
delay( 500 );
}
}
else if( command == "HIGH" ) {
state = HIGH;
} else if( command == "LOW" ) {
state = LOW;
} else {
return -1;
}
digitalWrite( ledPin1, state );
digitalWrite( ledPin2, state );
return 1;
}
void loop() {
}
Click to Expand
First take away for me might be a more common situation for beginners of circuit and coding, which is missing semicolons, comma and other elements of coding.
To be specific, like replacing the semicolons in the for loop with comma.
Another one is to locate the toINT command to turn string command into int. Which I found on the Arduino website.
https://www.arduino.cc/reference/en/language/variables/data-types/string/functions/toint/
The website us really helpful to find relevant commands.
A hands-on introductory course exploring the Internet of Things and connected product experiences.
In this exercise, we’re going to create the first circuit, create a short program and control some components with code.