int ledPinBl = D5;
int ledPinRe = D2;
int ledPin;
void setup() {
// Configure the pins to be outputs
pinMode(ledPinBl, OUTPUT);
pinMode(ledPinRe, OUTPUT);
// Initialize both the LEDs to be OFF
digitalWrite(ledPinBl, LOW);
digitalWrite(ledPinRe, LOW);
//Register our Particle function here
Particle.function("strength", strengthControl);
Particle.function("blink 3 times", blinkControl1);
Particle.function("blink n times", blinkControl2);
Particle.function("which LED you want to control", ledControl);
}
void loop() {
}
// 1 means the command works while -1 means fails
int ledControl(String command)
{
// get which led the user want to control
if(command == "RED"){
ledPin = ledPinRe;
}else if(command == "BLUE"){
ledPin = ledPinBl;
}else{
return -1;
}
// write to the appropriate pin and signal the user his/her choice
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
return ledPin;
}
int strengthControl(String command)
{
int state = LOW;
// find out the state of the led
if(command == "HIGH"){
state = HIGH;
}else if(command == "LOW"){
state = LOW;
}else{
return -1;
}
// write to the appropriate pin
digitalWrite(ledPin, state);
return 1;
}
int blinkControl1(String command)
{
delay(500);
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
return 1;
}
int blinkControl2(String command)
{
// get the number of blinks wanted
int value = command.toInt();
// blink loop
if(value > 0){
for (int i = 1; i <= value; i++){
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
}
}
else{
return -1;
}
return 1;
}
Click to Expand