Guest User

jellifish spark core code

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