Removing Void Fade

Hi,

I found a simple cross fade two LEDs code online and it seems to be using void fade.

In Order, its using
Void Setup
Void Loop
Void Fade

Is it possible to make this not use void fade and run as code inside void loop/void setup…
As I am moving a bunch of LED Effects into Void Setup to run at startup, kind of like a demo before doing other actions in my code.

I have been using “for (int i = 0; i <= 500; i++){” to allow it to run a bunch of times before moving on…

/*
 Two Fade - Fades two LEDs independently at different rates
 Arduino Uno 
 
 Halloween 2013

 */

// NOTE: other end of LEDs are tied through 330 ohm resistor to +

int led1 = 9;           // the pin that the LED is attached to
int brightness1 = 180;  // how bright the LED is
int fadeAmount1 = 2;    // how many points to fade the LED by

int led2 = 10;          // the pin that the LED is attached to
int brightness2 = 60;   // how bright the LED is
int fadeAmount2 = 2;    // how many points to fade the LED by

// the setup routine runs once when you press reset:
void setup()  { 
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);  
} 

// the loop routine runs over and over again forever:
void loop()  { 
  analogWrite(led1, brightness1);    
  analogWrite(led2, brightness2);      

  fade(brightness1, fadeAmount1);
  fade(brightness2, fadeAmount2);  
}

void fade(int& brightness, int& fadeAmount) {
  // reverse the direction of the fading at the ends of the fade: 
  if (brightness <= (random(20,40) * 2) || brightness >= (random(110,120) * 2)) {
    fadeAmount = -fadeAmount ; 
  }     

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

  // change rate of brightness change depending on how bright
  // which allows smoother fades across all brightnesses
  if (brightness >= 200 && brightness <= 255) {
    delay(20);                            
  }
  else {
    delay(5);
  }

}

Cheers
Shaun

Hi,
I can't see why you want to avoid using "fade".

You can use it as many times as you like anywhere in your code, including setup().

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

Tom... :slight_smile:

Explain what you want the behaviour to be, explain what the existing behaviour is and why its
not what you want.

What the functions are called isn't relevant to understanding the basic issue in the first place.

For the sake of sanity and so you learn something, can you please stop referring to “Void Setup”, “Void Loop”, “Void Fade” etc

setup(), loop() and fade() are functions and you cannot even get their names right. setup is not the same as Setup. The word void in front of the function names indicates that the functions will not return a value to the code that calls them so do not form part of the name. Again you cannot even get the return type name correct. Void is not the same as void.

When programming, details matter.