//Part 1
//Create variable connecting to D2
int ledPin = D2;
void setup() {
//Create a function led blink
Particle.function("ledBlink",ledBlink);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
}
void loop() {
}
//Initiate function led blink
int ledBlink(String command)
{
int state = LOW;
//If command is high, blink 3 times
if(command == "HIGH")
{
digitalWrite(ledPin,LOW);
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);
delay(500);
}
//Else return -1
else
{
return -1;
}
}
//Part 2
//Create variable for D2
int ledPin = D2;
void setup() {
//Create a function to control the led
Particle.function("led",ledControl);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
}
void loop() {
}
//Initiate the led function
int ledControl(String command)
{
int amnt = 0;
//If the amount is not 0 then create an variable of the number
if(command != "0")
{
amnt = command.toInt();
}
else
{
return -1;
}
//Then run a loop to blink once for the number of the variable amount
for (int i=1; i <= amnt; i++)
{
digitalWrite(ledPin, LOW);
delay(50);
digitalWrite(ledPin, HIGH);
delay(50);
digitalWrite(ledPin, LOW);
delay(50);
}
return amnt;
}
//Part 3
//Create 2 variables, one for D2 and one for D3
int led1 = D2;
int led2 = D3;
//Create a function to control the different lights
void setup()
{
Particle.function("lights",lights);
pinMode(led1, OUTPUT);
digitalWrite(led1, LOW);
pinMode(led2, OUTPUT);
digitalWrite(led2, LOW);
}
void loop()
{}
//have 4 different commands to determine which light to turn on
int lights(String command)
{
int state = LOW;
int light = led1;
if(command == "gron")
{
state = HIGH;
light = led1;
}
else if (command == "groff")
{
state = LOW;
light = led1;
}
else if (command == "redon")
{
state = HIGH;
light = led2;
}
else if (command == "redoff")
{
state = LOW;
light = led2;
}
else
{
return -1;
}
digitalWrite(light, state);
return light;
}
Click to Expand