fading led for time amount

Hi,
I want to fade an LED/s on & off for say 10-40 secs

then switches off

After that another led switches on

int ledPin = 9;    // LED connected to digital pin 9

void setup()  { 
  // nothing happens in setup 
pinMode(3, OUTPUT); //added this code
} 

void loop()  { 
  // fade in from min to max in increments of 5 points:
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 

  // fade out from max to min in increments of 5 points:
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 
}

After this fading goes for 10-40 secs it switches off then turns on another LED

that's all

I want to fade an LED/s on & off for say 10-40 secs

Permission granted.

Why not make the fading for loop into a function and pass it the pin number to use. Once it is finished doing say pin 9 pass it another pin number.

Each loop takes about 1.5 seconds (255/5*30=1530 mSec) so about 3 seconds for a full cycle. Run that cycle 10 to 13 times (30 to 39 seconds). Repeat with the other LED.

thanks for the replies

well I'm stumped

I actually repeated the cycle but for some reason the led stops dimming half way through after the code runs through

and the other LED doesn't lite up to full brightness either

could there be some sort of 'bug' or limitation for the Arduino (UNO R3) ?

If only we could see the program you have the problem with ........

yeah ok don't laugh I thought this would do though..for starters

int ledPin = 3;    // LED connected to digital pin 3
int ledPin2 = 5;    // LED connected to digital pin 5


void setup()  { 
  
    
   // fade in from min to max in increments of 5 points:
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=8) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 

  // fade out from max to min in increments of 5 points:
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=44) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 
  
   // fade in from min to max in increments of 5 points:
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=8) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 

  // fade out from max to min in increments of 5 points:
  for(int fadeValue = 255 ; fadeValue >= ; fadeValue -=44) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 
   // fade in from min to max in increments of 5 points:
  for(int fadeValue = 1 ; fadeValue <= 255; fadeValue +=8) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 

  // fade out from max to min in increments of 5 points:
  for(int fadeValue = 255 ; fadeValue >= 1; fadeValue -=44) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 
   // fade in from min to max in increments of 5 points:
  for(int fadeValue = 1 ; fadeValue <= 255; fadeValue +=8) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 

  // fade out from max to min in increments of 5 points:
  for(int fadeValue = 255 ; fadeValue >= 1; fadeValue -=44) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 
   // fade in from min to max in increments of 5 points:
  for(int fadeValue = 1 ; fadeValue <= 255; fadeValue +=8) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 

  // fade out from max to min in increments of 5 points:
  for(int fadeValue = 255 ; fadeValue >= 1; fadeValue -=44) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 
   // fade in from min to max in increments of 5 points:
  for(int fadeValue = 1 ; fadeValue <= 255; fadeValue +=8) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 

  // fade out from max to min in increments of 5 points:
  for(int fadeValue = 255 ; fadeValue >= 1; fadeValue -=44) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 
   // fade in from min to max in increments of 5 points:
  for(int fadeValue = 1 ; fadeValue <= 255; fadeValue +=8) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 

  // fade out from max to min in increments of 5 points:
  for(int fadeValue = 255 ; fadeValue >= 1; fadeValue -=44) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 
  
   // fade in from min to max in increments of 5 points:
  for(int fadeValue = 1 ; fadeValue <= 255; fadeValue +=8) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 

  // fade out from max to min in increments of 5 points:
  for(int fadeValue = 255 ; fadeValue >= 1; fadeValue -=44) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 
  
   // fade in from min to max in increments of 5 points:
  for(int fadeValue = 1 ; fadeValue <= 255; fadeValue +=8) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 

  // fade out from max to min in increments of 5 points:
  for(int fadeValue = 255 ; fadeValue >= 1; fadeValue -=44) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 
  
   // fade in from min to max in increments of 5 points:
  for(int fadeValue = 1 ; fadeValue <= 255; fadeValue +=8) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 

  // fade out from max to min in increments of 5 points:
  for(int fadeValue = 255 ; fadeValue >= 1; fadeValue -=44) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 
  
   // fade in from min to max in increments of 5 points:
  for(int fadeValue = 1 ; fadeValue <= 255; fadeValue +=8) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 

  // fade out from max to min in increments of 5 points:
  for(int fadeValue = 255 ; fadeValue >= 1; fadeValue -=44) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 
  
   // fade in from min to max in increments of 5 points:
  for(int fadeValue = 1 ; fadeValue <= 255; fadeValue +=8) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 

  // fade out from max to min in increments of 5 points:
  for(int fadeValue = 255 ; fadeValue >= 1; fadeValue -=44) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 
  
  
  
  {analogWrite(ledPin, LOW);}
  //{analogWrite(ledPin2, LOW);} //this is for blue leds on always
  {analogWrite(ledPin2, HIGH);} //this is for blue leds on always
    
  
  
} 

