How to get LED to fade/ease in and out of color, rather than just "appear/disappear"?

edited June 2016 in Arduino

Just needed help adjusting my processing code to make the LED fade in and out of color (or ease in/out), rather than just "appear/disappear". Here is a snippet of code for when the LED on pin9 activates in response to certain sound pitch range (f) being detected:

  if(f > 175  && f < 237) {
  background(255,0,0);
  arduino.analogWrite(9, 255);
  }

cheers!

Tagged:

Answers

  • It needs to be on a PWM pin - depends on which arduino you're using but pin 9 not usually PWM.

    https://www.arduino.cc/en/Reference/AnalogWrite

  • edited June 2016

    it is on a pwm pin, I am pretty sure anyway. I ran a sample pwm sketch and the LED wored fine. I am using the Uno model. Regardless, lets just assume I am using the right pin. What would the code be?

  • edited June 2016

    This gives you a sawtooth value - just use value of LFO ie (analogWrite 9, LFO);

        // this makes a sawtooth wave
    
        float LFO = 0; // starting value
        float dLFO = 3; // delta LFO ie speed of change 
    
        void setup() {
        }
    
        void draw() { 
          LFO += dLFO; 
          if ((LFO <= 0) || (LFO >= 255)) { // if out of bounds
            dLFO = - dLFO; // swap direction
          }
          println(LFO);
    
  • Much appreciated! Going to test it out when I get home. Thank you

Sign In or Register to comment.