/*
 * 
 * Simple sequence - warning, not very musical
 * 
 */
long randNumber; //variable to store random number
int pwmPin = 11; //define output pin, Meergranen output pin is 11

//define a sequence of beats
int pattern[][8] = { {1,2,0,2,1,1,0,1}, //multidimensional array (nested array) 8 must be there, its the number of elements in the last array
                     {1,1,1,2,1,1,0,1},
                     {1,2,0,2,0,0,2,1},
                     {1,1,2,0,1,1,0,2} 
                    };


void setup() { //default setup loop
  Serial.begin(9600);
  pinMode(pwmPin,OUTPUT); //set pwmPin to output mode (otherwise the built-in pulldown resistor sets it as input, aka you wont hear anything)
}

void snare(){ //custom function describing a snare
    for(int i=0;i<25;i++){ 
    randNumber = random(10, 100); //generate random number between 10 and 100
    digitalWrite(pwmPin, LOW); //set output pin to LOW (0v)
    delayMicroseconds(randNumber/10*(i/4)*(600/10));//wait for randomNumber (10-100 Mircroseconds (0.00001-0.0001 seconds))
    digitalWrite(pwmPin, HIGH); //set output pin to HIGH (5v)
    delayMicroseconds(randNumber/10*(i/4)*(600/10)); //wait for randomNumber (10-100 Mircroseconds (0.00001-0.0001 seconds))
  }
   for(int i=0;i<300;i++){ 
    randNumber = random(10, 100); //generate random number between 10 and 100
    digitalWrite(pwmPin, LOW); //set output pin to LOW (0v)
    delayMicroseconds(randNumber);//wait for randomNumber (10-100 Mircroseconds (0.00001-0.0001 seconds))
    digitalWrite(pwmPin, HIGH); //set output pin to HIGH (5v)
    delayMicroseconds(randNumber); //wait for randomNumber (10-100 Mircroseconds (0.00001-0.0001 seconds))
  }
   for(int i=0;i<100;i++){ 
    randNumber = random(20, 200); //generate random number between 10 and 100
    digitalWrite(pwmPin, LOW); //set output pin to LOW (0v)
    delayMicroseconds(randNumber);//wait for randomNumber (10-100 Mircroseconds (0.00001-0.0001 seconds))
    digitalWrite(pwmPin, HIGH); //set output pin to HIGH (5v)
    delayMicroseconds(randNumber); //wait for randomNumber (10-100 Mircroseconds (0.00001-0.0001 seconds))
  }
  for(int i=0;i<200;i++){ 
    randNumber = random(10, 100); //generate random number between 10 and 100
    digitalWrite(pwmPin, LOW); //set output pin to LOW (0v)
    delayMicroseconds(randNumber);//wait for randomNumber (10-100 Mircroseconds (0.00001-0.0001 seconds))
    digitalWrite(pwmPin, HIGH); //set output pin to HIGH (5v)
    delayMicroseconds(randNumber); //wait for randomNumber (10-100 Mircroseconds (0.00001-0.0001 seconds))
  }
}

void kick(){ //custom function describing a kick
  
//ATTACK
     for(int i=0;i<7;i++){ // i = DELAY+SUSTAIN+RELEASE of ATTACK
       analogWrite(11,0);
      delayMicroseconds(1*i*i); // lower the pitch over time
       analogWrite(11,255);
      delayMicroseconds(1*i*i); // lower the pitch over time
       analogWrite(11,0);
      }

//SUSTAIN RELEASE
     for(int i=0;i<20;i++){ // i = DELAY+SUSTAIN+RELEASE  < this should be linked to lfo..
       analogWrite(11,0);
      delayMicroseconds(analogRead(A1)*i); // lower the pitch over time
       analogWrite(11,255);
      delayMicroseconds(analogRead(A1)*i); // lower the pitch over time
       analogWrite(11,0);
      delay(1);
      }

     
}

void loop() { // main loop!

int preset = map(analogRead(A0), 0, 1024, 0,4); //map analog read pot 0 to 0-4 value of our presets
Serial.println(preset);

for (byte i = 0; i < 7; i++) { //for the lenght of the pattern do:
 
  if(pattern[preset][i]==1){
    kick();
    delay(analogRead(A2)); //rest
    }
  if(pattern[preset][i]==2){
    snare();
    delay(analogRead(A2)); //rest
   }
  if(pattern[preset][i]==0){
    delay(analogRead(A2)*2); //rest
   }
}


delay(analogRead(A2)); //main tempo

}
