Advertisement
Guest User

Untitled

a guest
May 12th, 2015
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. /* Forked codes:
  2. Adafruit Arduino - Lesson 3. RGB LED
  3. Internet connected LED - Week 3 lecture
  4.  
  5. Edited by: Christian Veloso
  6. Weather Cloud
  7. */
  8.  
  9. // Define the RGB LED pins we're going to use
  10. int redPin = D0;
  11. int greenPin = D1;
  12. int bluePin = D2;
  13.  
  14. //uncomment this line if using a Common Anode LED
  15. //#define COMMON_ANODE
  16.  
  17. // This routine runs only once upon reset
  18. void setup()
  19. {
  20. //Register a Spark function. We will call this function to control the RGB LED
  21. Spark.function("ledSunny", sunny);
  22. Spark.function("ledRain", rain);
  23. Spark.function("ledCloudy", cloudy);
  24.  
  25. //initialize the LED pin as an output.
  26. pinMode(redPin, OUTPUT);
  27. pinMode(greenPin, OUTPUT);
  28. pinMode(bluePin, OUTPUT);
  29.  
  30. // set the LED to be OFF
  31. digitalWrite(redPin, LOW);
  32. digitalWrite(greenPin, LOW);
  33. digitalWrite(bluePin, LOW);
  34. }
  35.  
  36. void loop()
  37. {
  38. // Nothing to do here
  39. }
  40.  
  41. // This function gets called whenever there is a matching API request
  42. // the command string format is l<pin number>,<state>
  43. // for example: 0,HIGH or 0,LOW
  44. // 5,HIGH or 5,LOW
  45.  
  46. int sunny(String command)
  47. {
  48. int state = 0;
  49. //find out the pin number and convert the ascii to integer
  50. int pinNumber = command.charAt(0) - '0';
  51. //Sanity check to see if the pin numbers are within limits
  52. if (pinNumber < 0 || pinNumber > 7) return -1;
  53.  
  54. // find out the state of the led
  55. if(command.substring(2,6) == "HIGH") state = 1;
  56. else if(command.substring(2,5) == "LOW") state = 0;
  57. else return -1;
  58.  
  59. // write to the appropriate pin
  60. digitalWrite(pinNumber, state);
  61. setColor(255, 191, 0); // sunburst orange
  62. return 1;
  63. }
  64.  
  65. // This function gets called whenever there is a matching API request
  66. // the command string format is l<pin number>,<state>
  67. // for example: 0,HIGH or 0,LOW
  68. // 5,HIGH or 5,LOW
  69.  
  70. int rain(String command)
  71. {
  72. int state = 0;
  73. //find out the pin number and convert the ascii to integer
  74. int pinNumber = command.charAt(0) - '0';
  75. //Sanity check to see if the pin numbers are within limits
  76. if (pinNumber < 0 || pinNumber > 7) return -1;
  77.  
  78. // find out the state of the led
  79. if(command.substring(2,6) == "HIGH") state = 1;
  80. else if(command.substring(2,5) == "LOW") state = 0;
  81. else return -1;
  82.  
  83. // write to the appropriate pin
  84. digitalWrite(pinNumber, state);
  85. setColor(8, 8, 138); // dark blue
  86. return 1;
  87. }
  88.  
  89. // This function gets called whenever there is a matching API request
  90. // the command string format is l<pin number>,<state>
  91. // for example: 0,HIGH or 0,LOW
  92. // 5,HIGH or 5,LOW
  93.  
  94. int cloudy(String command)
  95. {
  96. int state = 0;
  97. //find out the pin number and convert the ascii to integer
  98. int pinNumber = command.charAt(0) - '0';
  99. //Sanity check to see if the pin numbers are within limits
  100. if (pinNumber < 0 || pinNumber > 7) return -1;
  101.  
  102. // find out the state of the led
  103. if(command.substring(2,6) == "HIGH") state = 1;
  104. else if(command.substring(2,5) == "LOW") state = 0;
  105. else return -1;
  106.  
  107. // write to the appropriate pin
  108. digitalWrite(pinNumber, state);
  109. setColor(255, 255, 255); // white
  110. return 1;
  111. }
  112.  
  113. void setColor(int red, int green, int blue)
  114. {
  115. #ifdef COMMON_ANODE
  116. red = 255 - red;
  117. green = 255 - green;
  118. blue = 255 - blue;
  119. #endif
  120. analogWrite(redPin, red);
  121. analogWrite(greenPin, green);
  122. analogWrite(bluePin, blue);
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement