Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Forked codes:
- Adafruit Arduino - Lesson 3. RGB LED
- Internet connected LED - Week 3 lecture
- Edited by: Christian Veloso
- Weather Cloud
- */
- // Define the RGB LED pins we're going to use
- int redPin = D0;
- int greenPin = D1;
- int bluePin = D2;
- //uncomment this line if using a Common Anode LED
- //#define COMMON_ANODE
- // This routine runs only once upon reset
- void setup()
- {
- //Register a Spark function. We will call this function to control the RGB LED
- Spark.function("ledSunny", sunny);
- Spark.function("ledRain", rain);
- Spark.function("ledCloudy", cloudy);
- //initialize the LED pin as an output.
- pinMode(redPin, OUTPUT);
- pinMode(greenPin, OUTPUT);
- pinMode(bluePin, OUTPUT);
- // set the LED to be OFF
- digitalWrite(redPin, LOW);
- digitalWrite(greenPin, LOW);
- digitalWrite(bluePin, LOW);
- }
- void loop()
- {
- // Nothing to do here
- }
- // This function gets called whenever there is a matching API request
- // the command string format is l<pin number>,<state>
- // for example: 0,HIGH or 0,LOW
- // 5,HIGH or 5,LOW
- int sunny(String command)
- {
- int state = 0;
- //find out the pin number and convert the ascii to integer
- int pinNumber = command.charAt(0) - '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(2,6) == "HIGH") state = 1;
- else if(command.substring(2,5) == "LOW") state = 0;
- else return -1;
- // write to the appropriate pin
- digitalWrite(pinNumber, state);
- setColor(255, 191, 0); // sunburst orange
- return 1;
- }
- // This function gets called whenever there is a matching API request
- // the command string format is l<pin number>,<state>
- // for example: 0,HIGH or 0,LOW
- // 5,HIGH or 5,LOW
- int rain(String command)
- {
- int state = 0;
- //find out the pin number and convert the ascii to integer
- int pinNumber = command.charAt(0) - '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(2,6) == "HIGH") state = 1;
- else if(command.substring(2,5) == "LOW") state = 0;
- else return -1;
- // write to the appropriate pin
- digitalWrite(pinNumber, state);
- setColor(8, 8, 138); // dark blue
- return 1;
- }
- // This function gets called whenever there is a matching API request
- // the command string format is l<pin number>,<state>
- // for example: 0,HIGH or 0,LOW
- // 5,HIGH or 5,LOW
- int cloudy(String command)
- {
- int state = 0;
- //find out the pin number and convert the ascii to integer
- int pinNumber = command.charAt(0) - '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(2,6) == "HIGH") state = 1;
- else if(command.substring(2,5) == "LOW") state = 0;
- else return -1;
- // write to the appropriate pin
- digitalWrite(pinNumber, state);
- setColor(255, 255, 255); // white
- return 1;
- }
- void setColor(int red, int green, int blue)
- {
- #ifdef COMMON_ANODE
- red = 255 - red;
- green = 255 - green;
- blue = 255 - blue;
- #endif
- analogWrite(redPin, red);
- analogWrite(greenPin, green);
- analogWrite(bluePin, blue);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement