Compare commits
2 Commits
4539554ac1
...
d6ed3d7409
Author | SHA1 | Date |
---|---|---|
dennisdebel | d6ed3d7409 | 5 years ago |
dennisdebel | 281796da8d | 5 years ago |
@ -0,0 +1,47 @@
|
|||||||
|
/*
|
||||||
|
*
|
||||||
|
* Envelope Generator (ADSR)
|
||||||
|
*
|
||||||
|
* A3 = trigger (sync) in
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
int pwmPin = 11; //define output pin, Meergranen output pin is 11
|
||||||
|
bool triggered; // logic: trigered, 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) {
|
||||||
|
|
||||||
|
//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;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,48 @@
|
|||||||
|
/*
|
||||||
|
*
|
||||||
|
* Simple (PWM) Oscillator with Trigger
|
||||||
|
*
|
||||||
|
* A2 = pitch
|
||||||
|
* A1 = note duration
|
||||||
|
* A3 = trigger in
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
bool triggered; // logic: trigered, yes or no
|
||||||
|
#define SPEAKER_PIN 11
|
||||||
|
int noteDuration = 100;
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
// set pin 11 as output
|
||||||
|
pinMode(SPEAKER_PIN, OUTPUT);
|
||||||
|
Serial.begin(9600); // debugging (see if trigger is registered)
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
int input3 = digitalRead(A3); // read analog pin 3 (trigger)
|
||||||
|
int noteDuration = analogRead(A1)/10; // read analog pin 1 (note duration)
|
||||||
|
|
||||||
|
// trigger
|
||||||
|
if(input3 && !triggered) {
|
||||||
|
Serial.println("I hear a trigger!");
|
||||||
|
Serial.println(noteDuration);
|
||||||
|
|
||||||
|
for(int i=0;i<noteDuration;i++){
|
||||||
|
digitalWrite( SPEAKER_PIN, LOW );
|
||||||
|
delayMicroseconds( analogRead(A2)*10); // wait
|
||||||
|
digitalWrite( SPEAKER_PIN, HIGH );
|
||||||
|
delayMicroseconds( analogRead(A2)*10 ); // wait
|
||||||
|
triggered = true;
|
||||||
|
delay(1); //playtime of the osc note: this numer * noteDuration milliseconds
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue