From e0d40b7444c3e34075f9c02be58d6ea13994e7b1 Mon Sep 17 00:00:00 2001 From: Max Lehmann Date: Fri, 11 Oct 2019 16:06:05 +0200 Subject: [PATCH] white noise with a trigger --- .../5_Whitenoise_trigger-detect.ino | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 sketches/max_sketch/5_Whitenoise_trigger-detect/5_Whitenoise_trigger-detect.ino diff --git a/sketches/max_sketch/5_Whitenoise_trigger-detect/5_Whitenoise_trigger-detect.ino b/sketches/max_sketch/5_Whitenoise_trigger-detect/5_Whitenoise_trigger-detect.ino new file mode 100644 index 0000000..66c9ccb --- /dev/null +++ b/sketches/max_sketch/5_Whitenoise_trigger-detect/5_Whitenoise_trigger-detect.ino @@ -0,0 +1,43 @@ +/* + * + * Trigger Detect + * + * + * Detecting triggers is essential to interact with other modules, for example to generate short bursts of sounds + * Try the output of simple-lfo.ino with this patch and open the serial monitor to see what happens! + * + * A3 = Trigger in + * + */ + + +bool triggered; // logic: trigered, yes or no +long randNumber; +int pwmPin = 11; + +void setup() { + Serial.begin(9600); // debugging (see if trigger is registered) +} + +void loop() { + + int input3 = digitalRead(A3); // read analog pin 3 + + // trigger + if(input3==HIGH && !triggered) { //if there is a reading on input3 and the bool condition triggered is not true do + Serial.println("I hear a trigger!"); + + for(int i=0;i<2000;i++){ + randNumber = random(10, 100); + digitalWrite(pwmPin, LOW); + delayMicroseconds(randNumber); + digitalWrite(pwmPin, HIGH); + delayMicroseconds(randNumber); + } + } + 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; + } + +}