Guest User

sd card module debug

a guest
Dec 9th, 2025
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <SD.h>
  3. #include <TMRpcm.h>
  4. #include <LowPower.h>
  5.  
  6. TMRpcm audio;
  7.  
  8. #define SD_CS 4
  9. #define AUDIO_PIN 9
  10. #define TILT_PIN 2
  11.  
  12. const uint8_t totalFiles = 29;
  13.  
  14. // Counters
  15. uint8_t pressCount = 0;
  16. unsigned long lastPress = 0;
  17.  
  18. // Timers
  19. unsigned long lastActivity = 0;
  20. const unsigned long sleepDelay = 10000;
  21. const unsigned long timeoutReset = 10000;
  22.  
  23. // Debounce
  24. uint8_t lastState = 0;
  25. unsigned long lastDebounce = 0;
  26. const unsigned long debounceDelay = 40;
  27.  
  28. bool shakePlaying = false;
  29. bool lockInput = false;
  30.  
  31. void wakeUp() {}
  32.  
  33.  
  34.  
  35. // ----------------------------
  36. // STABILNI SD + SPI RESET
  37. // ----------------------------
  38. void resetSDandAudio() {
  39.  
  40. // Stop audio fully
  41. audio.stopPlayback();
  42. audio.disable();
  43. delay(5);
  44.  
  45. // Reset SPI bus
  46. SPI.end();
  47. delay(5);
  48. SPI.begin();
  49. delay(5);
  50.  
  51. // Reinitialize SD CS
  52. pinMode(SD_CS, OUTPUT);
  53. digitalWrite(SD_CS, HIGH);
  54. delay(10);
  55.  
  56. // Re-init SD card
  57. if (!SD.begin(SD_CS)) {
  58. Serial.println("[ERR] SD failed, retry...");
  59. delay(50);
  60. if (!SD.begin(SD_CS)) {
  61. Serial.println("[FATAL] SD permanently failed");
  62. return;
  63. }
  64. }
  65.  
  66. // Restart audio system
  67. audio.speakerPin = AUDIO_PIN;
  68. audio.setVolume(5);
  69.  
  70. Serial.println("[SD] Recovered");
  71. }
  72.  
  73.  
  74.  
  75. void setup() {
  76. Serial.begin(9600);
  77. delay(300);
  78.  
  79. pinMode(TILT_PIN, INPUT);
  80.  
  81. audio.speakerPin = AUDIO_PIN;
  82. audio.setVolume(5);
  83.  
  84. Serial.println("=== START ===");
  85.  
  86. resetSDandAudio(); // use the stable init
  87.  
  88. if (!SD.exists("SHAKE.WAV")) {
  89. Serial.println("[ERR] Missing SHAKE.WAV at startup!");
  90. }
  91.  
  92. lastActivity = millis();
  93. }
  94.  
  95.  
  96.  
  97.  
  98. void loop() {
  99.  
  100. // ==== SLEEP MODE ====
  101. if (millis() - lastActivity > sleepDelay) {
  102.  
  103. Serial.println("[SLEEP] Entering sleep");
  104.  
  105. audio.stopPlayback();
  106. shakePlaying = false;
  107. lockInput = false;
  108.  
  109. delay(20);
  110. attachInterrupt(digitalPinToInterrupt(TILT_PIN), wakeUp, CHANGE);
  111. Serial.end();
  112. delay(5);
  113. LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
  114. detachInterrupt(digitalPinToInterrupt(TILT_PIN));
  115.  
  116. // Clear serial garbage
  117. Serial.begin(9600);
  118. delay(20);
  119. Serial.println("[WAKE]");
  120.  
  121. resetSDandAudio(); // <--- FIXES YOUR WAKE SD ERRORS
  122.  
  123. pressCount = 0;
  124. lastActivity = millis();
  125. lastPress = millis();
  126. }
  127.  
  128.  
  129.  
  130. // ==== READ TILT ====
  131. uint8_t reading = digitalRead(TILT_PIN);
  132.  
  133. if (reading != lastState) {
  134. if (millis() - lastDebounce > debounceDelay) {
  135. lastState = reading;
  136. if (reading == HIGH) {
  137. handleTilt();
  138. }
  139. }
  140. lastDebounce = millis();
  141. }
  142.  
  143.  
  144.  
  145. // ==== CHECK AUDIO FINISH ====
  146. if (!audio.isPlaying()) {
  147.  
  148. if (shakePlaying) {
  149. shakePlaying = false;
  150. Serial.println("[INFO] Shake done");
  151. }
  152.  
  153. if (lockInput) {
  154. lockInput = false;
  155. Serial.println("[INFO] Random audio ended, input unlocked");
  156. }
  157. }
  158. }
  159.  
  160.  
  161.  
  162.  
  163. void handleTilt() {
  164.  
  165. if (lockInput) {
  166. Serial.println("[LOCK] Ignored tilt during random playback");
  167. return;
  168. }
  169.  
  170. unsigned long now = millis();
  171. lastActivity = now;
  172.  
  173. if (now - lastPress > timeoutReset) {
  174. pressCount = 0;
  175. Serial.println("[RESET] Timeout");
  176. }
  177.  
  178. pressCount++;
  179. lastPress = now;
  180.  
  181. Serial.print("[TILT] Count ");
  182. Serial.println(pressCount);
  183.  
  184. // 1–6 → SHAKE
  185. if (pressCount >= 1 && pressCount <= 6) {
  186. if (!audio.isPlaying()) {
  187. shakePlaying = true;
  188. playFile("SHAKE.WAV");
  189. }
  190. return;
  191. }
  192.  
  193. // 7 → stop shake
  194. if (pressCount == 7) {
  195. audio.stopPlayback();
  196. shakePlaying = false;
  197. Serial.println("[STOP] Shake stopped");
  198. return;
  199. }
  200.  
  201. // 8 → random
  202. if (pressCount == 8) {
  203. pressCount = 0;
  204. playRandom();
  205. }
  206. }
  207.  
  208.  
  209.  
  210.  
  211. void playFile(const char *name) {
  212.  
  213. if (!SD.exists(name)) {
  214. Serial.print("[ERR] Missing ");
  215. Serial.println(name);
  216. return;
  217. }
  218.  
  219. audio.stopPlayback();
  220. delay(5);
  221. audio.play(name);
  222.  
  223. Serial.print("[PLAY] ");
  224. Serial.println(name);
  225. }
  226.  
  227.  
  228.  
  229. void playRandom() {
  230.  
  231. lockInput = true;
  232.  
  233. char buf[8];
  234. uint8_t r = random(1, totalFiles + 1);
  235.  
  236. sprintf(buf, "%d.WAV", r);
  237.  
  238. playFile(buf);
  239. }
  240.  
Advertisement
Add Comment
Please, Sign In to add comment