Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.98 KB | None | 0 0
  1. #include <Adafruit_NeoPixel.h>
  2. #ifdef __AVR__
  3. #include <avr/power.h>
  4. #endif
  5.  
  6. #define PIN 6
  7.  
  8. #define trueNum 64
  9.  
  10. #define NUM_PIXELS trueNum
  11. #define NUM_LEDS trueNum
  12.  
  13. #define BRIGHTNESS 100
  14.  
  15. int timer = 0;
  16. //int ratio = 3;
  17. //double sR = 7.5;
  18.  
  19. int sVal = 2;//2
  20. Adafruit_NeoPixel strip = Adafruit_NeoPixel(trueNum, PIN, NEO_RGB + NEO_KHZ800);
  21.  
  22. /*int gamma[] = {
  23. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  24. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
  25. 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2,
  26. 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5,
  27. 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10,
  28. 10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16,
  29. 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25,
  30. 25, 26, 27, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 35, 36,
  31. 37, 38, 39, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 50,
  32. 51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68,
  33. 69, 70, 72, 73, 74, 75, 77, 78, 79, 81, 82, 83, 85, 86, 87, 89,
  34. 90, 92, 93, 95, 96, 98, 99,101,102,104,105,107,109,110,112,114,
  35. 115,117,119,120,122,124,126,127,129,131,133,135,137,138,140,142,
  36. 144,146,148,150,152,154,156,158,160,162,164,167,169,171,173,175,
  37. 177,180,182,184,186,189,191,193,196,198,200,203,205,208,210,213,
  38. 215,218,220,223,225,228,231,233,236,239,241,244,247,249,252,255 };
  39. */
  40. /********************************************************************************************************************************************/
  41. /********************************************************************************************************************************************/
  42. /********************************************************************************************************************************************/
  43. void setup() {
  44. Serial.begin(115200);
  45. pinMode(13,INPUT);
  46. pinMode(12,OUTPUT);
  47. digitalWrite(12,HIGH);
  48. strip.setBrightness(BRIGHTNESS);
  49. strip.begin();
  50. strip.show(); // Initialize all LEDS to 'off'
  51. }
  52. /********************************************************************************************************************************************/
  53. /********************************************************************************************************************************************/
  54. /********************************************************************************************************************************************/
  55. void loop() {
  56. select();
  57. }
  58.  
  59. /********************************************************************************************************************************************/
  60. /********************************************************************************************************************************************/
  61. /********************************************************************************************************************************************/
  62. /*void off(){
  63. Serial.println("Off");
  64.  
  65. for (int led = 0; led < trueNum; led++) {
  66. setHue(255, led);
  67. }
  68. strip.show();
  69. select();
  70. }
  71. /********************************************************************************************************************************************/
  72. /********************************************************************************************************************************************/
  73. /********************************************************************************************************************************************/
  74. void sparkleRain(){
  75. int fast = 1;
  76. int vals[NUM_LEDS/fast];
  77. double ratio = 255.0/(NUM_LEDS/fast);
  78. for(int i = 0; i<NUM_LEDS/fast;i++){
  79. vals[i] = int(i*ratio);}
  80. int index = 0;
  81. int adjuster = 1;
  82. int val = 0;
  83.  
  84. while(1) {
  85. if(timer >= 500){
  86. timer = 0;
  87. break;
  88. }
  89. else{
  90.  
  91. timer++;
  92. Serial.println(timer);
  93. }
  94.  
  95. index++;
  96. if(index>=NUM_LEDS/fast)index=00;
  97. for(int i = 0;i<NUM_LEDS/fast;i++){
  98. int tmp = i+index;
  99. if(tmp>=NUM_LEDS/fast)tmp-=NUM_LEDS/fast;
  100. setHue(int(vals[tmp]), i);
  101. //setHue(int(vals[tmp]), i+NUM_LEDS/fast);
  102. //setHue(int(vals[tmp]), i+NUM_LEDS/2);
  103. //setHue(int(vals[tmp]), i+NUM_LEDS/2+NUM_LEDS/fast);
  104. //if(int(random(0,100))==0)strip.setPixelColor(i,strip.Color(255,255,255));
  105. }
  106. strip.show();
  107. delay(getSpeed());
  108. }
  109. select();
  110. }
  111. /********************************************************************************************************************************************/
  112. /********************************************************************************************************************************************/
  113. /********************************************************************************************************************************************/
  114. void solidRain(){
  115. Serial.println(F("SOLID RAIN"));
  116. //Serial.println(NUM_LEDS);
  117. //int vals[255];
  118. //for(int i = 0; i<255;i++){
  119. // vals[i] = int(i);}
  120. int index = 0;
  121. while(1) {
  122. if(timer >= 500){
  123. timer = 0;
  124. break;
  125. }
  126. else{
  127.  
  128. timer++;
  129. Serial.println(timer);
  130. }
  131. index+=2;
  132. if(index>=255)index=00;
  133. for(int i = 0;i<trueNum;i+=1){
  134. setHue(index, i);
  135. }
  136. strip.show();
  137. delay(getSpeed());
  138. }
  139. select();
  140. }
  141. /********************************************************************************************************************************************/
  142. /********************************************************************************************************************************************/
  143. /********************************************************************************************************************************************/
  144. /*void colorWipe() {
  145. while(1){
  146. if(timer >= 500){
  147. timer = 0;
  148. break;
  149. }
  150. else{
  151.  
  152. timer++;
  153. Serial.println(timer);
  154. }
  155. int color = int(random(0,255));
  156. for(uint16_t i=0; i<strip.numPixels(); i++) {
  157. if(timer >= 500){
  158. timer = 0;
  159. break;
  160. }
  161. else{
  162.  
  163. timer++;
  164. Serial.println(timer);
  165. }
  166. setHue(color,i);
  167. strip.show();
  168. delay(10);
  169. }
  170. select();
  171. }}
  172. /********************************************************************************************************************************************/
  173. /********************************************************************************************************************************************/
  174. /********************************************************************************************************************************************/
  175. void colorWipeReturn(){
  176. while(1){
  177. if(timer >= 750){
  178. timer = 0;
  179. break;
  180. }
  181. int color = int(random(0,255));
  182.  
  183. for(uint16_t i=0; i<strip.numPixels(); i++) {
  184. timer++;
  185. setHue(color,i);
  186. strip.show();
  187. delay(getSpeed()-10);
  188. }
  189. color = int(random(0,255));
  190. for(uint16_t i=strip.numPixels()-1; i>0; i--) {
  191. timer++;
  192. setHue(color,i);
  193. strip.show();
  194. delay(getSpeed()-10);
  195. }}
  196. select();
  197. }
  198. /********************************************************************************************************************************************/
  199. /********************************************************************************************************************************************/
  200. /********************************************************************************************************************************************/
  201. void changer(){
  202. while(1){
  203. if(timer >= 200){
  204. timer = 0;
  205. break;
  206. }
  207. else{
  208.  
  209. timer++;
  210. Serial.println(timer);
  211. }
  212. int color = int(random(0,255));
  213. for(uint16_t i=0; i<strip.numPixels(); i++) {
  214. setHue(color,i);
  215. }
  216. strip.show();
  217. delay(450);
  218.  
  219. }
  220. select();
  221. }
  222. /********************************************************************************************************************************************/
  223. /********************************************************************************************************************************************/
  224. /********************************************************************************************************************************************/
  225. /*void strobe(){
  226. for(uint16_t i=0; i<strip.numPixels(); i++) {
  227. if(i%3 == 0)
  228. strip.setPixelColor(i,strip.Color(255,255,255));
  229. else
  230. strip.setPixelColor(i,strip.Color(0,0,0));
  231. }
  232. strip.show();
  233. delay(getSpeed()*2);
  234. for(uint16_t i=strip.numPixels()-1; i>0; i=i) {
  235. //Serial.println(i);
  236. i--;
  237. strip.setPixelColor(i,strip.Color(0,0,0));
  238. }
  239. strip.show();
  240. delay(getSpeed());
  241.  
  242. select();
  243. }
  244. /********************************************************************************************************************************************/
  245. /********************************************************************************************************************************************/
  246. /********************************************************************************************************************************************/
  247. /*void light(){
  248. for(uint16_t i=0; i<strip.numPixels(); i++) {
  249. if(i%3 == 0)
  250. strip.setPixelColor(i,strip.Color(255,255,255));
  251. else
  252. strip.setPixelColor(i,strip.Color(0,0,0));
  253. }
  254. strip.show();
  255. delay(getSpeed());
  256.  
  257. select();
  258. }
  259. /********************************************************************************************************************************************/
  260. /********************************************************************************************************************************************/
  261. /********************************************************************************************************************************************/
  262. /*void whiteOverRainbow() {
  263. uint8_t whiteLength =3;
  264. uint8_t whiteSpeed = 20;
  265. if(whiteLength >= strip.numPixels()) whiteLength = strip.numPixels() - 1;
  266.  
  267. int head = whiteLength - 1;
  268. int tail = 0;
  269.  
  270. int loops = 99999;
  271. int loopNum = 0;
  272.  
  273. static unsigned long lastTime = 0;
  274.  
  275. int ex = 0;
  276. while(true){
  277. int sel = digitalRead(13);
  278. if(sel == 1){
  279. ex = 1;
  280. break;
  281. }
  282. for(int j=0; j<256; j++) {
  283. for(uint16_t i=0; i<strip.numPixels(); i++) {
  284. if((i >= tail && i <= head) || (tail > head && i >= tail) || (tail > head && i <= head) ){
  285. strip.setPixelColor(i, strip.Color(255,255,255, 255 ) );
  286. }
  287. else{
  288. setHue((((i * 256 / strip.numPixels()) + j) & 255),i);
  289. }
  290.  
  291. }
  292.  
  293. if(millis() - lastTime > whiteSpeed) {
  294. head++;
  295. tail++;
  296. if(head == strip.numPixels()){
  297. loopNum++;
  298. }
  299. lastTime = millis();
  300. }
  301.  
  302. if(loopNum == loops) return;
  303.  
  304. head%=strip.numPixels();
  305. tail%=strip.numPixels();
  306. strip.show();
  307. delay(getSpeed());
  308. }
  309. }
  310. select();
  311.  
  312. }
  313. /********************************************************************************************************************************************/
  314. /********************************************************************************************************************************************/
  315. /********************************************************************************************************************************************/
  316. void disco(){
  317. while(1){
  318. if(timer >= 200){
  319. timer = 0;
  320. break;
  321. }
  322. else{
  323.  
  324. timer++;
  325. Serial.println(timer);
  326. }
  327. int color = int(random(0,255));
  328. for(uint16_t i=0; i<strip.numPixels(); i++) {
  329. if(i%10 == 0)
  330. color = int(random(0,255));
  331. setHue(color,i);
  332. }
  333. strip.show();
  334. delay(450);
  335. }
  336. select();
  337. }
  338.  
  339.  
  340.  
  341. //FOR CHRISTMAS LIGHTS: R:255, lower g and b values, mostly b to get the gold color
  342.  
  343.  
  344.  
  345. /********************************************************************************************************************************************/
  346. /********************************************************************************************************************************************/
  347. /********************************************************************************************************************************************/
  348. void select(){
  349. sVal++;
  350. if(sVal > 4)
  351. sVal = 0;
  352. Serial.println(sVal);
  353. switch(sVal){
  354. case 0:
  355. disco();
  356. break;
  357. case 1:
  358. sparkleRain();
  359. break;
  360. case 2:
  361. solidRain();
  362. break;
  363. case 3:
  364. colorWipeReturn();
  365. break;
  366. case 4:
  367. changer();
  368. break;
  369. //default:
  370. // off();
  371. }
  372. }
  373. /********************************************************************************************************************************************/
  374. /********************************************************************************************************************************************/
  375. /********************************************************************************************************************************************/
  376. int getSpeed(){
  377. /*int spd = int(1000*(double(analogRead(0))/1024.0));
  378. if(spd <0)
  379. spd *=-1;
  380. //Serial.println(spd);
  381. //int spd = 100;
  382. */
  383. int spd = 50;
  384. return spd;
  385. }
  386. /********************************************************************************************************************************************/
  387. /********************************************************************************************************************************************/
  388. /********************************************************************************************************************************************/
  389.  
  390. /********************************************************************************************************************************************/
  391. /********************************************************************************************************************************************/
  392. /********************************************************************************************************************************************/
  393. void setHue(int h, uint16_t n) {
  394. //this is the algorithm to convert from RGB to HSV
  395. double r=0;
  396. double g=0;
  397. double b=0;
  398.  
  399. double hf=h/42.5; // Not /60 as range is _not_ 0-360
  400.  
  401. int i=(int)floor(h/42.5);
  402. double f = h/42.5 - i;
  403. double qv = 1 - f;
  404. double tv = f;
  405.  
  406. switch (i)
  407. {
  408. case 0:
  409. r = 1;
  410. g = tv;
  411. break;
  412. case 1:
  413. r = qv;
  414. g = 1;
  415. break;
  416. case 2:
  417. g = 1;
  418. b = tv;
  419. break;
  420. case 3:
  421. g = qv;
  422. b = 1;
  423. break;
  424. case 4:
  425. r = tv;
  426. b = 1;
  427. break;
  428. case 5:
  429. r = 1;
  430. b = qv;
  431. break;
  432. }
  433.  
  434. int red = constrain((int)255*r,0,255);
  435. int green = constrain((int)255*g,0,255);
  436. int blue = constrain((int)255*b,0,255);
  437. strip.setPixelColor(n,strip.Color(red,green,blue));
  438. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement