Advertisement
S_Robertson

Rocket Sounds for Arduino

Jan 23rd, 2019
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.82 KB | None | 0 0
  1. /* sketch 1
  2. turn on a LED when the button is pressed
  3. turn it off when the button is not pressed (or released)
  4. */
  5. int buttonState = 0;
  6. int pinButton = 2; //the pin where we connect the button
  7. int LED = 13; //the pin we connect the LED
  8.  
  9. #include <Adafruit_NeoPixel.h>
  10.  
  11. // data pin
  12. #define PIN 13
  13. // led count
  14. #define CNT 16
  15.  
  16. // Parameter 1 = number of pixels in strip
  17. // Parameter 2 = pin number (most are valid)
  18. // Parameter 3 = pixel type flags, add together as needed:
  19. //   NEO_RGB     Pixels are wired for RGB bitstream
  20. //   NEO_GRB     Pixels are wired for GRB bitstream
  21. //   NEO_KHZ400  400 KHz bitstream (e.g. FLORA pixels)
  22. //   NEO_KHZ800  800 KHz bitstream (e.g. High Density LED strip)
  23. Adafruit_NeoPixel strip = Adafruit_NeoPixel(CNT, PIN, NEO_GRB + NEO_KHZ800);
  24.  
  25. uint32_t fire_color   = strip.Color (75, 30, 0);
  26. uint32_t off_color    = strip.Color (0, 0, 0);
  27.  
  28. ///
  29. /// Fire simulator
  30. ///
  31. class NeoFire
  32. {
  33.   Adafruit_NeoPixel &strip;
  34.  public:
  35.  
  36.   NeoFire(Adafruit_NeoPixel&);
  37.   void Draw();
  38.   void Clear(bool update_afterwards = false);
  39.   void AddColor(uint8_t position, uint32_t color);
  40.   void SubstractColor(uint8_t position, uint32_t color);
  41.   uint32_t Blend(uint32_t color1, uint32_t color2);
  42.   uint32_t Substract(uint32_t color1, uint32_t color2);
  43. };
  44.  
  45. ///
  46. /// Constructor
  47. ///
  48. NeoFire::NeoFire(Adafruit_NeoPixel& n_strip)
  49. : strip (n_strip)
  50. {
  51. }
  52.  
  53. ///
  54. /// Set all colors
  55. ///
  56. void NeoFire::Draw()
  57. {
  58. Clear();
  59.  
  60. for(int i=0;i<strip.numPixels();i++)
  61.   {
  62.   AddColor(i, fire_color);
  63.   int r = random(70);
  64.   uint32_t diff_color = strip.Color ( r, r/2, r/2);
  65.   SubstractColor(i, diff_color);
  66.   }
  67.  
  68. strip.show();
  69. }
  70.  
  71. ///
  72. /// Set color of LED
  73. ///
  74. void NeoFire::AddColor(uint8_t position, uint32_t color)
  75. {
  76. uint32_t blended_color = Blend(strip.getPixelColor(position), color);
  77. strip.setPixelColor(position, blended_color);
  78. }
  79.  
  80. ///
  81. /// Set color of LED
  82. ///
  83. void NeoFire::SubstractColor(uint8_t position, uint32_t color)
  84. {
  85. uint32_t blended_color = Substract(strip.getPixelColor(position), color);
  86. strip.setPixelColor(position, blended_color);
  87. }
  88.  
  89. ///
  90. /// Color blending
  91. ///
  92. uint32_t NeoFire::Blend(uint32_t color1, uint32_t color2)
  93. {
  94. uint8_t r1,g1,b1;
  95. uint8_t r2,g2,b2;
  96. uint8_t r3,g3,b3;
  97.  
  98. r1 = (uint8_t)(color1 >> 16),
  99. g1 = (uint8_t)(color1 >>  8),
  100. b1 = (uint8_t)(color1 >>  0);
  101.  
  102. r2 = (uint8_t)(color2 >> 16),
  103. g2 = (uint8_t)(color2 >>  8),
  104. b2 = (uint8_t)(color2 >>  0);
  105.  
  106. return strip.Color(constrain(r1+r2, 0, 255), constrain(g1+g2, 0, 255), constrain(b1+b2, 0, 255));
  107. }
  108.  
  109. ///
  110. /// Color blending
  111. ///
  112. uint32_t NeoFire::Substract(uint32_t color1, uint32_t color2)
  113. {
  114. uint8_t r1,g1,b1;
  115. uint8_t r2,g2,b2;
  116. uint8_t r3,g3,b3;
  117. int16_t r,g,b;
  118.  
  119. r1 = (uint8_t)(color1 >> 16),
  120. g1 = (uint8_t)(color1 >>  8),
  121. b1 = (uint8_t)(color1 >>  0);
  122.  
  123. r2 = (uint8_t)(color2 >> 16),
  124. g2 = (uint8_t)(color2 >>  8),
  125. b2 = (uint8_t)(color2 >>  0);
  126.  
  127. r=(int16_t)r1-(int16_t)r2;
  128. g=(int16_t)g1-(int16_t)g2;
  129. b=(int16_t)b1-(int16_t)b2;
  130. if(r<0) r=0;
  131. if(g<0) g=0;
  132. if(b<0) b=0;
  133.  
  134. return strip.Color(r, g, b);
  135. }
  136.  
  137. ///
  138. /// Every LED to black
  139. ///
  140. void NeoFire::Clear(bool display_afterwards)
  141. {
  142. for(uint16_t i=0; i<strip.numPixels(); i++)
  143.   {
  144.   strip.setPixelColor(i, off_color);
  145.   }
  146.   if(display_afterwards)
  147.   {
  148.     strip.show();
  149.   }
  150. }
  151.  
  152. NeoFire fire(strip);
  153.  
  154. ///
  155. /// Setup
  156. ///
  157. void setup()
  158. {
  159. pinMode(pinButton, INPUT_PULLUP); //set the button pin as INPUT
  160. pinMode(LED, OUTPUT); //set the LED pin as OUTPUT
  161. strip.begin();
  162. strip.show(); // Initialize all pixels to 'off'
  163. }
  164.  
  165. ///
  166. /// Main loop
  167. ///
  168. void loop(){
  169.   buttonState = digitalRead(pinButton);
  170.    if(buttonState == 0) { //if is pressed
  171.       digitalWrite(LED, 1); //write 1 or HIGH to led pin
  172.       fire.Draw();
  173.   } else { //if not pressed
  174.      digitalWrite(LED, 0);  //write 0 or low to led pin
  175.      fire.Clear();
  176.   }
  177. delay(random(5,60));
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement