Can't control fade

Hello,

I tried to add control of this fader with a pot, but it doesn't work like I wanted. I commented out the first two lines in the loop and it works fine with a smooth fade. but when I include those lines the pot controls the speed of the blink, but not the fade like I hoped.

/*
  Fade

  This example shows how to fade an LED on pin 9 using the analogWrite()
  function.

  The analogWrite() function uses PWM, so if you want to change the pin you're
  using, be sure to use another PWM capable pin. On most Arduino, the PWM pins
  are identified with a "~" sign, like ~3, ~5, ~6, ~9, ~10 and ~11.

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/Fade
*/

int led = 9;           // the PWM pin the LED is attached to
int brightness = 0;    // how bright the LED is
int fadeAmount = 0;    // how many points to fade the LED by
int sensorPin = A1;
int sensorValue = 0;


// the setup routine runs once when you press reset:
void setup() {
  // declare pin 9 to be an output:
  pinMode(led, OUTPUT);
  pinMode(sensorPin, INPUT);
 
}

// the loop routine runs over and over again forever:
void loop() {
  //sensorValue = analogRead(sensorPin);
 // fadeAmount = map(sensorValue, 0, 1023, 0, 250);
  
  // set the brightness of pin 9:
  analogWrite(led, brightness);

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness <= 0 || brightness >= 255) {
    fadeAmount = -fadeAmount;
  }
  // wait for 30 milliseconds to see the dimming effect
  delay(14);
}

What blink?

What did you hope would happen and what actually happens?

You should maybe consider the values that fade can have. By mapping up to 250, it will go vary quickly to max. Maybe try 50.

I have tried narrowing the range down to 50 and way lower and that has changed the responsiveness. It still only blinks though at different speeds. The code without the analogRead() lines fades the led with a pulse as it goes through the if() lines in the loop(). I’m trying to control the fadeAmount variable with the pot.

Did you expect something else?
By the way, what is your board?

Until you change 'fadeAmount' to a value other than 0 your sketch will do nothing.

Yes, I forgot that. When the first two lines of the loop are removed that needs to be set to 5 for it to fade at medium rate. I want to be able to change that with the pot. Changing it in the code changes the rate of the fade/pulse. Using the pot changes it to a blink.

You already explained why this is happening.
Why do you set the fade to 5 without a potentiometer and 250 with a potentiometer? Your led is faded out and raised too quickly.
Set the map limit to 10

and make a delay of 30-50 ms instead of 14 - and you will see the difference

That line will un-do this line:

You should keep the fadeAmount and fadeDirection separate:

int fadeAmount = 1;    // how many points to fade the LED by
int fadeDirection = 1; 

  fadeAmount = map(sensorValue, 0, 1023, 1, 10);

  brightness += fadeAmount * fadeDirection;
  if (brightness < 0 || brightness > 255) 
  {
    fadeDirection = -fadeDirection;
    brightness = constrain(brightness, 0, 255);
  }

I’m pretty confused. Is this all in the loop or is fadeDirection declared at the beginning globally?

Yes. Just like fadeAmount.

I don't understand what I'm doing wrong. I'm reading through the loop but I'm not seeing what's wrong. This code fades up once but doesn't go back down.



int led = 9;           
int brightness = 0;    // how bright the LED is
int fadeAmount = 1; // how many points to fade the LED by
int fadeDirection = 1;
int sensorPin = A1;
int sensorValue = 0;

// the setup routine runs once when you press reset:
void setup() {
  pinMode(led, OUTPUT);
  pinMode(sensorPin, INPUT);

}

void loop() {
 sensorValue = analogRead(sensorPin);
 fadeAmount = map(sensorValue, 0, 1023, 0, 10);

 brightness += fadeAmount * fadeDirection;
  if(brightness < 0 || brightness > 255);
 {
  fadeDirection = -fadeDirection;
  brightness = constrain(brightness, 0, 255);
 }
  
  analogWrite(led, brightness);

  brightness = brightness + fadeAmount;
  if(brightness <= 0 || brightness >= 255); 
 {
    fadeAmount = -fadeAmount;
 }
   delay(30);
}

You left some old code in your loop.

That's what I thought! I removed that first, but it produced a flickering light. I messed with the parameters but couldn't smooth it out.

The situation is that I fabricated a dozen of these props with the code below injected that produced a pleasant pulse, but then I was asked to add a pot to control the tempo in case they wanted to change it on the day of filming. It seemed like an easy change, but I need to get this same fading pulse.


int led = 9;           // the PWM pin the LED is attached to
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by
int sensorPin = A1;
int sensorValue = 0;

void setup() {
  // declare pin 9 to be an output:
  pinMode(led, OUTPUT);
  pinMode(sensorPin, INPUT);
  
}

void loop() {
  
  analogWrite(led, brightness);

  brightness = brightness + fadeAmount;

  if (brightness <= 0 || brightness >= 255) {
    fadeAmount = -fadeAmount;
  }
  delay(14);
}

I think the best way to accomplish what I’m going for will be to enclose the analogWrite and if statement inside a for() loop and keep the fadeAmount at 5. Then use a second delay outside that at the end and use the pot to control that. I’ll just need to figure that out.

What doesn't work?
Have you tried as I said - make the delay bigger and change the fadeAmount not wide than from 0 to 10 ?

And you don't answer - what is your board?

I think you are right. Using the pot to adjust the delay between steps is much easier to do:

int led = 9;           // the PWM pin the LED is attached to
int brightness = 0;    // how bright the LED is
int fadeAmount = 1;    // how many points to fade the LED by
int sensorPin = A1;


// the setup routine runs once when you press reset:
void setup()
{
  // declare pin 9 to be an output:
  pinMode(led, OUTPUT);
  pinMode(sensorPin, INPUT);
}

// the loop routine runs over and over again forever:
void loop()
{
  // set the brightness of pin 9:
  analogWrite(led, brightness);

  // change the brightness for next time through the loop:
  brightness += fadeAmount;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness < 0 || brightness > 255)
  {
    fadeAmount = -fadeAmount;
    brightness = constrain(brightness, 0, 255);
  }

   // Delay from 0 to 10230 microseconds
   // From very fast to about 5 seconds per cycle
  delayMicroseconds(analogRead(sensorPin)*10); 
}