void loop()  { 
  
}

I'm not laughing but I did get bored reading

  // fade in from min to max in increments of 5 points:
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=8) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 

  // fade out from max to min in increments of 5 points:
  for(int fadeValue = 255 ; fadeValue >= ; fadeValue -=44) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  }

12 times, or was it 11 or maybe 13.
Read up on and use a for loop to run that block of code several times.
Making the comments match the code is always a good idea too to help with understanding it.

As to your problem. Print the value of fadeValue during the fades. Is the value what you expect ?

How is the LED wired ?

Adding a pinMode() command to the code could be helpful.

sorry for that, yeah my code sucks

I will try your suggestions, but it will take a while for me to catch up

the LED is with a 180k resistor in series

thanks for the suggestions I better get to work

cheers

the LED is with a 180k resistor in series

Do you really mean that ?

bah ,

180 Ohms

Hey, You didn't use pinMode(); command to declare pins. How is it working?

I just added it to the code above

there is no difference

I'm still trying to figure how to add a counting method & use it in void loop

I just added it to the code above

I have lost track of what code you are referring to. Please can you post the complete program as it is now ?

int ledPin = 3;    // LED connected to digital pin 3
int ledPin2 = 5;    // LED connected to digital pin 5
void setup()  { 
  // nothing happens in setup 
pinMode(3, OUTPUT); //added this code
pinMode(5, OUTPUT);
} 

void loop()  { 
  // fade in from min to max in increments of 5 points:
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 

  // fade out from max to min in increments of 5 points:
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 


for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 

  // fade out from max to min in increments of 5 points:
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 

for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 

  // fade out from max to min in increments of 5 points:
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 

  // fade out from max to min in increments of 5 points:
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 

  // fade out from max to min in increments of 5 points:
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 

  // fade out from max to min in increments of 5 points:
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 

  // fade out from max to min in increments of 5 points:
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 


 {analogWrite(ledPin, LOW);}
 {analogWrite(ledPin2, LOW);} //this is for blue leds on always
 {analogWrite(ledPin2, HIGH);} //this is for blue leds on always
    


}

then the program exits the loop and lights an LED
I'm trying to figure that part out

then the program exits the loop and lights an LED

Should the fades repeat or happen only once ?

 {analogWrite(ledPin2, LOW);} //this is for blue leds on always
 {analogWrite(ledPin2, HIGH);} //this is for blue leds on always

Do you really mean to turn off ledPin2 then immediately afterwards turn it on ? Why are the braces there ?

In your program suggest that the first thing to do is to tidy up the fading code. Maybe something like this

const byte ledPins[] = {10, 11};
const byte numLeds = sizeof(ledPins) / sizeof(ledPins[0]);

void setup()
{
  Serial.begin(115200);
  for (int led = 0; led < numLeds; led++)
  {
    pinMode(ledPins[led], OUTPUT);
  }
}

void loop()
{
  for (int led = 0; led < numLeds; led++)
  {
    showLed(led);
  }
}

void showLed(int led)
{
  for (int level = 0; level <= 255; level +=5)
  {
    Serial.println(level);
    analogWrite(ledPins[led], level);
    delay(100);
  }

  for (int level = 255; level >= 0; level -=5)
  {
    analogWrite(ledPins[led], level);
    delay(10);
  }
}

It would be even better without delay()s but you can fix that later.

sorry I've taken a while to reply

Should the fades repeat or happen only once ?

the code runs once as you see it but there should be more lines
the problem is that posting the code in the forum will not allow more lines of code

but that looks like garbage anyways after seeing proper coding which I will now attempt
as per UKHeliBob's suggestion

Do you really mean to turn off ledPin2 then immediately afterwards turn it on ? Why are the braces there ?

for some reason the Arduino doesn't switch off the leds after the code is completed

So I put in those codes to force the hardware to shut off the leds

I will now work on tidying it all up as suggested

thanks UKHeliBob and everyone

this may take some time for me to learn this proper way to code

Cheers :slight_smile: