// -----------------------------------
// Controlling LEDs over the Internet
// -----------------------------------
// name the pins
int led1 = D0;
int led2 = D1;
int led3 = D2;
int led4 = D3;
int led5 = D4;
int led6 = D5;
int led7 = D6;
int led8 = D7;
// This routine runs only once upon reset
void setup()
{
//Register one Particle function here
Particle.function("led", ledControl);
//Register two Particle function here
Particle.function("menorah", SetDay);
// Configure the pins to be outputs
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
pinMode(led6, OUTPUT);
pinMode(led7, OUTPUT);
pinMode(led8, OUTPUT);
// Initialize the LEDs to be OFF
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
digitalWrite(led6, LOW);
digitalWrite(led7, LOW);
digitalWrite(led8, LOW);
}
// This routine loops forever
void loop()
{
delay(100);
// Nothing to do here
}
// This function gets called whenever there is a matching API request
// the command string format is l<led number>,<state>
// for example: l1,HIGH or l1,LOW
// l2,HIGH or l2,LOW
int ledControl(String command)
{
int state = 0;
//find out the pin number and convert the ascii to integer
int pinNumber = command.charAt(1) - '0';
//Sanity check to see if the pin numbers are within limits
if (pinNumber < 0 || pinNumber > 7) return -1;
// find out the state of the led
if(command.substring(3,7) == "HIGH") state = 1;
else if(command.substring(3,6) == "LOW") state = 0;
else return -1;
// write to the appropriate pin
digitalWrite(pinNumber, state);
return 1;
}
int SetDay(String command)
{
int i=0;
//find out the day number and convert the ascii to integer
int dayNumber = command.charAt(0) - '0';
//Sanity check to see if the pin numbers are within limits
if (dayNumber < 0 || dayNumber > 8) return -1;
if (dayNumber==0) {
for (i=0; i<8; i++){
digitalWrite(i, 0);
}
}
else {
for (i=0; i<8; i++){
digitalWrite(i, (i < dayNumber));
}
}
return 1;
}
Please log in or sign up to comment.