Fabouh62

Stranger Things Lamp Bluetooth

Jan 24th, 2026
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.00 KB | None | 0 0
  1. #include <Adafruit_NeoPixel.h>
  2.  
  3. // ------------------------------------------------------
  4. // CONFIG BANDEAUX
  5. // ------------------------------------------------------
  6. #define PIN_TOP 4
  7. #define LED_TOP 68
  8.  
  9. #define PIN_BOTTOM 5
  10. #define LED_BOTTOM 70
  11.  
  12. // ------------------------------------------------------
  13. // BOUTON
  14. // ------------------------------------------------------
  15. #define BUTTON_PIN 2
  16.  
  17. // ------------------------------------------------------
  18. // MICRO
  19. // ------------------------------------------------------
  20. #define MIC_PIN A0
  21.  
  22. Adafruit_NeoPixel stripTop(LED_TOP, PIN_TOP, NEO_GRB + NEO_KHZ800);
  23. Adafruit_NeoPixel stripBottom(LED_BOTTOM, PIN_BOTTOM, NEO_GRB + NEO_KHZ800);
  24.  
  25. // ------------------------------------------------------
  26. // VARIABLES MODES
  27. // ------------------------------------------------------
  28. int modeActuel = 0;
  29. const int totalModes = 6;
  30.  
  31. bool lastButtonState = HIGH;
  32. unsigned long lastDebounce = 0;
  33. unsigned long debounceDelay = 50;
  34.  
  35. // ------------------------------------------------------
  36. // VARIABLES VARIABLES
  37. // ------------------------------------------------------
  38. unsigned long debutCycle = 0;
  39. unsigned long prochainEvent = 0;
  40. bool flash = false;
  41. unsigned long finFlash = 0;
  42.  
  43. // Variables pour le Scanner (Mode 5)
  44. int scannerPos = 0;
  45. int scannerDir = 1;
  46. unsigned long lastScannerUpdate = 0;
  47.  
  48. // ------------------------------------------------------
  49. // SETUP
  50. // ------------------------------------------------------
  51. void setup() {
  52. stripTop.begin();
  53. stripBottom.begin();
  54. stripTop.show();
  55. stripBottom.show();
  56.  
  57. pinMode(BUTTON_PIN, INPUT_PULLUP);
  58. pinMode(MIC_PIN, INPUT); // Le micro
  59.  
  60. randomSeed(analogRead(1)); // Changé à 1 pour ne pas interférer avec le micro sur A0
  61. debutCycle = millis();
  62. }
  63.  
  64. // ------------------------------------------------------
  65. // LOOP PRINCIPAL
  66. // ------------------------------------------------------
  67. void loop() {
  68. handleButton();
  69. updateTopEffects();
  70. updateBottomAudioReactive(); // Réactivé pour le son
  71. }
  72.  
  73. // ======================================================
  74. // GESTION DU BOUTON
  75. // ======================================================
  76. void handleButton() {
  77. bool reading = digitalRead(BUTTON_PIN);
  78.  
  79. if (reading != lastButtonState) {
  80. lastDebounce = millis();
  81. }
  82.  
  83. if ((millis() - lastDebounce) > debounceDelay) {
  84. if (reading == LOW) {
  85. modeActuel++;
  86. if (modeActuel >= totalModes) {
  87. modeActuel = 0;
  88. }
  89. stripTop.clear();
  90. stripTop.show();
  91. delay(200);
  92. }
  93. }
  94. lastButtonState = reading;
  95. }
  96.  
  97. // ======================================================
  98. // GESTION DES MODES (BANDEAU HAUT)
  99. // ======================================================
  100. void updateTopEffects() {
  101. switch (modeActuel) {
  102. case 0: effetOrage(); break;
  103. case 1: effetBattement(); break;
  104. case 2: effetFixeCalme(); break;
  105. case 3: effetEtincelles(); break;
  106. case 4: effetFeu(); break;
  107. case 5: effetScanner(); break;
  108. }
  109. }
  110.  
  111. // ======================================================
  112. // MODE 0 — ORAGE
  113. // ======================================================
  114. void effetOrage() {
  115. unsigned long t = millis();
  116. unsigned long elapsed = t - debutCycle;
  117.  
  118. if (elapsed >= 7000) {
  119. debutCycle = t;
  120. elapsed = 0;
  121. }
  122.  
  123. if (elapsed < 3000) {
  124. if (flash && t >= finFlash) {
  125. flash = false;
  126. setRougeNormal();
  127. setBlancFixe();
  128. stripTop.show();
  129. prochainEvent = t + random(80, 200);
  130. }
  131.  
  132. if (!flash && t >= prochainEvent) {
  133. flash = true;
  134. finFlash = t + random(40, 120);
  135. setRougeFlash();
  136. setBlancFixe();
  137. stripTop.show();
  138.  
  139. if (random(0, 10) < 4) prochainEvent = t + random(40, 120);
  140. else prochainEvent = finFlash + random(200, 500);
  141. }
  142. return;
  143. }
  144.  
  145. flash = false;
  146. setRougeNormal();
  147. setBlancFixe();
  148. stripTop.show();
  149. }
  150.  
  151. // ======================================================
  152. // MODE 1 — BATTEMENT
  153. // ======================================================
  154. void effetBattement() {
  155. float brightness = (exp(sin(millis() / 2000.0 * PI)) - 0.36787944) * 108.0;
  156.  
  157. for (int i = 0; i < LED_TOP; i++) {
  158. if (i < 12 || i >= 44) {
  159. stripTop.setPixelColor(i, stripTop.Color(brightness, 0, 0));
  160. } else {
  161. stripTop.setPixelColor(i, stripTop.Color(brightness, brightness, brightness));
  162. }
  163. }
  164. stripTop.show();
  165. }
  166.  
  167. // ======================================================
  168. // MODE 2 — FIXE CALME
  169. // ======================================================
  170. void effetFixeCalme() {
  171. setRougeNormal();
  172. setBlancFixe();
  173. stripTop.show();
  174. }
  175.  
  176. // ======================================================
  177. // MODE 3 — ÉTINCELLES
  178. // ======================================================
  179. void effetEtincelles() {
  180. for (int i = 0; i < LED_TOP; i++) {
  181. uint32_t c = stripTop.getPixelColor(i);
  182. uint8_t r = (c >> 16) & 0xFF;
  183. uint8_t g = (c >> 8) & 0xFF;
  184. uint8_t b = c & 0xFF;
  185.  
  186. r = (r * 180) / 255;
  187. g = (g * 180) / 255;
  188. b = (b * 180) / 255;
  189.  
  190. stripTop.setPixelColor(i, r, g, b);
  191. }
  192.  
  193. int index = random(0, LED_TOP);
  194. if (random(0, 2) == 0) stripTop.setPixelColor(index, stripTop.Color(255, 255, 255));
  195. else stripTop.setPixelColor(index, stripTop.Color(255, 0, 0));
  196.  
  197. stripTop.show();
  198. delay(20);
  199. }
  200.  
  201. // ======================================================
  202. // MODE 4 — FEU
  203. // ======================================================
  204. void effetFeu() {
  205. for (int i = 0; i < LED_TOP; i++) {
  206. int flicker = random(0, 120);
  207.  
  208. if (i < 12 || i >= 44) {
  209. int r = 180 - flicker;
  210. if (r < 20) r = 20;
  211. stripTop.setPixelColor(i, r, 0, 0);
  212. }
  213. else {
  214. int w = 80 - (flicker / 2);
  215. if (w < 10) w = 10;
  216. stripTop.setPixelColor(i, w, w, w);
  217. }
  218. }
  219. stripTop.show();
  220. delay(random(30, 80));
  221. }
  222.  
  223. // ======================================================
  224. // MODE 5 — SCANNER K2000
  225. // ======================================================
  226. void effetScanner() {
  227. if (millis() - lastScannerUpdate > 30) {
  228. lastScannerUpdate = millis();
  229.  
  230. for(int i = 0; i < LED_TOP; i++) {
  231. uint32_t c = stripTop.getPixelColor(i);
  232. uint8_t r = (c >> 16) & 0xFF;
  233. uint8_t g = (c >> 8) & 0xFF;
  234. uint8_t b = c & 0xFF;
  235.  
  236. r = (r * 200) / 255;
  237. g = (g * 200) / 255;
  238. b = (b * 200) / 255;
  239.  
  240. stripTop.setPixelColor(i, r, g, b);
  241. }
  242.  
  243. scannerPos += scannerDir;
  244.  
  245. if (scannerPos >= LED_TOP) {
  246. scannerPos = LED_TOP - 2;
  247. scannerDir = -1;
  248. } else if (scannerPos < 0) {
  249. scannerPos = 1;
  250. scannerDir = 1;
  251. }
  252.  
  253. if (scannerPos < 12 || scannerPos >= 44) {
  254. stripTop.setPixelColor(scannerPos, stripTop.Color(255, 0, 0));
  255. } else {
  256. stripTop.setPixelColor(scannerPos, stripTop.Color(255, 255, 255));
  257. }
  258.  
  259. stripTop.show();
  260. }
  261. }
  262.  
  263. // ======================================================
  264. // BANDEAU BAS — RÉACTIF AU SON (Luminosité Rouge Réduite Globalement)
  265. // ======================================================
  266. void updateBottomAudioReactive() {
  267. int sensorValue = analogRead(MIC_PIN);
  268.  
  269. // Recentrage sur 0 (le silence est au milieu à 512 sur Arduino)
  270. int amplitude = abs(sensorValue - 512);
  271.  
  272. // Filtre anti-bruit (si c'est < 30, on considère que c'est du silence)
  273. if (amplitude < 30) amplitude = 0;
  274.  
  275. // --- ROUGE (Base + Son) ---
  276.  
  277. // Mapper l'amplitude à la luminosité rouge.
  278. // La plage de sortie est ajustée pour avoir une très faible luminosité au repos.
  279. int rawBrightness = map(amplitude, 0, 512, 5, 255);
  280.  
  281. // Appliquer une réduction de 15% à la luminosité calculée.
  282. // Pour réduire de 15%, on multiplie par (100 - 15) / 100 = 0.85
  283. int redBrightness = rawBrightness * 0.85;
  284.  
  285. // On s'assure que la luminosité ne dépasse pas 255 et ne descend pas trop bas.
  286. if (redBrightness > 255) redBrightness = 255;
  287. if (redBrightness < 5) redBrightness = 5; // Garder un minimum visible
  288.  
  289. // Pour le moment, on ne modifie que la composante rouge.
  290. // Les composantes verte et bleue restent à 0 pour un rouge pur.
  291. uint32_t color = stripBottom.Color(redBrightness, 0, 0);
  292.  
  293. for (int i = 0; i < LED_BOTTOM; i++) {
  294. stripBottom.setPixelColor(i, color);
  295. }
  296. stripBottom.show();
  297. }
  298. // ======================================================
  299. // FONCTIONS LED — UTILITAIRES
  300. // ======================================================
  301. void setRougeFlash() {
  302. for (int i = 0; i < 12; i++) stripTop.setPixelColor(i, stripTop.Color(255, 0, 0));
  303. for (int i = 44; i < 68; i++) stripTop.setPixelColor(i, stripTop.Color(255, 0, 0));
  304. }
  305.  
  306. void setRougeNormal() {
  307. for (int i = 0; i < 12; i++) stripTop.setPixelColor(i, stripTop.Color(40, 0, 0));
  308. for (int i = 44; i < 68; i++) stripTop.setPixelColor(i, stripTop.Color(40, 0, 0));
  309. }
  310.  
  311. void setBlancFixe() {
  312. for (int i = 12; i < 44; i++) stripTop.setPixelColor(i, stripTop.Color(60, 60, 60));
  313. }
  314.  
Advertisement
Add Comment
Please, Sign In to add comment