No delay value smoother

I tried to assemble a program which would run without delay() which failed and just looked like a dimmed light constantly on. It seemed like it didn’t have the time to run the smoothing code since it was running it so many times and that would create a visual appearance of a LED. I intended for the LED to fade in proportion to the interval which would be determined by a potentiometer. I couldn’t solve my first problem so I stayed just getting the LED to flash smoothly with time delay. I’m less interested with the proportional delay between LED smoothing interval and the actual time completely off.

I had previously written the code with for() and while() but it seemed like those caused a delay or at least one of them. Perhaps I was using them incorrectly. I’m pretty sure whatever I’m doing in this sketch is off badly considering the results it yielded.

const int ledPin = 13;
const int potPin = 7;
int potInterval = 0;
int potDuration = 0;
int fade = 0;
long preTime = 0;

void setup(){
  pinMode(ledPin, OUTPUT);
  pinMode(potPin, INPUT);
  Serial.begin(9600);
}

void loop(){
  unsigned long time = millis();
  potInterval = analogRead(potPin);    

  if(time - preTime > potInterval){
    preTime = time;

    (fade < 255);
    fade +=5;
    analogWrite(ledPin, fade);

    (fade > 255);
    fade = 0;
    fade -=5;
    analogWrite(ledPin, fade);

    Serial.println(fade);
  }
}


}

That code is nearly impossible to read. There are useless curly braces all over the place, and poor indenting everywhere.

Put each { on a new line. Put each } on its own line. Use Tools + Auto format to fix the indenting. Then, get rid of the useless curly braces, and re-format. Then, come on back.

I have done so now.

preTime = time;
    
   (fade < 255);

Even if you indent that correctly, it doesn’t make a lot of sense.

Put each { on a new line.

I have done so now.

void setup(){
void loop(){
  if(time - preTime > potInterval){

Your idea of putting each { on a new line and mine must be different, then.

Use Tools + Auto format to fix the indenting.

You don't even have a match number of open and close curly braces. I KNOW that Tools + Auto Format told you that.

Actually Auto format did not say any like that. Either way I realize my code is bad and I guess nonsense by what you tell me, could you point me in the right direction to produce the events I desired? I’m very new to using millis() as a solution to delay() and C++ in general.

Either way I realize my code is bad and I guess nonsense by what you tell me, could you point me in the right direction to produce the events I desired?

It would certainly be easier to determine if the code was doing what you want/expect if there were comments in the code.

Ignoring the mismatched braces for the moment, please explain what you think this code is doing:

  if(time - preTime > potInterval){
    preTime = time;

    (fade < 255);
    fade +=5;
    analogWrite(ledPin, fade);

    (fade > 255);
    fade = 0;
    fade -=5;
    analogWrite(ledPin, fade);

It appears as though that stuff in parentheses is supposed to have an if in front of it. If that is the case, the code after it SHOULD be in curly braces, and the semicolon on the end is wrong.

For now, change

    (fade < 255);
    fade +=5;
    analogWrite(ledPin, fade);

to

    if(fade < 255)
    {
       fade +=5;
       analogWrite(ledPin, fade);
    }

Make corresponding changes to the code that follows. Try that, and let us know what the code looks like, what the serial output looks like, and what the visual results look like.

It starts the loop and fades the LED. Serial resets the variable back to 0 and fades on the serial monitor from 0 to 255 on a +5 interval. How can I get it to roll back? To reverse after it hits 255 and to go back to 0.

Please carefully read all the words in Reply #6.

You changed some code. We can’t see how.

const int ledPin = 13;
const int potPin = 7;
int potInterval = 0;
int potDuration = 0;
int fade = 0;
long preTime = 0;

void setup()
{
  pinMode(ledPin, OUTPUT);
  pinMode(potPin, INPUT);
  Serial.begin(9600);
}

void loop()
{
  unsigned long time = millis();
  potInterval = analogRead(potPin);    

  if(time - preTime > potInterval)
  {
    preTime = time;

    if(fade < 255)
    {
      fade +=5;
      analogWrite(ledPin, fade);
    }

    Serial.println(fade);
  }
}

What happened to the code that was (incorrectly) attempting to fade back down?

    (fade > 255);
    fade = 0;
    fade -=5;
    analogWrite(ledPin, fade);

should be

    if(fade > 255)
    {
       fade = 0;
       fade -=5;
       analogWrite(ledPin, fade);
    }

I know that the following code does not work. That’s why I removed if(fade > 255).

const int ledPin = 13;
const int potPin = 7;
int potInterval = 0;
int potDuration = 0;
int fade = 0;
long preTime = 0;

void setup()
{
  pinMode(ledPin, OUTPUT);
  pinMode(potPin, INPUT);
  Serial.begin(9600);
}

void loop()
{
  unsigned long time = millis();
  potInterval = analogRead(potPin);    

  if(time - preTime > potInterval)
  {
    preTime = time;

    if(fade < 255)
    {
      fade +=5;
      analogWrite(ledPin, fade);
    }

    if(fade > 255)
    {
      fade -=5;
      analogWrite(ledPin, fade);
    }

    Serial.println(fade);
  }
}

If I’m right, when you have an if statement saying that when under 255 to climb to it by 5 and another if statement saying if at 255 to reverse that it will just stay at 255. Whereas I want it to climb to 255 and fall back to 0 after reaching it. I simply don’t know how to code that without delay(), that’s why I’m trying to figure it out.

The problem you are having, I think, is failing to handle limit conditions. You want to change the increment value when the fade value equals or exceeds the valid range. You want to apply the increment regardless of the actual value.

Create a new (global) variable, fadeIncrement, with an initial value of 5.

If fade equals or exceeds 255, change fadeIncrement to -5. If fade equals or is less than 0, change fadeIncrement to +5.

Add fadeIncrement to fade, unconditionally. Apply the fade value, unconditionally.

Thanks for your assistance, I revised the code as you recommended from what I understood. I don’t know if I didn’t do something properly and it seems to only give 0’s on serial monitor.

const int ledPin = 13;
const int potPin = 7;
int potInterval = 0;
int potDuration = 0;
int fade = 0;
int fadeIncrement = 5;
long preTime = 0;

void setup()
{
  pinMode(ledPin, OUTPUT);
  pinMode(potPin, INPUT);
  Serial.begin(9600);
}

void loop()
{
  unsigned long time = millis();
  potInterval = analogRead(potPin);    

  if(time - preTime > potInterval)
  {
    preTime = time;

    if(fade <= 0)
    {
      fadeIncrement +=5;
    }
    if(fade >= 255)
    {
      fadeIncrement -=5;
    }

    Serial.println(fade);
  }
  fade = fadeIncrement;
  analogWrite(ledPin, fade);
}
    if(fade <= 0)
    {
      fadeIncrement +=5;
    }
    if(fade >= 255)
    {
      fadeIncrement -=5;
    }

    Serial.println(fade);

So, if fade is less than or equal 0, change fadeIncrement. If fade is greater than or equal 255, change fade increment. Then, print fade. Since fade started at 0, of course it will print 0 the first time.

  fade = fadeIncrement;
  analogWrite(ledPin, fade);

If you increment, instead of set fade, fade will gradually take on different values.

Of course, that should happen INSIDE the “if it’s time” block. And, before the print of fade.

  unsigned long time = millis();
  potInterval = analogRead(potPin);    

  if(time - preTime > potInterval)

Picky, I know, but I like names like currTime and prevTime so it is clear which is which.

Oh, and setting prevTime to currTime somewhere would be useful, too.