Advertisement
shauryarajbansal

error

Nov 6th, 2019
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.65 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <LiquidCrystal_I2C.h>
  3. #include <boarddefs.h>
  4. #include <IRremote.h>
  5. LiquidCrystal_I2C lcd( 0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
  6.  
  7. int result;
  8. int IR_Recv = 11;
  9.  
  10. IRrecv irrecv(IR_Recv);
  11. decode_results results;
  12. boolean replay = false;
  13.  
  14. //mario========================================================================================================
  15. #define NOTE_B0 31
  16. #define NOTE_C1 33
  17. #define NOTE_CS1 35
  18. #define NOTE_D1 37
  19. #define NOTE_DS1 39
  20. #define NOTE_E1 41
  21. #define NOTE_F1 44
  22. #define NOTE_FS1 46
  23. #define NOTE_G1 49
  24. #define NOTE_GS1 52
  25. #define NOTE_A1 55
  26. #define NOTE_AS1 58
  27. #define NOTE_B1 62
  28. #define NOTE_C2 65
  29. #define NOTE_CS2 69
  30. #define NOTE_D2 73
  31. #define NOTE_DS2 78
  32. #define NOTE_E2 82
  33. #define NOTE_F2 87
  34. #define NOTE_FS2 93
  35. #define NOTE_G2 98
  36. #define NOTE_GS2 104
  37. #define NOTE_A2 110
  38. #define NOTE_AS2 117
  39. #define NOTE_B2 123
  40. #define NOTE_C3 131
  41. #define NOTE_CS3 139
  42. #define NOTE_D3 147
  43. #define NOTE_DS3 156
  44. #define NOTE_E3 165
  45. #define NOTE_F3 175
  46. #define NOTE_FS3 185
  47. #define NOTE_G3 196
  48. #define NOTE_GS3 208
  49. #define NOTE_A3 220
  50. #define NOTE_AS3 233
  51. #define NOTE_B3 247
  52. #define NOTE_C4 262
  53. #define NOTE_CS4 277
  54. #define NOTE_D4 294
  55. #define NOTE_DS4 311
  56. #define NOTE_E4 330
  57. #define NOTE_F4 349
  58. #define NOTE_FS4 370
  59. #define NOTE_G4 392
  60. #define NOTE_GS4 415
  61. #define NOTE_A4 440
  62. #define NOTE_AS4 466
  63. #define NOTE_B4 494
  64. #define NOTE_C5 523
  65. #define NOTE_CS5 554
  66. #define NOTE_D5 587
  67. #define NOTE_DS5 622
  68. #define NOTE_E5 659
  69. #define NOTE_F5 698
  70. #define NOTE_FS5 740
  71. #define NOTE_G5 784
  72. #define NOTE_GS5 831
  73. #define NOTE_A5 880
  74. #define NOTE_AS5 932
  75. #define NOTE_B5 988
  76. #define NOTE_C6 1047
  77. #define NOTE_CS6 1109
  78. #define NOTE_D6 1175
  79. #define NOTE_DS6 1245
  80. #define NOTE_E6 1319
  81. #define NOTE_F6 1397
  82. #define NOTE_FS6 1480
  83. #define NOTE_G6 1568
  84. #define NOTE_GS6 1661
  85. #define NOTE_A6 1760
  86. #define NOTE_AS6 1865
  87. #define NOTE_B6 1976
  88. #define NOTE_C7 2093
  89. #define NOTE_CS7 2217
  90. #define NOTE_D7 2349
  91. #define NOTE_DS7 2489
  92. #define NOTE_E7 2637
  93. #define NOTE_F7 2794
  94. #define NOTE_FS7 2960
  95. #define NOTE_G7 3136
  96. #define NOTE_GS7 3322
  97. #define NOTE_A7 3520
  98. #define NOTE_AS7 3729
  99. #define NOTE_B7 3951
  100. #define NOTE_C8 4186
  101. #define NOTE_CS8 4435
  102. #define NOTE_D8 4699
  103. #define NOTE_DS8 4978
  104. #define REST 0
  105. #define melodyPin 3
  106. //Mario main theme melody
  107. int melody[] = {
  108. NOTE_E7, NOTE_E7, 0, NOTE_E7,
  109. 0, NOTE_C7, NOTE_E7, 0,
  110. NOTE_G7, 0, 0, 0,
  111. NOTE_G6, 0, 0, 0,
  112.  
  113. NOTE_C7, 0, 0, NOTE_G6,
  114. 0, 0, NOTE_E6, 0,
  115. 0, NOTE_A6, 0, NOTE_B6,
  116. 0, NOTE_AS6, NOTE_A6, 0,
  117.  
  118. NOTE_G6, NOTE_E7, NOTE_G7,
  119. NOTE_A7, 0, NOTE_F7, NOTE_G7,
  120. 0, NOTE_E7, 0, NOTE_C7,
  121. NOTE_D7, NOTE_B6, 0, 0,
  122.  
  123. NOTE_C7, 0, 0, NOTE_G6,
  124. 0, 0, NOTE_E6, 0,
  125. 0, NOTE_A6, 0, NOTE_B6,
  126. 0, NOTE_AS6, NOTE_A6, 0,
  127.  
  128. NOTE_G6, NOTE_E7, NOTE_G7,
  129. NOTE_A7, 0, NOTE_F7, NOTE_G7,
  130. 0, NOTE_E7, 0, NOTE_C7,
  131. NOTE_D7, NOTE_B6, 0, 0
  132. };
  133. //Mario main them tempo
  134. int tempo[] = {
  135. 12, 12, 12, 12,
  136. 12, 12, 12, 12,
  137. 12, 12, 12, 12,
  138. 12, 12, 12, 12,
  139.  
  140. 12, 12, 12, 12,
  141. 12, 12, 12, 12,
  142. 12, 12, 12, 12,
  143. 12, 12, 12, 12,
  144.  
  145. 9, 9, 9,
  146. 12, 12, 12, 12,
  147. 12, 12, 12, 12,
  148. 12, 12, 12, 12,
  149.  
  150. 12, 12, 12, 12,
  151. 12, 12, 12, 12,
  152. 12, 12, 12, 12,
  153. 12, 12, 12, 12,
  154.  
  155. 9, 9, 9,
  156. 12, 12, 12, 12,
  157. 12, 12, 12, 12,
  158. 12, 12, 12, 12,
  159. };
  160. //Underworld melody
  161. int underworld_melody[] = {
  162. NOTE_C4, NOTE_C5, NOTE_A3, NOTE_A4,
  163. NOTE_AS3, NOTE_AS4, 0,
  164. 0,
  165. NOTE_C4, NOTE_C5, NOTE_A3, NOTE_A4,
  166. NOTE_AS3, NOTE_AS4, 0,
  167. 0,
  168. NOTE_F3, NOTE_F4, NOTE_D3, NOTE_D4,
  169. NOTE_DS3, NOTE_DS4, 0,
  170. 0,
  171. NOTE_F3, NOTE_F4, NOTE_D3, NOTE_D4,
  172. NOTE_DS3, NOTE_DS4, 0,
  173. 0, NOTE_DS4, NOTE_CS4, NOTE_D4,
  174. NOTE_CS4, NOTE_DS4,
  175. NOTE_DS4, NOTE_GS3,
  176. NOTE_G3, NOTE_CS4,
  177. NOTE_C4, NOTE_FS4, NOTE_F4, NOTE_E3, NOTE_AS4, NOTE_A4,
  178. NOTE_GS4, NOTE_DS4, NOTE_B3,
  179. NOTE_AS3, NOTE_A3, NOTE_GS3,
  180. 0, 0, 0
  181. };
  182. //Underwolrd tempo
  183. int underworld_tempo[] = {
  184. 12, 12, 12, 12,
  185. 12, 12, 6,
  186. 3,
  187. 12, 12, 12, 12,
  188. 12, 12, 6,
  189. 3,
  190. 12, 12, 12, 12,
  191. 12, 12, 6,
  192. 3,
  193. 12, 12, 12, 12,
  194. 12, 12, 6,
  195. 6, 18, 18, 18,
  196. 6, 6,
  197. 6, 6,
  198. 6, 6,
  199. 18, 18, 18, 18, 18, 18,
  200. 10, 10, 10,
  201. 10, 10, 10,
  202. 3, 3, 3
  203. }; //mario end================================================================================================================================================
  204. // change this to make the song slower or faster
  205. int tempopotter = 144;
  206.  
  207. // change this to whichever pin you want to use
  208. int buzzer = 3;
  209.  
  210. // notes of the moledy followed by the duration.
  211. // a 4 means a quarter note, 8 an eighteenth , 16 sixteenth, so on
  212. // !!negative numbers are used to represent dotted notes,
  213. // so -4 means a dotted quarter note, that is, a quarter plus an eighteenth!!
  214. int melodypotter[] = {
  215.  
  216.  
  217.  
  218.  
  219. REST, 2, NOTE_D4, 4,
  220. NOTE_G4, -4, NOTE_AS4, 8, NOTE_A4, 4,
  221. NOTE_G4, 2, NOTE_D5, 4,
  222. NOTE_C5, -2,
  223. NOTE_A4, -2,
  224. NOTE_G4, -4, NOTE_AS4, 8, NOTE_A4, 4,
  225. NOTE_F4, 2, NOTE_GS4, 4,
  226. NOTE_D4, -1,
  227. NOTE_D4, 4,
  228.  
  229. NOTE_G4, -4, NOTE_AS4, 8, NOTE_A4, 4, //10
  230. NOTE_G4, 2, NOTE_D5, 4,
  231. NOTE_F5, 2, NOTE_E5, 4,
  232. NOTE_DS5, 2, NOTE_B4, 4,
  233. NOTE_DS5, -4, NOTE_D5, 8, NOTE_CS5, 4,
  234. NOTE_CS4, 2, NOTE_B4, 4,
  235. NOTE_G4, -1,
  236. NOTE_AS4, 4,
  237.  
  238. NOTE_D5, 2, NOTE_AS4, 4,//18
  239. NOTE_D5, 2, NOTE_AS4, 4,
  240. NOTE_DS5, 2, NOTE_D5, 4,
  241. NOTE_CS5, 2, NOTE_A4, 4,
  242. NOTE_AS4, -4, NOTE_D5, 8, NOTE_CS5, 4,
  243. NOTE_CS4, 2, NOTE_D4, 4,
  244. NOTE_D5, -1,
  245. REST, 4, NOTE_AS4, 4,
  246.  
  247. NOTE_D5, 2, NOTE_AS4, 4,//26
  248. NOTE_D5, 2, NOTE_AS4, 4,
  249. NOTE_F5, 2, NOTE_E5, 4,
  250. NOTE_DS5, 2, NOTE_B4, 4,
  251. NOTE_DS5, -4, NOTE_D5, 8, NOTE_CS5, 4,
  252. NOTE_CS4, 2, NOTE_AS4, 4,
  253. NOTE_G4, -1,
  254.  
  255. };
  256.  
  257. // sizeof gives the number of bytes, each int value is composed of two bytes (16 bits)
  258. // there are two values per note (pitch and duration), so for each note there are four bytes
  259. int notespotter = sizeof(melodypotter) / sizeof(melodypotter[0]) / 2;
  260.  
  261. // this calculates the duration of a whole note in ms (60s/tempo)*4 beats
  262. int wholenotepotter = (60000 * 4) / tempopotter;
  263.  
  264. int dividerpotter = 0, noteDurationpotter = 0;
  265. //================================================================================================================================
  266.  
  267.  
  268. void setup(void)
  269. {
  270. pinMode(3, OUTPUT);//buzzer
  271. irrecv.enableIRIn(); // Starts the receiver
  272.  
  273. lcd.begin(16, 2);
  274. lcd.print("Welcome to");
  275. lcd.setCursor(3, 1);
  276. delay(1000);
  277. lcd.print("WEATHER TV");
  278. delay(5000);
  279. lcd.setCursor(0, 0);
  280. lcd.clear();
  281. lcd.print("select");
  282. lcd.setCursor(0, 0);
  283. delay(5000);
  284. lcd.clear();
  285. lcd.print("1. Songs");
  286. lcd.setCursor(0, 1);
  287. lcd.print("2. Clock");
  288.  
  289. }
  290. void loop()
  291. {
  292.  
  293. if (irrecv.decode(&results)) {
  294. nothing:
  295. irrecv.resume();
  296. if (replay == true) {
  297. lcd.setCursor(0, 0);
  298. lcd.clear();
  299. lcd.print("select");
  300. lcd.setCursor(0, 0);
  301. delay(5000);
  302. lcd.clear();
  303. lcd.print("1. Songs");
  304. lcd.setCursor(0, 1);
  305. lcd.print("2. Clock");
  306. }
  307.  
  308.  
  309.  
  310. switch (results.value) {
  311. case 16738455:
  312. something:
  313. irrecv.resume();
  314. lcd.clear();
  315. lcd.setCursor(0, 0);
  316. lcd.print("1 Mario");
  317. lcd.setCursor(0, 1);
  318. lcd.print("2. Potter");
  319. delay(2000);
  320. if (results.value == 16738455) {
  321. lcd.clear();
  322. lcd.setCursor(0, 0);
  323. lcd.print("PLAYING MARIO");
  324. mario();
  325. lcd.clear();
  326. }
  327. else if (results.value == 16750695) {
  328. lcd.clear();
  329. lcd.setCursor(0, 0);
  330. lcd.print("PLAYING POTTER");
  331. potter();
  332. lcd.clear();
  333. }
  334. else if (results.value == 16730805) {
  335. lcd.clear();
  336. boolean replay = true;
  337. goto nothing;
  338. }
  339.  
  340. break;
  341. }
  342. boolean replay = false;
  343. irrecv.resume();
  344. goto something;
  345. }
  346. }
  347.  
  348.  
  349.  
  350. //=================================================================F U N C T I O N S================================================================
  351. void mario()
  352. {
  353. sing(1);
  354. sing(1);
  355. sing(2);
  356. }
  357.  
  358. int song = 0;
  359.  
  360. void sing(int s) {
  361. // iterate over the notes of the melody:
  362. song = s;
  363. if (song == 2) {
  364. Serial.println(" 'Underworld Theme'");
  365. int size = sizeof(underworld_melody) / sizeof(int);
  366. for (int thisNote = 0; thisNote < size; thisNote++) {
  367.  
  368. // to calculate the note duration, take one second
  369. // divided by the note type.
  370. //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
  371. int noteDuration = 1000 / underworld_tempo[thisNote];
  372.  
  373. buzz(melodyPin, underworld_melody[thisNote], noteDuration);
  374.  
  375. // to distinguish the notes, set a minimum time between them.
  376. // the note's duration + 30% seems to work well:
  377. int pauseBetweenNotes = noteDuration * 1.30;
  378. delay(pauseBetweenNotes);
  379.  
  380. // stop the tone playing:
  381. buzz(melodyPin, 0, noteDuration);
  382.  
  383. }
  384.  
  385. } else {
  386.  
  387. Serial.println(" 'Mario Theme'");
  388. int size = sizeof(melody) / sizeof(int);
  389. for (int thisNote = 0; thisNote < size; thisNote++) {
  390.  
  391. // to calculate the note duration, take one second
  392. // divided by the note type.
  393. //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
  394. int noteDuration = 1000 / tempo[thisNote];
  395.  
  396. buzz(melodyPin, melody[thisNote], noteDuration);
  397.  
  398. // to distinguish the notes, set a minimum time between them.
  399. // the note's duration + 30% seems to work well:
  400. int pauseBetweenNotes = noteDuration * 1.30;
  401. delay(pauseBetweenNotes);
  402.  
  403. // stop the tone playing:
  404. buzz(melodyPin, 0, noteDuration);
  405.  
  406. }
  407. }
  408. }
  409.  
  410. void buzz(int targetPin, long frequency, long length) {
  411. digitalWrite(13, HIGH);
  412. long delayValue = 1000000 / frequency / 2; // calculate the delay value between transitions
  413. //// 1 second's worth of microseconds, divided by the frequency, then split in half since
  414. //// there are two phases to each cycle
  415. long numCycles = frequency * length / 1000; // calculate the number of cycles for proper timing
  416. //// multiply frequency, which is really cycles per second, by the number of seconds to
  417. //// get the total number of cycles to produce
  418. for (long i = 0; i < numCycles; i++) { // for the calculated length of time...
  419. digitalWrite(targetPin, HIGH); // write the buzzer pin high to push out the diaphram
  420. delayMicroseconds(delayValue); // wait for the calculated delay value
  421. digitalWrite(targetPin, LOW); // write the buzzer pin low to pull back the diaphram
  422. delayMicroseconds(delayValue); // wait again or the calculated delay value
  423. }
  424. digitalWrite(13, LOW);
  425. }
  426. void potter() {
  427. // iterate over the notes of the melody.
  428. // Remember, the array is twice the number of notes (notes + durations)
  429. for (int thisNotepotter = 0; thisNotepotter < notespotter * 2; thisNotepotter = thisNotepotter + 2) {
  430.  
  431. // calculates the duration of each note
  432. dividerpotter = melodypotter[thisNotepotter + 1];
  433. if (dividerpotter > 0) {
  434. // regular note, just proceed
  435. noteDurationpotter = (wholenotepotter) / dividerpotter;
  436. } else if (dividerpotter < 0) {
  437. // dotted notes are represented with negative durations!!
  438. noteDurationpotter = (wholenotepotter) / abs(dividerpotter);
  439. noteDurationpotter *= 1.5; // increases the duration in half for dotted notes
  440. }
  441.  
  442. // we only play the note for 90% of the duration, leaving 10% as a pause
  443. tone(buzzer, melodypotter[thisNotepotter], noteDurationpotter * 0.9);
  444.  
  445. // Wait for the specief duration before playing the next note.
  446. delay(noteDurationpotter);
  447.  
  448. // stop the waveform generation before the next note.
  449. noTone(buzzer);
  450. }
  451. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement