Guest User

Untitled

a guest
Dec 16th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.64 KB | None | 0 0
  1. /*
  2. White elephant gift for Degeneratesmas 2017
  3. By: Andrew DalPino
  4. */
  5.  
  6. #include <Metro.h>
  7. #include <Adafruit_NeoPixel.h>
  8. #include <Bounce2.h>
  9.  
  10. #define PIXEL_PIN 3
  11. #define BUTTON_PIN 4
  12. #define NUM_PIXELS 8
  13. #define BRIGHTNESS 50
  14. #define MAX_LUMINANCE 255
  15. #define DEBOUNCE_INTERVAL 50
  16.  
  17. int mode = 0;
  18. int button_state = LOW;
  19. int cube_positions[4] = {0, 1, 2, 3};
  20. int cube_colors[4][3] = {{255, 0, 0}, {0, 255, 0}, {0, 0, 255}, {255, 191, 0}};
  21. int strobe_color[3] = {255, 255, 255};
  22. int sort_positions[8] = {0, 1, 2, 3, 4, 5, 6, 7};
  23. int sort_colors[8][3] = {{255, 0, 0}, {255, 128, 0}, {255, 255, 0}, {0, 255, 0}, {0, 255, 255}, {0, 0, 255}, {128, 0, 255}, {255, 0, 128}};
  24. int binary_counter = 0;
  25. int binary_colors[2][3] = {{50, 0, 0}, {64, 0, 255}};
  26. int pulse_position = 0;
  27. int pulse_color[3] = {255, 0, 64};
  28. bool pulse_reverse = false;
  29.  
  30. Metro metro = Metro();
  31. Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUM_PIXELS, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
  32. Bounce debouncer = Bounce();
  33.  
  34. void setup() {
  35. Serial.begin(9600);
  36. Serial.println("Merry Christmas mother fuckers. -Andrew");
  37.  
  38. debouncer.attach(BUTTON_PIN);
  39. debouncer.interval(DEBOUNCE_INTERVAL);
  40.  
  41. pixels.setBrightness(BRIGHTNESS);
  42. pixels.begin();
  43. }
  44.  
  45. void loop() {
  46. switch (mode) {
  47. case 0:
  48. blackout();
  49. break;
  50. case 1:
  51. acid();
  52. break;
  53. case 2:
  54. binary_count();
  55. break;
  56. case 3:
  57. rubix();
  58. break;
  59. case 4:
  60. pulse();
  61. break;
  62. case 5:
  63. bubble_sort();
  64. break;
  65. case 6:
  66. strobe();
  67. break;
  68. }
  69.  
  70. pushMyButtons();
  71. }
  72.  
  73. void acid() {
  74. for (int i = 0; i < NUM_PIXELS; i++) {
  75. int r = adjustLuminance(random(0, 255));
  76. int g = adjustLuminance(random(0, 255));
  77. int b = adjustLuminance(random(0, 255));
  78.  
  79. pixels.setPixelColor(i, pixels.Color(r, g, b));
  80. }
  81.  
  82. pixels.show();
  83.  
  84. metro.interval(100);
  85. metro.reset();
  86.  
  87. while (metro.check() != true) {
  88. pushMyButtons();
  89. }
  90. }
  91.  
  92. void rubix() {
  93. int first = random(0, 4), second;
  94. int swap, count = 0;
  95. int r, g, b;
  96.  
  97. if (first >= 3) {
  98. second = 2;
  99. } else if (first <= 0) {
  100. second = 1;
  101. } else {
  102. if (random(0, 1) == 0) {
  103. second = first - 1;
  104. } else {
  105. second = first + 1;
  106. }
  107. }
  108.  
  109. swap = cube_positions[first];
  110.  
  111. cube_positions[first] = cube_positions[second];
  112. cube_positions[second] = swap;
  113.  
  114. for (int i = 0; i < 4; i++) {
  115. r = adjustLuminance(cube_colors[cube_positions[i]][0]);
  116. g = adjustLuminance(cube_colors[cube_positions[i]][1]);
  117. b = adjustLuminance(cube_colors[cube_positions[i]][2]);
  118.  
  119. for (int j = 0; j < round(NUM_PIXELS / 4); j++) {
  120. pixels.setPixelColor(count, pixels.Color(r, g, b));
  121.  
  122. count++;
  123. }
  124. }
  125.  
  126. pixels.show();
  127.  
  128. metro.interval(500);
  129. metro.reset();
  130.  
  131. while (metro.check() != true) {
  132. pushMyButtons();
  133. }
  134. }
  135.  
  136. void pulse() {
  137. int r, g, b;
  138.  
  139. if (pulse_position > NUM_PIXELS - 1) {
  140. pulse_position = NUM_PIXELS - 1;
  141. pulse_reverse = true;
  142. } else if (pulse_position < 0) {
  143. pulse_position = 0;
  144. pulse_reverse = false;
  145. }
  146.  
  147. if (pulse_reverse != true) {
  148. pulse_position++;
  149. } else {
  150. pulse_position--;
  151. }
  152.  
  153. r = adjustLuminance(pulse_color[0]);
  154. g = adjustLuminance(pulse_color[1]);
  155. b = adjustLuminance(pulse_color[2]);
  156.  
  157. blackout();
  158.  
  159. pixels.setPixelColor(pulse_position, pixels.Color(r, g, b));
  160.  
  161. pixels.show();
  162.  
  163. metro.interval(25);
  164. metro.reset();
  165.  
  166. while (metro.check() != true) {
  167. pushMyButtons();
  168. }
  169. }
  170.  
  171. void bubble_sort() {
  172. int swap;
  173. int r, g, b;
  174.  
  175. shuffle(sort_positions, 8);
  176.  
  177. for (int i = 0; i < NUM_PIXELS; i++) {
  178. r = adjustLuminance(sort_colors[sort_positions[i]][0]);
  179. g = adjustLuminance(sort_colors[sort_positions[i]][1]);
  180. b = adjustLuminance(sort_colors[sort_positions[i]][2]);
  181.  
  182. pixels.setPixelColor(i, pixels.Color(r, g, b));
  183. }
  184.  
  185. pixels.show();
  186.  
  187. metro.interval(1000);
  188. metro.reset();
  189.  
  190. while (metro.check() != true) {
  191. pushMyButtons();
  192. }
  193.  
  194. for (int i = 0; i < NUM_PIXELS - 1; i++) {
  195. for (int j = i + 1; j < NUM_PIXELS; j++) {
  196. if (sort_positions[i] >= sort_positions[j]) {
  197. swap = sort_positions[i];
  198.  
  199. sort_positions[i] = sort_positions[j];
  200. sort_positions[j] = swap;
  201.  
  202. for (int k = 0; k < NUM_PIXELS; k++) {
  203. r = adjustLuminance(sort_colors[sort_positions[k]][0]);
  204. g = adjustLuminance(sort_colors[sort_positions[k]][1]);
  205. b = adjustLuminance(sort_colors[sort_positions[k]][2]);
  206.  
  207. pixels.setPixelColor(k, pixels.Color(r, g, b));
  208. }
  209.  
  210. pixels.show();
  211.  
  212. metro.interval(100);
  213. metro.reset();
  214.  
  215. while (metro.check() != true) {
  216. pushMyButtons();
  217. }
  218. }
  219. }
  220. }
  221.  
  222. metro.interval(1000);
  223. metro.reset();
  224.  
  225. while (metro.check() != true) {
  226. pushMyButtons();
  227. }
  228. }
  229.  
  230. void strobe() {
  231. int r, g, b;
  232.  
  233. for (int i = 0; i < NUM_PIXELS; i++) {
  234. if (random(0, 10) == 5) {
  235. r = adjustLuminance(strobe_color[0]);
  236. g = adjustLuminance(strobe_color[1]);
  237. b = adjustLuminance(strobe_color[2]);
  238. } else {
  239. r = 0; g = 0; b = 0;
  240. }
  241.  
  242. pixels.setPixelColor(i, pixels.Color(r, g, b));
  243. }
  244.  
  245. pixels.show();
  246.  
  247. metro.interval(10);
  248. metro.reset();
  249.  
  250. while (metro.check() != true) {
  251. pushMyButtons();
  252. }
  253. }
  254.  
  255. void binary_count() {
  256. int value;
  257. int r, g, b;
  258.  
  259. for (int i = 0; i < NUM_PIXELS; i++) {
  260. value = bitRead(binary_counter, i);
  261.  
  262. r = adjustLuminance(binary_colors[value][0]);
  263. g = adjustLuminance(binary_colors[value][1]);
  264. b = adjustLuminance(binary_colors[value][2]);
  265.  
  266. pixels.setPixelColor(i, pixels.Color(r, g, b));
  267. }
  268.  
  269. pixels.show();
  270.  
  271. if (binary_counter >= pow(2, NUM_PIXELS) - 1) {
  272. metro.interval(1000);
  273. binary_counter = 0;
  274. } else {
  275. metro.interval(100);
  276. binary_counter++;
  277. }
  278.  
  279. metro.reset();
  280.  
  281. while (metro.check() != true) {
  282. pushMyButtons();
  283. }
  284. }
  285.  
  286. void pushMyButtons() {
  287. debouncer.update();
  288.  
  289. button_state = debouncer.read();
  290.  
  291. if (button_state == HIGH) {
  292. mode++;
  293.  
  294. if (mode > 6) {
  295. mode = 0;
  296. }
  297. }
  298. }
  299.  
  300. void blackout() {
  301. pixels.clear();
  302. }
  303.  
  304. void setAllPixels(int r, int g, int b) {
  305. r = adjustLuminance(r);
  306. g = adjustLuminance(g);
  307. b = adjustLuminance(b);
  308.  
  309. for (int i = 0; i < NUM_PIXELS; i++) {
  310. pixels.setPixelColor(i, pixels.Color(r, g, b));
  311. }
  312.  
  313. pixels.show();
  314. }
  315.  
  316. int adjustLuminance(int value) {
  317. return constrain(value, 0, MAX_LUMINANCE);
  318. }
  319.  
  320. void shuffle(int *array, size_t n)
  321. {
  322. if (n > 1) {
  323. size_t i;
  324.  
  325. for (int i = 0; i < n - 1; i++) {
  326. size_t j = i + rand() / (RAND_MAX / (n - i) + 1);
  327.  
  328. int temp = array[j];
  329.  
  330. array[j] = array[i];
  331. array[i] = temp;
  332. }
  333. }
  334. }
Add Comment
Please, Sign In to add comment