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.

49 lines
1.3 KiB
C++

/*
*
* 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;
}
}