LUNA MOD ARDUINO
Autor: calebML | Publicado: 2016-03-24T14:39:00-07:00

Link para a versão arduino do Luna Mod Looper!

Arduino Luna Mod Looper


/*
*
* LunaMod for Arduino &amp; Attiny45
* Remix by Rob Miles
* Tacoma, WA August 8th 2011
*
* I saw the original project in Make vol. 26
* by Brian McNamara
* Brian's was running on a PicAxe and I only have attiny45s so.......
*
* The freqout section where the real magic happens is from Paul Badger's synth code on the main Arduino site
*
* I kept this pretty straight forward but with an Arduino this could get a lot more complicated if you like
* Even on an attiny45 if you use you add a button to the led pin you coud sneak in more effects
*
*/

#define frequencyPot &nbsp; 0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//frequency pot tied to pin 15 which is A1
#define tempoPot &nbsp; &nbsp; &nbsp; 1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//tempo pot tied to pin 16 which is A2
#define buttonPin &nbsp; &nbsp; &nbsp;9 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//programming button tied to pin 17 which is A3
#define ledPin &nbsp; &nbsp; &nbsp; &nbsp; 10 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //status led tied to pin 18 which is A4
#define speakerPin &nbsp; &nbsp; 11 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //speaker or output pin 19 whish is A5
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //if you use a speaker it should be at least a 16 ohm speaker an should have a
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //resistor, maybe 200ohm to 1K ohm, between the negative lead and ground.
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //a potentiometer would be even better.

int currentStep = 0; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//this is just to track which tone in memory we are currently playing

int steps[] = {500,500,100,100,100,100,100,100, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// this is our tone storage areae
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;100,100,100,100,100,100,100,100, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //I used 64 tones or 8 tones per beat
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;500,500,100,100,100,100,100,100, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //you can change these manually and experiment if you like
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;100,100,100,100,100,100,100,100, &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;500,500,100,100,100,100,100,100,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;100,100,100,100,100,100,100,100,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;500,500,100,100,100,100,100,100,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;100,100,100,100,100,100,100,100};

int tempo = 0; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//tempo or speed between tones
int duration = 0; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //how long each of the 64 tones plays
int frequency = 0; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//current tone
int pitchval = 1;

void setup() &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//set up your pins....
{
&nbsp; pinMode (frequencyPot, INPUT);
&nbsp; pinMode (tempoPot, INPUT);
&nbsp; pinMode (buttonPin, INPUT);
&nbsp; digitalWrite(buttonPin, HIGH);

&nbsp; pinMode (ledPin, OUTPUT); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; pinMode (speakerPin, OUTPUT);

}

void loop()
{


&nbsp; for (int i=0; i&lt;63; i++) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //64 individual notes played
&nbsp; {
&nbsp; 
&nbsp; currentStep = i; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //save our current position in the loop for later
&nbsp; 
&nbsp; if (i == 0 || i == 16 || i == 32 || i == 48){ &nbsp; &nbsp;//keep track of the beat on the led
&nbsp; digitalWrite(ledPin, HIGH);}

&nbsp; if (i == 7 || i == 23 || i == 39 || i == 55){ &nbsp; &nbsp;//keep track of the beat on the led
&nbsp; digitalWrite(ledPin, LOW);}




&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; if (digitalRead(buttonPin) == LOW) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //is the program button being pressed
&nbsp; &nbsp; { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//if so lets write a new tone the this location
&nbsp; &nbsp; 
&nbsp; &nbsp; steps[currentStep] = (analogRead(frequencyPot)); &nbsp;//read the frequency pot and set the new tone
&nbsp; &nbsp; freqout (steps[currentStep], duration); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //set the parameters for frequout below and play it
&nbsp; &nbsp; freqout (steps[currentStep]+64, duration); &nbsp; &nbsp; &nbsp; //play another tone a little bit different than the original to give
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //it a little more depth. this can be changed to your liking
&nbsp; &nbsp; freqout (steps[currentStep]+128, duration); &nbsp; &nbsp; &nbsp; //play another tone a little bit different than the original to give
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //it a little more depth. this can be changed to your liking

&nbsp; &nbsp; }
&nbsp; 
&nbsp; &nbsp; else { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//else play the tone
&nbsp; &nbsp; 
&nbsp; &nbsp; freqout (steps[currentStep], duration); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //set the parameters for frequout below and play it
&nbsp; &nbsp; freqout (steps[currentStep]+64, duration); &nbsp; &nbsp; &nbsp; //play another tone a little bit different than the original to give
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //it a little more depth. this can be changed to your liking
&nbsp; &nbsp; freqout (steps[currentStep]+128, duration); &nbsp; &nbsp; &nbsp; //play another tone a little bit different than the original to give
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //it a little more depth. this can be changed to your liking

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; }
&nbsp; &nbsp; tempo = (analogRead(tempoPot)/4); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//read the tempo pot
&nbsp; &nbsp; duration = tempo/8; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //set the individual tone durations
&nbsp; &nbsp; delay(tempo); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //wait a bit
}
}


void freqout(int freq, int t)
{
&nbsp; int hperiod; &nbsp;
&nbsp; long cycles, i;

&nbsp; hperiod = (500000 / ((freq - 7) * pitchval)); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 

&nbsp; cycles = ((long)freq * (long)t) / 1000; &nbsp;

&nbsp; for (i=0; i&lt;= cycles; i++)
&nbsp; {
&nbsp; &nbsp; digitalWrite(speakerPin, HIGH);
&nbsp; &nbsp; delayMicroseconds(hperiod);
&nbsp; &nbsp; digitalWrite(speakerPin, LOW);
&nbsp; &nbsp; delayMicroseconds(hperiod - 1);
&nbsp; }
}