You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.3 KiB
C++
48 lines
1.3 KiB
C++
/*
|
|
*
|
|
* Envelope Generator (ADSR)
|
|
*
|
|
* A3 = trigger (sync) in
|
|
*
|
|
*/
|
|
|
|
int pwmPin = 11; //define output pin, Meergranen output pin is 11
|
|
bool triggered; // logic: triggered, yes or no
|
|
|
|
void setup() {
|
|
pinMode(pwmPin, OUTPUT); //set pin as output
|
|
Serial.begin(9600); // debugging (see if trigger is registered)
|
|
}
|
|
|
|
void loop() { // run forever
|
|
int input3 = digitalRead(A3); // read analog pin 3, detect triggers
|
|
|
|
if(input3 && !triggered) {
|
|
Serial.println("trigger!");
|
|
//ATTACK
|
|
for(int value = 0; value<=255; value++){ // cycle through 255 values, start at 0. 8 bit output == 255 values
|
|
analogWrite(pwmPin, value); // analogWrite simulates smooth sinewave using complex pwm duty cycles
|
|
//DELAY
|
|
delay(1);
|
|
}
|
|
|
|
//SUSTAIN
|
|
for(int value = 255; value>=0; value--){ // cycle through 255 values, start at 0. 8 bit output == 255 values
|
|
analogWrite(pwmPin, value); // analogWrite simulates smooth sinewave using complex pwm duty cycles
|
|
delay(2);
|
|
}
|
|
|
|
//RELEASE
|
|
delay(20);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
}
|