/*
 * 
 * Trigger Delay
 * 
 * 
 * Delaying triggers is a way to create simple sequences (kick after snare for example) or to reduce the frequency of a fast incomming trigger
 * Try the output of simple-lfo.ino with this patch and open the serial monitor to see what happens!
 * 
 * A3 = Trigger in
 * 
 */

unsigned long time; // for debugging/indicating delay
bool triggered; // logic: trigered, yes or no

int pwmPin = 11; //define output pin, Meergranen output pin is 11

void setup() {
  Serial.begin(9600); // debugging (see if trigger is registered)
    pinMode(pwmPin,OUTPUT);
}

void loop() {
  
  int input3 = digitalRead(A3); // read analog pin 3
  
  //  trigger 
  if(input3 && !triggered) { //if there is a reading on input3 and the bool condition triggered is not true do
    // DO SOMETHING ON NORMAL TRIGGER HERE
     Serial.println("I hear a trigger!");
     Serial.println("-----------------"); //print pretty line
     digitalWrite(pwmPin, LOW); //set output pin to LOW (0v)  
     delay(analogRead(A2)); // DLEAY THE INCOMING TRIGGER (wait for amount set by analog 2 (first potentiometer on meergranen))
     digitalWrite(pwmPin, HIGH); //set output pin to HIGH (5v)
      // DO SOMETHING ON DELAYED TRIGGER HERE:
     Serial.println("delayed trigger with"); // print some info about the delayed trigger
     Serial.print(" ");
     Serial.print(analogRead(A2));
     Serial.print(" ");
     Serial.print("milliseconds");
     Serial.println(" ");
     Serial.println(" ");
           
      triggered = true;
    }
    else if(!input3 && triggered) { //if there is no reading on input3 and condition triggered is true (aka sound is playing), set triggered to false, aka stop playing 
      // STOP WHEN NO TRIGGER IS PRESENT (or do something else ;)
      triggered = false;
    }

}
