Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.81 KB | None | 0 0
  1. #include <TMRpcm.h> //audio libs
  2. #include <SPI.h>
  3. #include <SdFat.h>
  4. #include "I2Cdev.h" //gyro libs
  5. #include "MPU6050.h"
  6. #include "Wire.h"
  7.  
  8. SdFat sd;
  9. File file;
  10.  
  11. TMRpcm audio;
  12.  
  13. MPU6050 gyro;
  14.  
  15. int16_t gx, gy, gz; //Wip
  16.  
  17. int SWING;
  18.  
  19. int CLASH;
  20.  
  21. int ON_TIME;
  22. int OFF_TIME;
  23.  
  24. boolean state = false;
  25. long pulseTime; //used to keep track of time in fading
  26. boolean goinDown; //check to fade up or down
  27. int curBright13;
  28. long blasterTime; //used to see if blaster block or not
  29. int BLASTER_DELAY; //time to press under to trigger blaster
  30.  
  31. int font;
  32. int numBoot; //attributes
  33. int numClash;
  34. int numOff;
  35. int numOn;
  36. int numSwing;
  37. int curVolume;
  38. int numBlast;
  39. int PULSE_DELAY; //length of pulse change from bright to dim
  40. int MIN_BRIGHT_13; //from 0-255
  41. int MAX_13 = 255; //max brightness for rgb
  42.  
  43. const char configFile[11] = "config.txt";
  44. const char setFont[8] = "set.txt";
  45. char blast[11] = "blast0.wav";
  46. char zero = '0';
  47. char newLine = '\n';
  48. char boot[16]; //files names
  49. char clash[17];
  50. char hum[14];
  51. char lockup[17];
  52. char off[15];
  53. char on[14];
  54. char swing[17];
  55.  
  56. #define mainButton A4
  57. #define auxButton A5
  58. #define led 13
  59. #define crystalLED 6
  60.  
  61. //crystal stuff
  62. int curBright6 = 255;
  63. int CRYSTAL_DELAY = 10;
  64. int minBright6 = 128;
  65. long crystalTime = 0;
  66. boolean crysDown = true;
  67. /*
  68. int freeRam ()
  69. {
  70. extern int __heap_start, *__brkval;
  71. int v;
  72. return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
  73. } */
  74.  
  75. void setup() {
  76. //Serial.begin(57600); //test stuff
  77. //while(!Serial); //test stuff
  78. pwm6(curBright6);
  79. sd.begin(4, SPI_FULL_SPEED);
  80. pinMode(mainButton, INPUT_PULLUP);
  81. pinMode(auxButton, INPUT_PULLUP);
  82. audio.speakerPin = 9;
  83. pinMode(10, OUTPUT); //second audio pin
  84. audio.setVolume(4); //default safe volume
  85. audio.quality(1);
  86. delay(50); //wait a little
  87. if(digitalRead(mainButton) == LOW) { //if button held from start, enter menu
  88. audio.play("choose.wav", 0);
  89. while(audio.isPlaying(0)); //wait for sound to finish
  90. chooseFont();
  91. }
  92. delay(50); //wait a little
  93. getAttributes(); //gets all the variables on number of files from font
  94. audio.setVolume(curVolume);
  95. Wire.begin();
  96. gyro.initialize();
  97. gyro.setFullScaleGyroRange(MPU6050_ACCEL_FS_16);
  98. randomSeed(analogRead(A1));
  99. pwm613config(1); //187.5khz
  100. boot[10] = zero + random(0, numBoot);
  101. audio.play(boot);
  102. while(audio.isPlaying());
  103. audio.loop(1, 1);
  104. }
  105.  
  106. void loop() {
  107. gyro.getRotation(&gx, &gy, &gz);
  108. gx = abs(gx);
  109. if(digitalRead(mainButton) == LOW) {
  110. state = !state;
  111. if(state)
  112. onAnim();
  113. else
  114. offAnim();
  115. }
  116. if(state) { //if on
  117. if(digitalRead(auxButton) == LOW) {
  118. blasterTime = millis();
  119. while(digitalRead(auxButton) == LOW) {
  120. delay(50); //debounce
  121. if(millis() - blasterTime >= BLASTER_DELAY) { //lockup time
  122. curBright13 = MAX_13;
  123. pwm13(curBright13);
  124. pwm6(curBright13);
  125. audio.play(lockup, 0);
  126. audio.loop(1, 0);
  127. while(digitalRead(auxButton) == LOW);
  128. audio.loop(0, 0);
  129. }
  130. }
  131. if(millis() - blasterTime < BLASTER_DELAY) {
  132. pwm13(MAX_13); //for now cuz no rgb
  133. pwm6(MAX_13);
  134. blast[5] = zero + random(0, numBlast); //need to make settable
  135. audio.play(blast, 0);
  136. while(audio.isPlaying(0));
  137. pwm13(curBright13);
  138. pwm6(curBright13);
  139. }
  140. audio.stopPlayback(0);
  141. }
  142. if(gx >= SWING && gx < CLASH) {
  143. swing[11] = zero + random(0, numSwing);
  144. audio.play(swing, 0);
  145. while(audio.isPlaying(0))
  146. updateLED();
  147. }
  148. if(gx >= CLASH) {
  149. pwm13(MAX_13);
  150. pwm6(MAX_13);
  151. clash[11] = zero + random(0, numClash);
  152. audio.play(clash, 0);
  153. while(audio.isPlaying(0));
  154. pwm13(curBright13);
  155. pwm13(curBright13);
  156. }
  157. updateLED();
  158. }
  159. if(!state)
  160. updateCrystal();
  161. }
  162.  
  163. void updateCrystal() {
  164. if(millis() - crystalTime >= CRYSTAL_DELAY) {
  165. crystalTime = millis();
  166. if(crysDown) {
  167. curBright6--;
  168. if(curBright6 < minBright6) {
  169. crysDown = false;
  170. curBright6++;
  171. curBright6++;
  172. }
  173. } else {
  174. curBright6++;
  175. if(curBright6 > 255) {
  176. crysDown = true;
  177. curBright6--;
  178. curBright6--;
  179. }
  180. }
  181. pwm6(curBright6);
  182. }
  183. }
  184.  
  185. void updateLED() {
  186. if(millis() - pulseTime >= PULSE_DELAY) {
  187. pulseTime = millis();
  188. if(goinDown) {
  189. curBright13--;
  190. if(curBright13 < MIN_BRIGHT_13) {
  191. goinDown = false;
  192. curBright13++;
  193. curBright13++;
  194. }
  195. } else {
  196. curBright13++;
  197. if(curBright13 > MAX_13) {
  198. goinDown = true;
  199. curBright13--;
  200. curBright13--;
  201. }
  202. }
  203. pwm13(curBright13);
  204. pwm6(curBright13);
  205. }
  206. }
  207.  
  208.  
  209. void onAnim() {
  210. //Serial.println("Turning on");
  211. on[8] = zero + random(0, numOn);
  212. //Serial.println(on);
  213. audio.play(on, 0);
  214. for(int i = 0; i <= MAX_13; i++) {
  215. pwm13(i);
  216. if(i > curBright6)
  217. pwm6(i);
  218. delay(ON_TIME/(MAX_13 + 1));
  219. }
  220. audio.play(hum, 1);
  221. goinDown = true;
  222. curBright13 = MAX_13;
  223. while(audio.isPlaying(0))
  224. updateLED();
  225. }
  226.  
  227. void offAnim() {
  228. //Serial.println("off");
  229. off[9] = zero + random(0, numOff);
  230. //Serial.println(off);
  231. audio.play(off, 0);
  232. audio.stopPlayback(1);
  233. for(int i = curBright13; i >= 0; i--) {
  234. pwm13(i);
  235. pwm6(i);
  236. delay(OFF_TIME/(curBright13 + 1));
  237. }
  238. while(audio.isPlaying());
  239. for(int i = 0; i <= curBright6; i++) {
  240. pwm6(i);
  241. delay(CRYSTAL_DELAY);
  242. }
  243. }
  244.  
  245. void chooseFont() {
  246. file = sd.open(setFont);
  247. font = (file.readStringUntil(newLine).charAt(0) - zero); //gets current font
  248. file.close();
  249. file = sd.open(configFile);
  250. int totalFont = file.readStringUntil(newLine).charAt(0) - zero;
  251. file.close();
  252. char bootFile[16]; //files need to be char arrays
  253. (String("font" + String(font) + "/boot0.wav")).toCharArray(bootFile, 16);
  254. //Serial.println(font);
  255. audio.play(bootFile);
  256. while(digitalRead(auxButton)) { //waits for button press to confirm font
  257. if(!digitalRead(mainButton)) { //increments through fonts
  258. font = (font + 1) % totalFont;
  259. bootFile[4] = zero + font;
  260. audio.play(bootFile);
  261. //Serial.println(bootFile);
  262. delay(500);
  263. }
  264. }
  265. saveFont();
  266. }
  267.  
  268. void saveFont() {
  269. //Serial.println(font);
  270. audio.play("selected.wav", 0);
  271. while(audio.isPlaying(0));
  272. file = sd.open(setFont);
  273. String selectedFont = file.readStringUntil(newLine);
  274. selectedFont.setCharAt(0, zero + font);
  275. file.close();
  276. sd.remove(setFont);
  277. file = sd.open(setFont, FILE_WRITE);
  278. file.println(selectedFont);
  279. file.close();
  280. file = sd.open(setFont); //don't know why i have to open and close again
  281. //while(file.available())
  282. // Serial.write(file.read());
  283. file.close();
  284. }
  285.  
  286. void getAttributes() {
  287. file = sd.open(setFont);
  288. font = file.readStringUntil(newLine).charAt(0) - zero;
  289. file.close();
  290. file = sd.open(configFile);
  291. file.readStringUntil(newLine); //skip the total font line;
  292. SWING = file.readStringUntil(newLine).substring(0, 5).toInt();
  293. CLASH = file.readStringUntil(newLine).substring(0, 5).toInt();
  294. BLASTER_DELAY = file.readStringUntil(newLine).substring(0, 3).toInt();
  295. numBlast = file.readStringUntil(newLine).substring(0, 1).toInt();
  296. file.close();
  297. String base = "font" + String(font) + "/";
  298. char configFont[18]; //attribute file
  299. (String(base + "numbers.txt")).toCharArray(configFont, 18);
  300. file = sd.open(configFont);
  301. numBoot = file.readStringUntil(newLine).charAt(0) - zero;
  302. numClash = file.readStringUntil(newLine).charAt(0) - zero;
  303. numOff = file.readStringUntil(newLine).charAt(0) - zero;
  304. numOn = file.readStringUntil(newLine).charAt(0) - zero;
  305. numSwing = file.readStringUntil(newLine).charAt(0) - zero;
  306. curVolume = file.readStringUntil(newLine).charAt(0) - zero;
  307. ON_TIME = file.readStringUntil(newLine).substring(0, 4).toInt();
  308. OFF_TIME = file.readStringUntil(newLine).substring(0, 4).toInt();
  309. PULSE_DELAY = file.readStringUntil(newLine).substring(0, 2).toInt();
  310. MIN_BRIGHT_13 = file.readStringUntil(newLine).substring(0, 3).toInt();
  311. MIN_BRIGHT_13 = MAX_13 * MIN_BRIGHT_13 / 100; //calculate now so not later
  312. //crystal stuf
  313. CRYSTAL_DELAY = file.readStringUntil(newLine).substring(0, 2).toInt();
  314. minBright6 = file.readStringUntil(newLine).substring(0, 3).toInt();
  315. file.close();
  316. (String(base + "boot0.wav")).toCharArray(boot, 16);
  317. (String(base + "clash0.wav")).toCharArray(clash, 17);
  318. (String(base + "hum.wav")).toCharArray(hum, 14);
  319. (String(base + "lockup.wav")).toCharArray(lockup, 17);
  320. (String(base + "off0.wav")).toCharArray(off, 15);
  321. (String(base + "on0.wav")).toCharArray(on, 14);
  322. (String(base + "swing0.wav")).toCharArray(swing, 17);
  323. }
  324.  
  325. void pwm613config(int mode) {
  326. TCCR4A = 0;
  327. TCCR4B = mode;
  328. TCCR4C = 0;
  329. TCCR4D = 0;
  330. TCCR4D = 0;
  331. PLLFRQ = (PLLFRQ&0xCF)|0x30;
  332. OCR4C = 255;
  333. }
  334.  
  335. void pwm13(int value) {
  336. OCR4A = value;
  337. DDRC|=1<<7;
  338. TCCR4A=0x82;
  339. }
  340.  
  341. void pwm6(int value) {
  342. OCR4D = value;
  343. DDRD|=1<<7;
  344. TCCR4C|=0x09;
  345. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement