Why can´t I use "NoDelay" when dim and bright a LED?

I want a led to dim up and down in different intervall according to a potentiometer.
I have a question about delay´s vs Nodelay. I use a Arduino Nano.

If I use delay I can manage the delay time with a potentiometer:

const int ledPin = 10;
int a = 0;

void setup() {
pinMode (ledPin, OUTPUT) ;
}
void loop() {

int x = 1;
for (int i = 0; i > -1; i = i + x){
analogWrite(ledPin, i);
if (i == 255) x = -1;
delay(a);
a= analogRead(2)/70;
}}

But when I try to use the " Nodelay" I can’t get it to work:

const int ledPin = 10;
long Time1 = 0;
long Time2 = 0;
int a = 0;
void setup() {

pinMode (ledPin, OUTPUT) ;
}
void loop() {
unsigned long Time1 = millis();
if (( Time1 - Time2) > a) {
int x = 1;
for (int i = 0; i > -1; i = i + x) {
analogWrite(ledPin, i);
if (i == 255) x = -1;
a=analogRead(2);
Time2 = Time1;
}
}
}

Can anyone help me to save hours of troubleshooting to better use :-)?

Kent

The for loop controls the fading. But the time variables don’t ever change inside the loop. So it runs at lightning speed.

I hate to put it this way, but the way you are doing it is completely the wrong approach. There is no way to make it work in that fashion.

Perhaps you already have an inkling of this.

The answer is to use only “if” statements inside the loop, “for” is poison unless you expect the loop to finish its work much faster than the time delays that you are implementing.

Try

/* Blink without Delay

 Turns on and off a light emitting diode (LED) connected to a digital
 pin, without using the delay() function.  This means that other code
 can run at the same time without being interrupted by the LED code.

 The circuit:
 * LED attached from pin 13 to ground.
 * Note: on most Arduinos, there is already an LED on the board
 that's attached to pin 13, so no hardware is needed for this example.

 created 2005
 by David A. Mellis
 modified 8 Feb 2010
 by Paul Stoffregen
 modified 11 Nov 2013
 by Scott Fitzgerald


 This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
 */

// constants won't change. Used here to set a pin number :
int ledPin =  10;      // the number of the LED pin
boolean goingUp = 0;
// Variables will change :
int ledState = 0;             // ledState used to set the LED

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change :
long interval = 10;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the
  // difference between the current time and last time you blinked
  // the LED is bigger than the interval at which you want to
  // blink the LED.
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    if (goingUp) {
      if (ledState < 255) {
        ledState++;
      }
      else
      {
        ledState = 0;
        goingUp = 0;
      }
    }
    else {
      if (ledState > 0) {
        ledState--;
      }
      else {
        goingUp = 1;
      }
    }

    // save the last time you blinked the LED
    previousMillis = currentMillis;
    // set the LED with the ledState of the variable:
    analogWrite(ledPin, ledState);
  }
}

The demo several things at a time is an extended example of the BWoD technique for using millis() to manage timing.

Try this (not tested)

void loop() {
	unsigned long Time1 = millis();
	if ( Time1 - Time2 > a) {
		static int i = 0;
		static int x = 1;
		analogWrite(ledPin, i); 
		i = i + x;    
		if (i > 255) || (i < 0) {
			x = -x;
		}             
		a = analogRead(2); // this seems irrelevant
		Time2 = Time1;
	}
}

Tanks! Solved my problem, even if I did´t got your code to work Robin2.

Kejjonsson:
even if I did´t got your code to work Robin2.

I did say I had not tested it. What happened when you tried it ?

…R

Couldn´t get it to work:

Have some problem with this line:if (i > 255) || (i < 0)

“expected primary-expression before ‘||’ token”

My knowledge is still to weak on this subject :slight_smile:
/K

Kejjonsson:
if (i > 255) || (i < 0)

Needs a bracket round both of those inequalities. I think you can even lose the inner ones.

if ((i > 255) || (i < 0))
if (i > 255 || i < 0)

Kejjonsson:
Have some problem with this line:if (i > 255) || (i < 0)

Sorry - I should not have had the second (

Anyway, it is a good experience to learn to check for properly paired brackets. :slight_smile:

…R

Thanks again, It´s easy to fix when you know how it´s work! Hope I will be there soon!! :slight_smile:

Kejjonsson:
It´s easy to fix when you know how it´s work!

I understand that. But now you do know.

...R