diff --git a/examples/simple-pwm-led-audio/pwm-led-audio.ino b/examples/simple-pwm-led-audio/pwm-led-audio.ino index be4b143..4d74fc8 100644 --- a/examples/simple-pwm-led-audio/pwm-led-audio.ino +++ b/examples/simple-pwm-led-audio/pwm-led-audio.ino @@ -1,6 +1,7 @@ /* LED PWM Uses 2 pots to control the duty cycle of a Pulse Width Modulation + with resulting values used to set both the LED and audio signal <---- pot 0 ----> <---- pot 0 ----> | | | @@ -12,7 +13,6 @@ #define LED (13) #define POT_1 (0) #define POT_2 (1) -#define POT_3 (2) #define SPEAKER (11) float delay_on; @@ -24,7 +24,7 @@ void setup() { // Pin 13 has an LED connected on the nano board: pinMode(LED, OUTPUT); // initialize Pin 11 as audio out - pinMode(LED, SPEAKER); + pinMode(SPEAKER, OUTPUT); } void loop() { @@ -38,18 +38,11 @@ void loop() { delay_on = 1023 - analogRead(POT_1); delay_off = 1023 - analogRead(POT_2); - // We also read pot 3 value that we will use to control a bit - // the delay to reach audible range - //delay_div = 1023 - analogRead(POT_3); - // - // But meergranen 1.0 - delay_div = analogRead(POT_3); - // Parameters for the PWM digitalWrite(LED, HIGH); // turn the LED on - digitalWrite(SPEAKER, HIGH); // turn the LED on - delay(delay_on/delay_div); // wait for delay_on ms + analogWrite(SPEAKER, 255); // set max audio signal + delay(delay_on); // wait for delay_on ms digitalWrite(LED, LOW); // turn the LED off - digitalWrite(SPEAKER, LOW); // turn the LED off - delay(delay_off/delay_div); // wait for delay_off ms + analogWrite(SPEAKER, 0); // audio flat line + delay(delay_off); // wait for delay_off ms } diff --git a/examples/simple-pwm-led/pwm-led.ino b/examples/simple-pwm-led/pwm-led.ino new file mode 100644 index 0000000..5b01adf --- /dev/null +++ b/examples/simple-pwm-led/pwm-led.ino @@ -0,0 +1,41 @@ +/* + LED PWM + Uses 2 pots to control the duty cycle of a Pulse Width Modulation + + <---- pot 0 ----> <---- pot 0 ----> + | | | + | | | + | | | + <-- pot 1 --> <--- ... +*/ + +#define LED (13) +#define POT_1 (0) +#define POT_2 (1) + +int delay_on; +int delay_off; + +void setup() { + // initialize the digital pin as an output. + // Pin 13 has an LED connected on the nano board: + pinMode(LED, OUTPUT); +} + +void loop() { + // We read the value of the pots at the beginning of the loop + // and store them as integer variables (declared at the top) + //delay_on = analogRead(POT_1); + //delay_off = analogRead(POT_2); + // + // But in meergranen PCB 1.0, the pots are wired the wrong way + // So we need to adjust that :) + delay_on = 1023 - analogRead(POT_1); + delay_off = 1023 - analogRead(POT_2); + + // Parameters for the PWM + digitalWrite(LED, HIGH); // turn the LED on + delay(delay_on); // wait for delay_on ms + digitalWrite(LED, LOW); // turn the LED off + delay(delay_off); // wait for delay_off ms +}