Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Floppy Drive Music: Twinkle Twinkle Little Star
- Lapsus Antepedis
- Dec 25 2011
- Times are in microseconds
- */
- // Constants
- const int MAX_STEPS = 70;
- int scale[20] = {7644,7214,6812,6428,6068,5728,5406,5102,4816,4546,4290,4048,3824,3608,3404,3214,3034,2864,2704,2550};
- // Song, extra 0 at the start.
- 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};
- 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};
- int songlength = 43;
- int tempo = 100000;
- // Variables
- int curnote = 0;
- int stepcounter = 0;
- int dir = 0;
- unsigned long time = 0;
- unsigned long next_step = 0;
- unsigned long next_note = 0;
- unsigned long delaycalc = 0;
- // Pins
- int DriveEnable = 2;
- int Direction = 4;
- int Step = 3;
- void setup()
- {
- pinMode(DriveEnable, OUTPUT);
- pinMode(Direction, OUTPUT);
- pinMode(Step, OUTPUT);
- digitalWrite(DriveEnable, LOW);
- digitalWrite(Direction, HIGH);
- digitalWrite(Step, LOW);
- // Zero the stepper
- for (int i = 0; i < 100; i++)
- {
- digitalWrite(Step, HIGH);
- delay(2);
- digitalWrite(Step, LOW);
- delay(2);
- }
- digitalWrite(Direction, dir);
- curnote = 0;
- next_step = micros() + 100000;
- next_note = next_step;
- }
- void loop()
- {
- time = micros();
- // Step handling
- if (time >= next_step)
- {
- stepcounter++;
- if (stepcounter >= MAX_STEPS)
- {
- stepcounter = 0;
- dir = !dir;
- digitalWrite(Direction, dir);
- }
- digitalWrite(Step, HIGH);
- delayMicroseconds(1500);
- digitalWrite(Step, LOW);
- delaycalc = scale[note[curnote]];
- next_step = time + delaycalc;
- }
- // Note handling
- if (time >= next_note)
- {
- curnote++;
- if (curnote >= songlength)
- {
- curnote = 0;
- delay(10000);
- }
- delaycalc = length[curnote] * 100000;
- delay(50);
- next_note = micros() + delaycalc;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement