Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Anfangdes Arduino-Programms
- const byte BUTTONPIN = 2;
- const byte SPEAKERPIN = 10;
- const byte MELODIES = 100;
- ///////////////////
- //
- /////////////////////
- void setup()
- {
- // put your setup code here, to run once:
- Serial.begin(115200);
- // Serial.println("I\nprogram is up and running...");
- pinMode(BUTTONPIN, INPUT_PULLUP);
- pinMode(SPEAKERPIN, OUTPUT);
- jursTone(SPEAKERPIN, 440, 600);
- }
- unsigned long startTime = millis();
- int jursSteps = 0;
- int jursreturn = 0;
- bool firstround = true;
- unsigned long firstjurstiming = micros();
- bool test = true;
- /////////////////////////////////////////////////////////////////////////////////////
- //ORIGINAL
- /////////////////////////////////////////////////////////////////////////////////////
- void jursTone(const byte pin,const int frequency, const int duration)
- {
- unsigned long startTime=millis();
- int halfPeriod=1000000L/frequency/2;
- while(millis()-startTime<duration)
- {
- digitalWrite(SPEAKERPIN,HIGH);
- delayMicroseconds(halfPeriod);
- digitalWrite(SPEAKERPIN,LOW);
- delayMicroseconds(halfPeriod);
- }
- }
- //////////////////////////////////////////////////////////////////////////////////////
- //Versuch ohne delay
- //////////////////////////////////////////////////////////////////////////////////////
- byte mypin;
- int myfrequency;
- int myduration;
- int jursTone(const byte pin, const int frequency, const int duration)
- {
- if (firstround) {
- startTime = millis();
- mypin=pin;
- myfrequency=frequency;
- myduration=duration;
- firstround = false;
- jursreturn = 0;
- jursSteps=0;
- }
- Serial.println(jursSteps);
- unsigned long halfPeriod = 1000000UL / myfrequency / 2UL;
- if (millis() - startTime < myduration || jursSteps != 0) {
- switch (jursSteps) {
- case 0:
- digitalWrite(SPEAKERPIN, HIGH);
- jursSteps++;
- firstjurstiming = micros();
- break;
- case 1:
- if ((micros() - firstjurstiming) > (unsigned long)halfPeriod)
- jursSteps++;
- break;
- case 2:
- digitalWrite(SPEAKERPIN, LOW);
- jursSteps++;
- firstjurstiming = micros();
- break;
- case 3:
- if (micros() - firstjurstiming > (unsigned long) halfPeriod)
- jursSteps++;
- break;
- case 4:
- //jursreturn = 0;
- jursSteps = 0;
- break;
- }
- } else {
- jursreturn = 1;
- }
- Serial.println(jursSteps);
- return jursreturn;
- }
- uint16_t liedcount;
- uint16_t liedende;
- uint8_t liednr = 0;
- byte lspeed = 0;
- uint8_t dauer;
- uint8_t hoehe;
- uint8_t note ;
- void playlied(int liednr)
- {
- liednr = liednr;
- // Serial.print("Melodie Nr.: ");Serial.println(liednr);
- if (liednr == MELODIES) liedende = sizeof(lied);
- //bei letztem Lied ist Endwert Größe des Arrays
- else liedende = liedofs[liednr]; //ansonsten 1 Byte vor nächstem Lied
- //Serial.print(liedofs[liednr - 1]); Serial.print(" "); Serial.println(liedende);
- // liedcount=liedofs[liednr-1];
- lspeed = pgm_read_byte(&lied[liedcount]);
- // Serial.print("Speed: ");Serial.print(lspeed);Serial.println();
- liedcount = liedofs[liednr - 1] + 1;
- }
- boolean playSong = false;
- int steps = 0;
- unsigned long lastloopmillis = millis();
- void updatelied()
- {
- if (liedcount < liedende && playSong)
- {
- int thisdelay = 0;
- if (hoehe) thisdelay = int(dauer) * int(lspeed) * int(20); else lspeed * dauer * 20;
- note = pgm_read_byte(&lied[liedcount]); //Note holen
- hoehe = note & 0x1f; //Tonhöhe ausmaskieren (5 Bit)
- dauer = pgm_read_byte(&duration[note >> 5]); // 5 Bit nach rechts schieben und Zeit holen
- switch (steps) {
- case 0: lastloopmillis = millis(); if (hoehe) steps = steps + jursTone(SPEAKERPIN, pgm_read_word(&ton[hoehe - 1]), dauer * lspeed * 20); break;
- case 1: if (millis() - lastloopmillis > thisdelay) steps++; break;
- case 2: liedcount++; steps = 0; firstround = true; break;
- }
- } else playSong = false;
- }
- byte b, liedNummer;
- bool buttonPressed()
- {
- static byte oldState;
- byte newState = digitalRead(BUTTONPIN);
- if (newState != oldState && newState == LOW)
- {
- oldState = newState;
- return true;
- }
- oldState = newState;
- return false;
- }
- void loop()
- {
- updatelied();
- if (buttonPressed())
- {
- randomSeed(micros());
- playlied(1 + random(100));
- playSong = true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment