Back to Parent

//Exercise 2_3

int led1 = D2;
int led2 = D3;

void setup() {
    pinMode(led1, OUTPUT);
    pinMode(led2, OUTPUT);
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
    Particle.function("ledControlByTimes1", myLedControl1);
    Particle.function("ledControlByTimes2", myLedControl2);
}

void loop() {

}

int myLedControl1(String command) {
    int times = command.toInt();
    int state = LOW;
    for(int i = 1; i <= times * 2; i++) {
        if(i % 2 == 0) {
                state = HIGH;
                digitalWrite(led1, state);
                delay(500);
            } else {
            state = LOW;
            digitalWrite(led1, state);
            delay(500);
            }
    }
    digitalWrite(led1, LOW);
    return 1;
}

int myLedControl2(String command) {
    int times = command.toInt();
    int state = LOW;
    for(int i = 1; i <= times * 2; i++) {
        if(i % 2 == 0) {
                state = HIGH;
                digitalWrite(led2, state);
                delay(500);
            } else {
            state = LOW;
            digitalWrite(led2, state);
            delay(500);
            }
    }
    digitalWrite(led2, LOW);
    return 1;
}
Click to Expand

Content Rating

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

0