Advertisement
Guest User

myled.ino v2

a guest
Jan 5th, 2015
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. // This #include statement was automatically added by the Spark IDE.
  2. #include "SparkIntervalTimer/SparkIntervalTimer.h"
  3. #include "LPD6803.h"
  4.  
  5. int dataPin = D3; // 'yellow' wire (Arduino 4) [WAS D3]
  6. int clockPin = D4; // 'white' wire (Arduino 7) [WAS D4]
  7.  
  8. // Don't forget to connect 'blue' to ground and 'red' to +5V
  9.  
  10. // Set the first variable to the NUMBER of pixels. 20 = 20 pixels in a row
  11. LPD6803 strip = LPD6803(50, dataPin, clockPin);
  12.  
  13.  
  14. int numberofracks=8;
  15. int setflag=0;
  16. int initflag=0;
  17.  
  18. // Set first element to the number of racks
  19. uint8_t rackdata[8][3]= {
  20. {0,0,0}, //
  21. {0,0,0}, //
  22. {0,0,0}, //
  23. {0,0,0}, //
  24. {0,0,0}, //
  25. {0,0,0}, //
  26. {0,0,0}, //
  27. {0,0,0}, //
  28. };
  29.  
  30. void setup() {
  31.  
  32. Spark.function("ledSet", ledSet); // expects 'd' separated integers (racknum,red,green,blue)
  33.  
  34. // curl https://api.spark.io/v1/devices/jjcore1/ledset -d access_token=$TOKENID -d "c=4-32-32-32-"
  35.  
  36. // API Works with next line commented out (but lights don't function)
  37. // initflag=1;
  38.  
  39. }
  40.  
  41.  
  42. void loop() {
  43.  
  44. if (initflag ==1) {
  45. strip.begin();
  46. strip.show();
  47. delay(50);
  48. colorWipe(Color(0,0,0),50);
  49. delay(50);
  50. initflag=0;
  51. }
  52.  
  53. if (setflag == 1) {
  54. setall();
  55. setflag=0;
  56. delay(50);
  57. }
  58.  
  59. /*
  60. int r, f;
  61.  
  62. for (r=1; r<9; r++) {
  63. for (f=0; f<6; f++) {
  64. rackWipe(Color(0, 0, 63), 50, r); // Make rack r red
  65. delay(50);
  66. rackWipe(Color(15, 0, 0), 50, r); // Make rack r blue
  67. delay(50);
  68. }
  69. }
  70.  
  71. */
  72.  
  73. }
  74.  
  75. void setall() {
  76.  
  77. uint8_t rc;
  78. for (rc=0; rc<numberofracks; rc++) {
  79. rackWipe(Color(rackdata[rc][2], rackdata[rc][1], rackdata[rc][0]), 50, rc+1);
  80. delay (50);
  81. }
  82.  
  83. }
  84.  
  85. // fill the dots one after the other with said color
  86. // good for testing purposes
  87. void colorWipe(uint16_t c, uint8_t wait) {
  88. int i;
  89.  
  90. for (i=0; i < strip.numPixels(); i++) {
  91. strip.setPixelColor(i, c);
  92. strip.show();
  93. delay(wait);
  94. }
  95. }
  96.  
  97. // fill the dots one after the other with said color
  98. // good for testing purposes
  99. void rackWipe(uint16_t c, uint8_t wait, uint8_t rack) {
  100. int i;
  101.  
  102. for (i=(rack*6)-6; i < (rack*6); i++) {
  103. strip.setPixelColor(i, c);
  104. strip.show();
  105. delay(wait);
  106. }
  107. }
  108.  
  109.  
  110. int ledSet(String c) {
  111.  
  112.  
  113. int racknum;
  114. int red;
  115. int green;
  116. int blue;
  117.  
  118. int loc1=0;
  119. int loc2=0;
  120. int loc3=0;
  121. int loc4=0;
  122.  
  123. if (setflag==1) {
  124. return -1;
  125. }
  126.  
  127. loc1 = c.indexOf("-");
  128. racknum = c.substring(0,loc1).toInt();
  129.  
  130.  
  131. loc2 = c.indexOf("-",loc1+1);
  132. red = c.substring(loc1+1,loc2).toInt();
  133.  
  134. loc3 = c.indexOf("-",loc2+1);
  135. green = c.substring(loc2+1,loc3).toInt();
  136.  
  137. loc4 = c.indexOf("-",loc3+1);
  138. blue = c.substring(loc3+1,loc4).toInt();
  139.  
  140.  
  141. rackdata[racknum-1][0]=red;
  142. rackdata[racknum-1][1]=green;
  143. rackdata[racknum-1][2]=blue;
  144.  
  145. setflag=1;
  146.  
  147. return (racknum-1);
  148.  
  149. }
  150.  
  151. /* Helper functions */
  152.  
  153. // Create a 15 bit color value from R,G,B
  154. unsigned int Color(byte r, byte g, byte b)
  155. {
  156. //Take the lowest 5 bits of each value and append them end to end
  157. return( ((unsigned int)g & 0x1F )<<10 | ((unsigned int)b & 0x1F)<<5 | (unsigned int)r & 0x1F);
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement