Guest User

Untitled

a guest
Mar 24th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.14 KB | None | 0 0
  1. #include <Adafruit_NeoPixel.h>
  2. #ifdef __AVR__
  3. #include <avr/power.h>
  4. #endif
  5.  
  6. #define PIXELS 20
  7. #define PIN 6
  8. #define IN_0 3
  9. #define IN_1 4
  10.  
  11.  
  12. #define STATE_RAINBOW 0
  13. #define STATE_HYBRID 1
  14. #define STATE_WHITE 2
  15. #define STATE_CYCLE 3
  16.  
  17. #define TRANSITION_DELAY 15
  18. #define RAINBOW_DELAY 20
  19.  
  20. #define WHITE_BRIGHTNESS 200
  21. #define HYBRID_BRIGHTNESS 120
  22.  
  23. #define HYBRID_RAINBOW_FIRST 0
  24. #define HYBRID_RAINBOW_LAST 11
  25.  
  26. #define CYCLE_MAX 1000000
  27.  
  28. // Parameter 1 = number of pixels in strip
  29. // Parameter 2 = Arduino pin number (most are valid)
  30. // Parameter 3 = pixel type flags, add together as needed:
  31. // NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
  32. // NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
  33. // NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
  34. // NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
  35. // NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
  36. Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXELS, PIN, NEO_GRB + NEO_KHZ800);
  37. uint8_t current_state = -1;
  38. uint8_t rainbow_state = 0;
  39. uint32_t cycle_state = 0;
  40. uint8_t hybrid_state = 0;
  41.  
  42. // IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
  43. // pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
  44. // and minimize distance between Arduino and first pixel. Avoid connecting
  45. // on a live circuit...if you must, connect GND first.
  46.  
  47. uint8_t readState(){
  48. uint8_t state = 0;
  49. if(digitalRead(IN_0) == HIGH){
  50. state |= 1;
  51. }
  52. if(digitalRead(IN_1) == HIGH){
  53. state |= 2;
  54. }
  55. return state;
  56. }
  57.  
  58. bool stateCheck(){
  59. uint8_t last_state = current_state;
  60. current_state = readState();
  61. return last_state != current_state;
  62. }
  63.  
  64. void setup() {
  65. pinMode(IN_0, INPUT_PULLUP);
  66. pinMode(IN_1, INPUT_PULLUP);
  67. strip.begin();
  68. strip.show(); // Initialize all pixels to 'off'
  69.  
  70. initRandom();
  71. rainbow_state = random(256);
  72. hybrid_state = random(256);
  73. cycle_state = random(CYCLE_MAX);
  74.  
  75. stateCheck();
  76. switchState();
  77. }
  78.  
  79.  
  80. void initRandom(){
  81. long a = analogRead(3) & 255;
  82. delay(10);
  83. long b = analogRead(3) & 255;
  84. delay(10);
  85. long c = analogRead(3) & 255;
  86. delay(10);
  87. long d = analogRead(3) & 255;
  88.  
  89. long seed = a | (b << 8) | (c << 16) | (d << 24);
  90. randomSeed(seed);
  91. }
  92.  
  93. void switchState(){
  94. switch(current_state){
  95. case STATE_RAINBOW:{
  96. switchToRainbow();
  97. return;
  98. }
  99. case STATE_HYBRID:{
  100. switchToHybrid();
  101. return;
  102. }
  103. case STATE_WHITE:{
  104. showColor(strip.Color(WHITE_BRIGHTNESS, WHITE_BRIGHTNESS, WHITE_BRIGHTNESS), TRANSITION_DELAY);
  105. return;
  106. }
  107. case STATE_CYCLE:{
  108. switchToCycle();
  109. return;
  110. }
  111. }
  112. }
  113.  
  114. void switchToRainbow(){
  115. for(uint16_t i=0; i< strip.numPixels(); i++) {
  116. strip.setPixelColor(i, getRainbowColor(i));
  117. strip.show();
  118. delay(TRANSITION_DELAY);
  119. }
  120. }
  121.  
  122. void switchToHybrid(){
  123. for(uint16_t i=0; i< strip.numPixels(); i++) {
  124. strip.setPixelColor(i, getHybridColor(i));
  125. strip.show();
  126. delay(TRANSITION_DELAY);
  127. }
  128. }
  129.  
  130. void switchToCycle(){
  131. for(uint16_t i=0; i< strip.numPixels(); i++) {
  132. strip.setPixelColor(i, PreciseWheel(cycle_state));
  133. strip.show();
  134. delay(TRANSITION_DELAY);
  135. }
  136. }
  137.  
  138. void doStep(){
  139. if(stateCheck()){
  140. switchState();
  141. return;
  142. }
  143. switch(current_state){
  144. case STATE_WHITE:{
  145. delay(200);
  146. return;
  147. }
  148. case STATE_RAINBOW:{
  149. rainbowCycleStep();
  150. delay(RAINBOW_DELAY);
  151. return;
  152. }
  153. case STATE_HYBRID:{
  154. hybridCycleStep();
  155. delay(RAINBOW_DELAY);
  156. return;
  157. }
  158. case STATE_CYCLE:{
  159. colorCycleStep();
  160. return;
  161. }
  162. }
  163. }
  164.  
  165. void loop() {
  166. doStep();
  167. }
  168.  
  169. // Fill the dots one after the other with a color
  170. void showColor(uint32_t c, uint8_t wait) {
  171. for(uint16_t i=0; i<strip.numPixels(); i++) {
  172. strip.setPixelColor(i, c);
  173. strip.show();
  174. delay(wait);
  175. }
  176. }
  177.  
  178. void rainbowCycleStep() {
  179. for(uint16_t i=0; i< strip.numPixels(); i++) {
  180. strip.setPixelColor(i, getRainbowColor(i));
  181. }
  182. strip.show();
  183. rainbow_state++;
  184. }
  185.  
  186. void hybridCycleStep() {
  187. for(uint16_t i=0; i< strip.numPixels(); i++) {
  188. strip.setPixelColor(i, getHybridColor(i));
  189. }
  190. strip.show();
  191. hybrid_state++;
  192. }
  193.  
  194. void colorCycleStep() {
  195. uint32_t color = PreciseWheel(cycle_state);
  196. for(uint16_t i=0; i< strip.numPixels(); i++) {
  197. strip.setPixelColor(i, color);
  198. }
  199. strip.show();
  200. cycle_state++;
  201. if(cycle_state == CYCLE_MAX){
  202. cycle_state = 0;
  203. }
  204. }
  205.  
  206. uint32_t getRainbowColor(uint16_t i){
  207. return Wheel(((i * 256 / strip.numPixels()) + rainbow_state) & 255);
  208. }
  209.  
  210. uint32_t getHybridColor(uint16_t i){
  211. if(i >= HYBRID_RAINBOW_FIRST && i <= HYBRID_RAINBOW_LAST){
  212. return Wheel(((i * 256 / (HYBRID_RAINBOW_LAST - HYBRID_RAINBOW_FIRST + 1)) + hybrid_state) & 255);
  213. }
  214. else{
  215. return strip.Color(HYBRID_BRIGHTNESS, HYBRID_BRIGHTNESS, HYBRID_BRIGHTNESS);
  216. }
  217. }
  218.  
  219. // Input a value 0 to 255 to get a color value.
  220. // The colours are a transition r - g - b - back to r.
  221. uint32_t Wheel(byte WheelPos) {
  222. WheelPos = 255 - WheelPos;
  223. if(WheelPos < 85) {
  224. return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  225. }
  226. if(WheelPos < 170) {
  227. WheelPos -= 85;
  228. return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  229. }
  230. WheelPos -= 170;
  231. return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  232. }
  233.  
  234. uint32_t PreciseWheel(uint32_t pos){
  235. float H = pos / (float)CYCLE_MAX;
  236. uint8_t var_r = 0;
  237. uint8_t var_g = 0;
  238. uint8_t var_b = 0;
  239.  
  240. float var_h = H * 6;
  241. //if ( var_h >= 6 || var_h < 0 ) var_h = 0; //H must be < 1
  242. int var_i = int( var_h ); //Or ... var_i = floor( var_h )
  243. uint8_t var_2 = ( 1 - 1 * ( var_h - var_i ) ) * 255;
  244. uint8_t var_3 = ( 1 - 1 * ( 1 - ( var_h - var_i ) ) ) * 255;
  245.  
  246. if ( var_i == 0 ) { var_r = 255 ; var_g = var_3 ; var_b = 0 ; }
  247. else if ( var_i == 1 ) { var_r = var_2 ; var_g = 255 ; var_b = 0 ; }
  248. else if ( var_i == 2 ) { var_r = 0 ; var_g = 255 ; var_b = var_3; }
  249. else if ( var_i == 3 ) { var_r = 0 ; var_g = var_2 ; var_b = 255 ; }
  250. else if ( var_i == 4 ) { var_r = var_3 ; var_g = 0 ; var_b = 255 ; }
  251. else { var_r = 255 ; var_g = 0 ; var_b = var_2; }
  252.  
  253. return strip.Color(var_r, var_g, var_b);
  254. }
Add Comment
Please, Sign In to add comment