Advertisement
Guest User

Untitled

a guest
Dec 25th, 2011
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. /*
  2. Floppy Drive Music: Twinkle Twinkle Little Star
  3. Lapsus Antepedis
  4. Dec 25 2011
  5. Times are in microseconds
  6. */
  7.  
  8. // Constants
  9. const int MAX_STEPS = 70;
  10. int scale[20] = {7644,7214,6812,6428,6068,5728,5406,5102,4816,4546,4290,4048,3824,3608,3404,3214,3034,2864,2704,2550};
  11.  
  12. // Song, extra 0 at the start.
  13. int note[] = {0,0,0,7,7,9,9,7,5,5,4,4,2,2,0,7,7,5,5,4,4,2,7,7,5,5,4,4,2,0,0,7,7,9,9,7,5,5,4,4,2,2,0};
  14. int length[] = {0,4,4,4,4,4,4,8,4,4,4,4,4,4,8,4,4,4,4,4,4,8,4,4,4,4,4,4,8,4,4,4,4,4,4,8,4,4,4,4,4,4,8};
  15. int songlength = 43;
  16. int tempo = 100000;
  17.  
  18. // Variables
  19. int curnote = 0;
  20. int stepcounter = 0;
  21. int dir = 0;
  22. unsigned long time = 0;
  23. unsigned long next_step = 0;
  24. unsigned long next_note = 0;
  25. unsigned long delaycalc = 0;
  26.  
  27. // Pins
  28. int DriveEnable = 2;
  29. int Direction = 4;
  30. int Step = 3;
  31.  
  32. void setup()
  33. {
  34. pinMode(DriveEnable, OUTPUT);
  35. pinMode(Direction, OUTPUT);
  36. pinMode(Step, OUTPUT);
  37.  
  38. digitalWrite(DriveEnable, LOW);
  39. digitalWrite(Direction, HIGH);
  40. digitalWrite(Step, LOW);
  41.  
  42. // Zero the stepper
  43. for (int i = 0; i < 100; i++)
  44. {
  45. digitalWrite(Step, HIGH);
  46. delay(2);
  47. digitalWrite(Step, LOW);
  48. delay(2);
  49. }
  50.  
  51. digitalWrite(Direction, dir);
  52.  
  53. curnote = 0;
  54. next_step = micros() + 100000;
  55. next_note = next_step;
  56.  
  57. }
  58.  
  59. void loop()
  60. {
  61. time = micros();
  62.  
  63. // Step handling
  64. if (time >= next_step)
  65. {
  66. stepcounter++;
  67. if (stepcounter >= MAX_STEPS)
  68. {
  69. stepcounter = 0;
  70. dir = !dir;
  71. digitalWrite(Direction, dir);
  72. }
  73. digitalWrite(Step, HIGH);
  74. delayMicroseconds(1500);
  75. digitalWrite(Step, LOW);
  76. delaycalc = scale[note[curnote]];
  77. next_step = time + delaycalc;
  78. }
  79.  
  80. // Note handling
  81. if (time >= next_note)
  82. {
  83. curnote++;
  84. if (curnote >= songlength)
  85. {
  86. curnote = 0;
  87. delay(10000);
  88. }
  89. delaycalc = length[curnote] * 100000;
  90. delay(50);
  91. next_note = micros() + delaycalc;
  92. }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement