Advertisement
Guest User

Untitled

a guest
Feb 7th, 2015
443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.91 KB | None | 0 0
  1. #include <SdFat.h> // need to include the SD library
  2. //#define SD_ChipSelectPin 53 //example uses hardware SS pin 53 on Mega2560
  3. SdFat sd;
  4.  
  5. #include <TMRpcm.h> // also need to include this library...
  6. #include <SPI.h>
  7. #include <avr/power.h>
  8. #include <avr/sleep.h>
  9.  
  10. TMRpcm tmrpcm; // create an object for use in this sketch
  11.  
  12. #define SD_ChipSelectPin 10 //using digital pin 4 on arduino nano 328, can use other pins
  13.  
  14. long debounceDelay = 50; // the debounce time; increase if the output flickers
  15.  
  16. const int button3 = 7; // the number of the pushbutton pin
  17. int button3State = 0; // variable for reading the pushbutton status
  18. int lastButton3State = HIGH; // the previous reading from the input pin
  19. long lastDebounceTime3 = 0; // the last time the output pin was toggled
  20.  
  21. const int button2 = 6;
  22. int button2State = 0;
  23. int lastButton2State = HIGH; // the previous reading from the input pin
  24. long lastDebounceTime2 = 0; // the last time the output pin was toggled
  25.  
  26. const int buttonPin = 5; // the number of the pushbutton pin
  27. int buttonState = 0; // variable for reading the pushbutton status
  28. int lastButtonState = HIGH; // the previous reading from the input pin
  29. long lastDebounceTime = 0; // the last time the output pin was toggled
  30.  
  31. int resetPin = 4;
  32. int resetCount = 0;
  33.  
  34. int sdPower = 3;
  35.  
  36. int sleepWait = 0;
  37. int track1 = 1;
  38. int track2 = 1;
  39. int track3 = 1;
  40. String str;
  41. int wakePin = 2;
  42. int Speaker = 8; // variable for reading the speaker status
  43. int buttonSelect = 0;
  44.  
  45. int wasSleeping = 0;
  46.  
  47. Sd2Card card;
  48. SdVolume volume;
  49. SdFile root;
  50.  
  51. unsigned long time = 0;
  52.  
  53. int failSafe = 0;
  54.  
  55. void setup() {
  56. digitalWrite(resetPin, HIGH);
  57. pinMode(sdPower, OUTPUT);
  58. digitalWrite(sdPower, HIGH);
  59. pinMode(SD_ChipSelectPin, OUTPUT);
  60. digitalWrite(SD_ChipSelectPin, HIGH);
  61. delay(200);
  62. pinMode(resetPin, OUTPUT);
  63. pinMode(wakePin, INPUT);
  64. tmrpcm.speakerPin = 9; //5,6,11 or 46 on Mega, 9 on Uno, Nano, etc
  65. pinMode(Speaker, OUTPUT);
  66. digitalWrite(Speaker, LOW);
  67. Serial.begin(9600);
  68.  
  69. pinMode(buttonPin, INPUT);
  70. digitalWrite(buttonPin, HIGH);
  71.  
  72.  
  73. while (!sd.begin(SD_ChipSelectPin, SPI_HALF_SPEED)) {
  74. sd.begin(SD_ChipSelectPin, SPI_HALF_SPEED);
  75. Serial.println("SD FAILED");
  76. }
  77. Serial.println("SD OK");
  78.  
  79.  
  80.  
  81. }
  82.  
  83.  
  84.  
  85. void loop() {
  86.  
  87.  
  88. int reading = digitalRead(buttonPin);
  89. if (reading != lastButtonState) {
  90. // reset the debouncing timer
  91. lastDebounceTime = millis();
  92. Serial.println("button 1 pressed\n");
  93.  
  94. }
  95. if ((millis() - lastDebounceTime) > debounceDelay) {
  96. // whatever the reading is at, it's been there for longer
  97. // than the debounce delay, so take it as the actual current state:
  98.  
  99. // if the button state has changed:
  100. if (reading != buttonState) {
  101. buttonState = reading;
  102.  
  103. // only toggle the LED if the new button state is HIGH
  104. if (buttonState == LOW) {
  105. Serial.print("\nButton 1 Pressed\n");
  106. buttonSelect = 0;
  107. Play();
  108. }
  109. }
  110. }
  111. lastButtonState = reading;
  112.  
  113. int reading2 = digitalRead(button2);
  114. if (reading2 != lastButton2State) {
  115. // reset the debouncing timer
  116. lastDebounceTime2 = millis();
  117. Serial.println("button 2 pressed\n");
  118.  
  119. }
  120. if ((millis() - lastDebounceTime2) > debounceDelay) {
  121. // whatever the reading is at, it's been there for longer
  122. // than the debounce delay, so take it as the actual current state:
  123.  
  124. // if the button state has changed:
  125. if (reading2 != button2State) {
  126. button2State = reading2;
  127.  
  128. // only toggle the LED if the new button state is HIGH
  129. if (button2State == LOW) {
  130. Serial.print("\nButton 2 Pressed\n");
  131. buttonSelect = 1;
  132. Play();
  133. }
  134. }
  135. }
  136. lastButton2State = reading2;
  137.  
  138.  
  139. int reading3 = digitalRead(button3);
  140. if (reading3 != lastButton3State) {
  141. // reset the debouncing timer
  142. lastDebounceTime3 = millis();
  143. Serial.println("button 3 pressed\n");
  144.  
  145. }
  146. if ((millis() - lastDebounceTime3) > debounceDelay) {
  147. // whatever the reading is at, it's been there for longer
  148. // than the debounce delay, so take it as the actual current state:
  149.  
  150. // if the button state has changed:
  151. if (reading3 != button3State) {
  152. button3State = reading3;
  153.  
  154. // only toggle the LED if the new button state is HIGH
  155. if (button3State == LOW) {
  156. Serial.print("\nButton 3 Pressed\n");
  157. buttonSelect = 2;
  158. Play();
  159. }
  160. }
  161. }
  162. lastButton3State = reading3;
  163.  
  164.  
  165.  
  166. delay(100);
  167. if (!tmrpcm.isPlaying()) {
  168. if (sleepWait == 20) {
  169. digitalWrite(Speaker, LOW);
  170. digitalWrite(SD_ChipSelectPin, LOW);
  171. digitalWrite(sdPower, LOW);
  172. sleepNow();
  173. }
  174. sleepWait++;
  175. delay(50);
  176.  
  177. }
  178. }
  179.  
  180. void button2press (int reading) {
  181.  
  182.  
  183. }
  184.  
  185. void button1press (int reading) {
  186.  
  187.  
  188. }
  189.  
  190. void Play () {
  191. Serial.println("PLAYING");
  192. digitalWrite(SD_ChipSelectPin, HIGH);
  193. digitalWrite(sdPower, HIGH);
  194. digitalWrite(Speaker, HIGH);
  195.  
  196. if (wasSleeping == 1) {
  197. if (!sd.begin(SD_ChipSelectPin, SPI_HALF_SPEED)) {
  198. return;
  199. } else {
  200. Serial.println("SD OK");
  201. }
  202. }
  203.  
  204. wasSleeping = 0;
  205.  
  206. if (buttonSelect == 0) {
  207.  
  208. if (!tmrpcm.isPlaying()) {
  209. char b[20];
  210. str = "music/";
  211. str.concat(track1);
  212. str.toCharArray(b, 20);
  213. Serial.print(b);
  214. Serial.print("\n");
  215. if (sd.exists(b)) {
  216. tmrpcm.play(b);
  217. track1++;
  218. } else {
  219. track1 = 1;
  220. char b[20];
  221. str = "music/";
  222. str.concat(track1);
  223. // str = String(track); //converting integer into a string
  224. str.toCharArray(b, 20);
  225. Serial.print(b);
  226. Serial.print("\n");
  227. tmrpcm.play(b);
  228. track1++;
  229. Serial.print("\nDoesn't exist\n");
  230. }
  231. } else {
  232.  
  233. tmrpcm.stopPlayback();
  234. delay(100);
  235. char b[20];
  236. str = "music/";
  237. str.concat(track1);
  238. // str = String(track); //converting integer into a string
  239. str.toCharArray(b, 20);
  240. Serial.print(b);
  241. Serial.print("\n");
  242. if (sd.exists(b)) {
  243. tmrpcm.play(b);
  244. track1++;
  245. } else {
  246. str = "music/";
  247. track1 = 1;
  248. char b[20];
  249. str.concat(track1);
  250. // str = String(track); //converting integer into a string
  251. str.toCharArray(b, 20);
  252. Serial.print(b);
  253. Serial.print("\n");
  254. tmrpcm.play(b);
  255. track1++;
  256. Serial.print("\nDoesn't exist\n");
  257. }
  258. }
  259.  
  260. }
  261. if (buttonSelect == 1) {
  262.  
  263.  
  264. if (!tmrpcm.isPlaying()) {
  265. str = "fx/";
  266. char b[20];
  267. str.concat(track2);
  268. str.toCharArray(b, 20);
  269. Serial.print(b);
  270. Serial.print("\n");
  271. if (sd.exists(b)) {
  272. tmrpcm.play(b);
  273. track2++;
  274. } else {
  275. str = "fx/";
  276. track2 = 1;
  277. char b[20];
  278. str.concat(track2);
  279. // str = String(track2); //converting integer into a string
  280. str.toCharArray(b, 20);
  281. Serial.print(b);
  282. Serial.print("\n");
  283. tmrpcm.play(b);
  284. track2++;
  285. Serial.print("\nDoesn't exist\n");
  286. }
  287. } else {
  288. str = "fx/";
  289. tmrpcm.stopPlayback();
  290. delay(100);
  291. char b[20];
  292. str.concat(track2);
  293. // str = String(track2); //converting integer into a string
  294. str.toCharArray(b, 20);
  295. Serial.print(b);
  296. Serial.print("\n");
  297. if (sd.exists(b)) {
  298. tmrpcm.play(b);
  299. track2++;
  300. } else {
  301. str = "fx/";
  302. track2 = 1;
  303. char b[20];
  304. str.concat(track2);
  305. // str = String(track); //converting integer into a string
  306. str.toCharArray(b, 20);
  307. Serial.print(b);
  308. Serial.print("\n");
  309. tmrpcm.play(b);
  310. track2++;
  311. Serial.print("\nDoesn't exist\n");
  312. }
  313. }
  314. }
  315. if (buttonSelect == 2) {
  316.  
  317.  
  318. if (!tmrpcm.isPlaying()) {
  319. str = "msg/";
  320. char b[20];
  321. str.concat(track3);
  322. str.toCharArray(b, 20);
  323. Serial.print(b);
  324. Serial.print("\n");
  325. if (sd.exists(b)) {
  326. tmrpcm.play(b);
  327. track3++;
  328. } else {
  329. str = "msg/";
  330. track3 = 1;
  331. char b[20];
  332. str.concat(track3);
  333. // str = String(track3); //converting integer into a string
  334. str.toCharArray(b, 20);
  335. Serial.print(b);
  336. Serial.print("\n");
  337. tmrpcm.play(b);
  338. track3++;
  339. Serial.print("\nDoesn't exist\n");
  340. }
  341. } else {
  342. tmrpcm.stopPlayback();
  343. delay(100);
  344. str = "msg/";
  345. char b[20];
  346. str.concat(track3);
  347. // str = String(track3); //converting integer into a string
  348. str.toCharArray(b, 20);
  349. Serial.print(b);
  350. Serial.print("\n");
  351. if (sd.exists(b)) {
  352. tmrpcm.play(b);
  353. track3++;
  354. } else {
  355. str = "msg/";
  356. track3 = 1;
  357. char b[20];
  358. str.concat(track3);
  359. // str = String(track); //converting integer into a string
  360. str.toCharArray(b, 20);
  361. Serial.print(b);
  362. Serial.print("\n");
  363. tmrpcm.play(b);
  364. track3++;
  365. Serial.print("\nDoesn't exist\n");
  366. }
  367. }
  368. }
  369.  
  370.  
  371.  
  372. }
  373.  
  374. void sleepNow(void)
  375. {
  376. Serial.print("\nGoing to Sleep\n");
  377. // Set pin 2 as interrupt and attach handler:
  378. digitalWrite(SD_ChipSelectPin, LOW);
  379. lastButton2State = LOW;
  380. lastButton2State = LOW;
  381. lastButtonState = LOW;
  382. delay(100);
  383. Serial.flush();
  384. //
  385. // Choose our preferred sleep mode:
  386. set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  387.  
  388. noInterrupts ();
  389. attachInterrupt(0, Wake, LOW);
  390. //
  391. // Set sleep enable (SE) bit:
  392. sleep_enable();
  393. //
  394. // Put the device to sleep:
  395. interrupts ();
  396. sleep_cpu();
  397. //
  398. // Upon waking up, sketch continues from this point.
  399. sleep_disable();
  400. }
  401.  
  402. void Wake ()
  403. {
  404. sleepWait = 0;
  405. wasSleeping = 1;
  406. detachInterrupt(0);
  407. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement