Advertisement
Guest User

Untitled

a guest
Mar 24th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. int speakerPin = 9;
  2.  
  3. int length = 28; // the number of notes
  4.  
  5. char notes[] = "GGAGcB GGAGdc GGxecBA yyecdc";
  6.  
  7. int beats[] = { 2, 2, 8, 8, 8, 16, 1, 2, 2, 8, 8,8, 16, 1, 2,2,8,8,8,8,16, 1,2,2,8,8,8,16 };
  8.  
  9. int tempo = 150;
  10. #include <Wire.h>
  11. #include <LiquidCrystal_I2C.h>
  12. //i2c pins
  13. LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); //
  14.  
  15.  
  16. void playTone(int tone, int duration) {
  17.  
  18. for (long i = 0; i < duration * 1000L; i += tone * 2) {
  19.  
  20. digitalWrite(speakerPin, HIGH);
  21.  
  22. delayMicroseconds(tone);
  23.  
  24. digitalWrite(speakerPin, LOW);
  25.  
  26. delayMicroseconds(tone);
  27.  
  28. }
  29.  
  30. }
  31.  
  32. void playNote(char note, int duration) {
  33.  
  34. char names[] = {'C', 'D', 'E', 'F', 'G', 'A', 'B',
  35.  
  36. 'c', 'd', 'e', 'f', 'g', 'a', 'b',
  37.  
  38. 'x', 'y' };
  39.  
  40. int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014,
  41.  
  42. 956, 834, 765, 593, 468, 346, 224,
  43.  
  44. 655 , 715 };
  45.  
  46. int SPEE = 5;
  47.  
  48. // play the tone corresponding to the note name
  49.  
  50. for (int i = 0; i < 17; i++) {
  51.  
  52. if (names[i] == note) {
  53. int newduration = duration/SPEE;
  54. playTone(tones[i], newduration);
  55.  
  56. }
  57.  
  58. }
  59.  
  60. }
  61.  
  62. void setup() {
  63.  
  64. pinMode(speakerPin, OUTPUT);
  65. //WE define our LCD 16 columns and 2 rows
  66. lcd.begin(16, 2);
  67. lcd.backlight();//Power on the back light
  68. }
  69.  
  70. void loop() {
  71. //Write your text:
  72. lcd.setCursor(0, 0); //we start writing from the first row first column
  73. lcd.print(" Happy Birthday"); //16 characters poer line
  74. delay(1000);//Delay used to give a dinamic effect
  75. lcd.setCursor(0, 1);
  76. lcd.print(" Irakli!");
  77. delay(1000);
  78. for (int i = 0; i < length; i++) {
  79.  
  80. if (notes[i] == ' ') {
  81.  
  82. delay(beats[i] * tempo); // rest
  83.  
  84. } else {
  85.  
  86. playNote(notes[i], beats[i] * tempo);
  87.  
  88. }
  89.  
  90. // pause between notes
  91.  
  92. delay(tempo);
  93.  
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement