Skills Dev I: A Simple Internet Appliance

Made by Ziru Wei

get familiar with Particle platform and implement a simple connected LED product

Created: October 28th, 2024

0


Intention

I haven't touched an Arduino board in a long time. Through this practice, I've managed to recapture some of the old feelings, and I'm excited to work with Photon hardware. It's similar to Arduino but can connect to the online platform with itself to manipulate, which is really cool!

0

Outcome

welcome to watch it ⬇️

https://vimeo.com/1024126178?share=copy

0

Process

1/First Sketch

Exercise 1

Modify the program to Blink on and off every 3 seconds.

0
//first sketch-01

int ledPin = D3;    

void setup(){
    // declare how the pins will be used
    pinMode(ledPin, OUTPUT);     // pinMode is a function that let’s us say what the pins will do
}

void loop() {
  // on
  digitalWrite(ledPin, HIGH);   // digitalWrite is like a "pump" that can be turn on and off
  delay(3000);  // 3000 ms

  // off
  digitalWrite(ledPin, LOW);
  delay(3000);
}
Click to Expand
0

Exercise 2

Change the program to blink on and off 5 times then stop for 3 seconds. Each blink should be 0.5s.

0
//first sketch-02

int ledPin = D2;

void setup()
{
   pinMode(ledPin, OUTPUT);
   digitalWrite(ledPin, LOW);
   Particle.function("led", ledControl);
}

void loop()
{

}


int ledControl(String command)
{
   int blinkTime = atoi(command);
   {
        for (int i = 0; i < blinkTime; i++)
        {
    	    digitalWrite(ledPin, HIGH);
    	    delay(1000);
    	    digitalWrite(ledPin, LOW);
    	    delay(1000);
        }
        return 1;
   }
   return 0;
}
Click to Expand
0

Exercise 3
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.

0
//first sketch-03

int redLedPin = D3;
int oraLedPin = D2;

void setup(){
    pinMode(oraLedPin, OUTPUT);
    pinMode(redLedPin, OUTPUT);
}

void loop() {
  // red on, orange off
  digitalWrite(oraLedPin, HIGH);
  digitalWrite(redLedPin, LOW);
  delay(3000);

  // red off, orange on
  digitalWrite(oraLedPin, LOW);
  digitalWrite(redLedPin, HIGH);
  delay(3000);
}
Click to Expand
0

Extra Exploration 3-1

This is an extra exploration of the 2 LED circuit: This code controls two LEDs, red and orange, to alternately blink with changing speeds. The main loop of the program cycles through functions to speed up and then slow down the blinking. The speedUp function gradually decreases the delay between the blinking from a longer to a shorter interval, making the LEDs blink faster. Conversely, the slowDown function increases the delay, making the LEDs blink slower. This creates a visual effect of accelerating and decelerating light pulses between the two LEDs.

0
//first sketch - exploration - 3 - 1

int redLedPin = D3;
int oraLedPin = D2;

void setup()
{
    pinMode(oraLedPin, OUTPUT);
    pinMode(redLedPin, OUTPUT);
}

void loop()
{
    speedUp();
    slowDown();
}

void speedUp()
{
    // gradually speed up 
    for (int i = 0; i < 10; i++) 
    {
        // red on, orange off
        digitalWrite(oraLedPin, HIGH);
        digitalWrite(redLedPin, LOW);
        delay(i * 200);
    
        // red off, orange on
        digitalWrite(oraLedPin, LOW);
        digitalWrite(redLedPin, HIGH);
        delay(i * 200);
    }
}

void slowDown()
{
    // gradually slow doen
    for (int i = 10; i > 0; i--) 
    {
        // red on, orange off
        digitalWrite(oraLedPin, HIGH);
        digitalWrite(redLedPin, LOW);
        delay(i * 200);
    
        // red off, orange on
        digitalWrite(oraLedPin, LOW);
        digitalWrite(redLedPin, HIGH);
        delay(i * 200);
    }
}
Click to Expand
0

2/Connected LED

Exercise 1

Modify the cloud function to blink the LED 3 times after it is called.

0
//connected LED - 01

int ledPin = D2;

void setup()
{
   pinMode(ledPin, OUTPUT);
   digitalWrite(ledPin, LOW);
   //Register our Particle function here
   Particle.function("led", ledControl);
}

void loop()
{

}


int ledControl(String command)
{
   if(command.toLowerCase() == "blink")
   {
        for (int i = 0; i < 3; i++)
        {
    	    digitalWrite(ledPin, HIGH);
    	    delay(1000);
    	    digitalWrite(ledPin, LOW);
    	    delay(1000);
        }
        return 1;
   }
   return 0;
}
Click to Expand
0

Exercise 2

Pass the number of times user would like it to blink. Set the function to blink that number of times. Finally once it has completed all of the blinking it should turn the LED off.

0
//connected LED - 02

int ledPin = D2;

void setup()
{
   pinMode(ledPin, OUTPUT);
   digitalWrite(ledPin, LOW);
   Particle.function("led", ledControl);
}

void loop()
{

}


int ledControl(String command)
{
   int blinkTime = atoi(command);
   {
        for (int i = 0; i < blinkTime; i++)
        {
    	    digitalWrite(ledPin, HIGH);
    	    delay(1000);
    	    digitalWrite(ledPin, LOW);
    	    delay(1000);
        }
        return 1;
   }
   return 0;
}
Click to Expand
0

Exercise 3

