Internet Connected LED-Pot

Made by malikak · UNLISTED (SHOWN IN POOLS)

Learning to use the Particle platform by creating an internet-controllable LED.

Created: November 14th, 2020

0

Outcome

A dimmable LED that can be turned on/off by pushbutton or through the Particle Cloud API. 



0
int buttonPin = D3;
int buttonState; // has the button been pushed
int lastPress = 0;
int buttonDelay = .5*1000;

int ledPin = D2;
bool ledOn = false;
int ledBrightness = 0;

int potPin = A1;
int potReading = 0;

void setup() {
    pinMode( ledPin, OUTPUT );
    pinMode( buttonPin, INPUT_PULLUP );
    // Add a pullup resistor to the pin (prevent floating pin, where button is connected to the digital pin)

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

void loop() {
    int elapsedTime = millis()-lastPress;

    buttonState = digitalRead(buttonPin);
    potReading = analogRead(potPin); // from 0 to 4095
    ledBrightness = map(potReading, 0, 4095, 0, 255);
    
    if ((buttonState == LOW) && (elapsedTime > buttonDelay)) {
        lastPress = millis();
        ledOn = !ledOn;
    }

    if (ledOn == true) {
        analogWrite(ledPin, ledBrightness);
    }
    else {
        digitalWrite(ledPin, LOW);
    }
}

int ledControl(String command) {
    // find out the state of the led
    if(command == "HIGH"){
        ledOn = true;
    }else if(command == "LOW"){ 
        ledOn = false;
    }else{
        return -1;
    }
    return 1;
}
Click to Expand
0

Process 

I initially tried to put together all the code and circuit at once. It didn't work, and was not immediately clear what was going wrong even after some debugging (turns out I just forgot to connect my potentiometer to power). I scaled back to the basic pushbutton-LED demo code, then added back the potentiometer code and finally realized my wiring issue.

I wanted to play with exposing a Particle function, and initially forgot to return an int. I also realized it sometimes takes a few refreshes to show up on the console.

0

Reflection

I wanted to get set up with Particle in VSCode + using the CLI, which took way longer than I expected! Hopefully now that it's set up, things will go more smoothly.

I don't completely understand the difference between POST and the cURL commands. Is it just PHP vs cURL to make the REST request? I just used the Particle web console to control the LED, but I'd like to understand how to interact with the API directly too.

0
Particle web-connected LED demo
Malika Khurana - https://youtu.be/GUwZ3qNIa7U
x
Share this Project

This project is only listed in this pool. Be considerate and think twice before sharing.


Courses

Focused on
About

Learning to use the Particle platform by creating an internet-controllable LED.