Fade an LED on

Hi everyone. I need an LED to fade while turning on. I’ve tried using

/*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.

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

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

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

// 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 = 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(30);
}

But it fades it on and off. I can’t figure out how to make it fade on and then continue to the rest of the code. Thanks in advance.

    fadeAmount = -fadeAmount;What do you suppose that this line does ?

In addition, don't forget that the loop() function does what its name suggests.

I have tried commenting that line out and then it repeats to having the led off and dimming back on. I know it loops but how can I integrate it into my code. I’m trying to have an amazon alexa swtich on and off a nightlight via a MOSFET and I’m using an led to test. Here my code.

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiUdp.h>
#include <functional>
#include "switch.h"
#include "UpnpBroadcastResponder.h"
#include "CallbackFunction.h"

// prototypes
boolean connectWifi();

//on/off callbacks 
void Relay1On();
void Relay1Off();
void Relay2On();
void Relay2Off();
void Relay3On();
void Relay3Off();
void Relay4On();
void Relay4Off();
void Relay5On();
void Relay5Off();


// Change this before you flash
const char* ssid = "Parker 2.4Ghz (Don't Use)";
const char* password = "freshwind726";

boolean wifiConnected = false;

UpnpBroadcastResponder upnpBroadcastResponder;

Switch *relay1 = NULL;
Switch *relay2 = NULL;
Switch *relay3 = NULL;
Switch *relay4 = NULL;
Switch *relay5 = NULL;

int fadeValue = 0;
void setup()
{
 pinMode (D7, OUTPUT); //set pins
 pinMode (D2, OUTPUT);
 pinMode (D3, OUTPUT);
 pinMode (D4, OUTPUT);
  pinMode (D8, OUTPUT);
  
 digitalWrite (D7,LOW); //turn everything off for starters
 digitalWrite (D2,HIGH);
 digitalWrite (D3,HIGH);
 digitalWrite (D4,HIGH);
 digitalWrite (D8,LOW);
 
 Serial.begin(9600);
   
  // Initialise wifi connection
  wifiConnected = connectWifi();
  
  if(wifiConnected){
    upnpBroadcastResponder.beginUdpMulticast();
    
    // Define your switches here. Max 14
    // Format: Alexa invocation name, local port no, on callback, off callback
    relay1 = new Switch("Computer", 80, Relay1On, Relay1Off);
    relay2 = new Switch("Router", 81, Relay2On, Relay2Off);
    relay3 = new Switch("Modem", 82, Relay3On, Relay3Off);
    relay4 = new Switch("Test", 83, Relay4On, Relay4Off);
    relay5 = new Switch("Night Light", 84, Relay5On, Relay5Off);

    Serial.println("Adding switches upnp broadcast responder");
    upnpBroadcastResponder.addDevice(*relay1);
    upnpBroadcastResponder.addDevice(*relay2);
    upnpBroadcastResponder.addDevice(*relay3);
    upnpBroadcastResponder.addDevice(*relay4);
    upnpBroadcastResponder.addDevice(*relay5);
  }
}
 
void loop()
{
	 if(wifiConnected){
      upnpBroadcastResponder.serverLoop();
      
      relay1->serverLoop();
      relay2->serverLoop();
      relay3->serverLoop();
      relay4->serverLoop();
      relay5->serverLoop();
	 }
}

void Relay1On() {
    Serial.print("Switch Relay 1 turn on ...");
    digitalWrite (D7,HIGH);
    delay(1000); 
    digitalWrite (D7,LOW);
    
}

void Relay1Off() {
    Serial.print("Switch Relay 1 turn off ...");
    digitalWrite (D7, HIGH);
    delay(1000); 
    digitalWrite (D7,LOW);
}

void Relay2On() {
    Serial.print("Switch Relay 2 turn on ...");
    digitalWrite (D2,LOW); 
}

void Relay2Off() {
    Serial.print("Switch Relay 2 turn off ...");
    digitalWrite(D2,HIGH); 
}
void Relay3On() {
    Serial.print("Switch Relay 3 turn on ...");
    digitalWrite (D3,LOW); 
}

void Relay3Off() {
    Serial.print("Switch Relay 3 turn off ...");
    digitalWrite (D3,HIGH); 
}

void Relay4On() {
    Serial.print("Switch Relay 4 turn on ...");
    digitalWrite (D4,LOW); 
}

void Relay4Off() {
  Serial.print("Switch Relay 4 turn off ...");
  digitalWrite(D4,HIGH); 
}

void Relay5On() {
    Serial.print("Switch Relay 5 turn on ...");
    if(fadeValue <= 245){
      // sets the value (range from 0 to 255):
      analogWrite(D8, fadeValue);
      fadeValue = fadeValue += 10;
     // wait for 30 milliseconds to see the dimming effect
      delay(30); 
   // digitalWrite(D8,HIGH);
}
}

void Relay5Off() {
  Serial.print("Switch Relay 5 turn off ...");
  digitalWrite(D8,LOW); 
}

// connect to wifi – returns true if successful or false if not
boolean connectWifi(){
  boolean state = true;
  int i = 0;
  
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.println("");
  Serial.println("Connecting to WiFi");

  // Wait for connection
  Serial.print("Connecting ...");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    if (i > 10){
      state = false;
      break;
    }
    i++;
  }
  
  if (state){
    Serial.println("");
    Serial.print("Connected to ");
    Serial.println(ssid);
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
  }
  else {
    Serial.println("");
    Serial.println("Connection failed.");
  }
  
  return state;
}


//}

Use Ctrl + F to find void Relay5On()

That’s where I need to put the code to make the LED dim on and then go back to the beginning of the code to be ready for a new request. I’m totally lost in what to put there. What I have just makes it instantly turn on.

The LED fades up because the loop() function repeats. You can do something similar with a for loop that increments the value to be written to the LED, writes it to the LED then delay()s a while.

That may not be the best way to do what you want, but it is easy.

The thing is, I need the LED to be faded on and then the code goes back to the beginning to be ready for another request from the amazon echo.

(deleted)

Correction, the way I have it makes it light up very dimly.

CraftKing7777: The thing is, I need the LED to be faded on and then the code goes back to the beginning to be ready for another request from the amazon echo.

That is exactly what a for loop will do for you. Once it has finished the program will carry on at the next statement.

Wrap

    analogWrite(D8, fadeValue);
    fadeValue = fadeValue += 10;
    delay(30);

in a for loop where you want the fade up to happen. The number of steps and the increment value will control the rate at which the LED fades up.

I think what’s happening is the section of code is running once setting the pin to 10 and then continuing. How can I make it repeat over and over until the led is fully brightened and then continue onto the rest of the code?

CraftKing7777: How can I make it repeat over and over until

Did you miss everyone talking about for loops? You had one to begin with in this thread.

I know it is a loop but I don't want it to loop forever, I want it to loop just until it is fully brightened and then continue.

CraftKing7777: I know it is a loop but I don't want it to loop forever, I want it to loop just until it is fully brightened and then continue.

For loops don't loop forever. Where'd you get that idea? Why don't you take a little initiative and a few minutes and the advice you've been given and go study up on how for loops work in C++. I think you'll see what they're getting at if you'll put just a little bit of study in on it.

So I could use a while statement?

CraftKing7777:
So I could use a while statement?

I can see we are wasting our time with this one.

How many different times does someone have to say it?

Yes, you could do it with a while loop too. While and for are pretty closely related. But in this case the for loop certainly makes more sense since you have a variable you are incrementing with each pass.