Add a second LED to the circuit. Change the program and cloud function to allow you to control both LEDs remotely.

Due to the instructions is sort of brief here, I was wondering about how exactly to construct the blinking mechanism. Initially, I planned to use the '+' and '-' keys on the keyboard to control the alternating blinking speed of the two LEDs. However, after attempting this, I realized that user input was only prompted at the start of the program. Consequently, I opted to allow the user to input the blinking interval as a way to alternate the LED flashing.

0
//connected LED - 03

// Define the pins
int ledPinOrange = D2;
int ledPinRed = D3;

void setup()
{
    // Configure the pins as outputs
    pinMode(ledPinOrange, OUTPUT);
    pinMode(ledPinRed, OUTPUT);

    digitalWrite(ledPinOrange, LOW);
    digitalWrite(ledPinRed, LOW);

    Particle.function("led", ledControl);
}

void loop()
{
}


int ledControl(String command)
{   
    int blinkInterval = atoi(command);
    blinkLeds(blinkInterval);
    return 1; 
}

void blinkLeds(int delayTime)
{
    for (int i = 0; i < 50; i++) 
    {
        // Red on, orange off
        digitalWrite(ledPinOrange, LOW);
        digitalWrite(ledPinRed, HIGH);
        delay(delayTime);

        // Red off, orange on
        digitalWrite(ledPinOrange, HIGH);
        digitalWrite(ledPinRed, LOW);
        delay(delayTime);
    }

    // Turn off LEDs after blinking
    digitalWrite(ledPinOrange, LOW);
    digitalWrite(ledPinRed, LOW);
}
Click to Expand
0

But when users enter something that's not a number, the LEDs stop working. So, I'm planning to do something to keep them working in a default mode and send out a signal to let users know there's something wrong with their input.


*An iteration:

In the refined version of my code, I introduced a new function, parseBlinkInterval, which robustly converts the incoming string to an integer and validates it. This function uses strtol for conversion and checks if the entire string was valid for conversion. If not, it returns -1 as an error indicator.

Back in the ledControl function, I added a check for the -1 error code from parseBlinkInterval. If an invalid interval is detected, I default the blinking to a standard delay of 1000 milliseconds as a fallback, ensuring the program remains operational even with incorrect input.

0
//connected LED - 03-01 iteration

int ledPinOrange = D2;
int ledPinRed = D3;

void setup()
{
    pinMode(ledPinOrange, OUTPUT);
    pinMode(ledPinRed, OUTPUT);

    digitalWrite(ledPinOrange, LOW);
    digitalWrite(ledPinRed, LOW);

    Particle.function("led", ledControl);
}

void loop()
{
}


int ledControl(String command)
{   
    int blinkInterval = parseBlinkInterval(command);
    if (blinkInterval == -1) {
        blinkLeds(1000);  // Return error code if input is not valid
    }
    blinkLeds(blinkInterval);
    return 1;  // Return success
}

int parseBlinkInterval(String input)
{
    // Attempt to convert the input string to an integer
    char* endptr;
    long int interval = strtol(input.c_str(), &endptr, 10);
    
    // Check if the conversion was successful
    if (*endptr != '\0') {
        return -1; // Return -1 if the input is not a valid integer
    }

    return interval; // Return the valid interval
}

void blinkLeds(int delayTime)
{
    for (int i = 0; i < 50; i++)  // Run the blinking 10 times
    {
        // Red on, orange off
        digitalWrite(ledPinOrange, LOW);
        digitalWrite(ledPinRed, HIGH);
        delay(delayTime);

        // Red off, orange on
        digitalWrite(ledPinOrange, HIGH);
        digitalWrite(ledPinRed, LOW);
        delay(delayTime);
    }

    // Turn off LEDs after blinking
    digitalWrite(ledPinOrange, LOW);
    digitalWrite(ledPinRed, LOW);
}
Click to Expand
0

3/Connected RGB LED

0
// RGB LED

int redPin = D2;
int greenPin = D3;
int bluePin = D4;
int redValue = 255;
int greenValue = 255;
int blueValue = 255;

void setup()
{
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);

  Particle.function("led", ledControl);

  analogWrite(redPin, redValue);
  analogWrite(greenPin, greenValue);
  analogWrite(bluePin, blueValue);
}

void loop()
{
}

int ledControl(String command)
{
    String colors[3];
    colors[0]="";
    colors[1]="";
    colors[2]="";

    int index = 0;
    for(int i = 0; i < command.length(); i++)
    {
      if(index < 3)
      {
        char c = command.charAt(i);
        colors[index] += c;

        if(c == ',') index++;
      }
    }

    redValue = colors[0].toInt();
    greenValue = colors[1].toInt();
    blueValue = colors[2].toInt();

   analogWrite(redPin, redValue);
   analogWrite(greenPin, greenValue);
   analogWrite(bluePin, blueValue);

   return 1;
}
```
Click to Expand
0

Reflection / Next Steps

  • Particle document really helps! But there are many unfamiliar technical concepts and terms so I need more time to digest it.
  • ChatGPT is also a good assistant in checking the syntax etc. But sometimes it only provides code that seems can run but turns out not that obvious or proper when running in the board. So me as a designer must shoulder the responsibility to observe, hand-on adjust, and iterate the overall design.
  • The next steps may be:
    • dive into document
    • explore how to combine user input on a screen and tangible user input or sensor input as a system to manipulate the output.
x
Share this Project


Focused on
About

get familiar with Particle platform and implement a simple connected LED product