Back to Parent

//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

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0