Trigger Fading from Local Web Server

I'm working on a small at-home project where I've managed to make a ton of progress today!

What I'm trying to create is a lighting system that can be controlled over a local web server (Arduino) using any browser. Currently, I have been able to display real-time lux reading from the sensor onto the webpage and I've also had success on turning the LEDs on and off.

I'm trying to find the best route to take in order to fade the LEDs once the on-status has been established.

So just to be clear:

  1. I am able to view on/off buttons on local web page.
  2. By pressing on, the LED array turns on.
  3. While the LED state is on, I'd like to be able to say "while LED state is on, if the hour is 1400 or before 1700, fade down until brightness is at a 0.
  4. If time is between 600 and 1400, just remain on.

Is this possible?

Here is my loop section where I think is where the problem lies.

void loop(){
  WiFiClient client = server.available();   // Listen for incoming clients

  if (client) {                             // If a new client connects,
    Serial.println("New Client.");          // print a message out in the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    currentTime = millis();
    previousTime = currentTime;
    while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected
      currentTime = millis();         
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        header += c;
        if (c == '\n') {                    // if the byte is a newline character
          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println("Connection: close");
            client.println();
            int fadeValue;
            fadeValue = map(fadeValue, 0, 255, 0, 18800);
            // Sensor reading //
            String warning;
            float sensorread;
            float sensorPin;
            float calculateLux;
            uint32_t lum = tsl.getFullLuminosity();
            uint16_t ir, full;
            ir = lum >> 16;
            full = lum & 0xFFFF;
            int lux = tsl.calculateLux(full, ir);
            if (lux < 500){
              Serial.println("Warning: Lux not within desired range!");
              warningstate = "Warning: Lux not within range!";
            }
            else {
              Serial.println("Lux is within desired range.");
              warningstate = "System status: Lux is within desired range.";
            }
            // turns the GPIOs on and off
            if (header.indexOf("GET /5/on") >= 0) {
              Serial.println("System 1 on");
              system1state = "on";
              for (fadeValue =0; fadeValue <= 255; fadeValue+=1){
                analogWrite(output5, fadeValue);
                delay(30);
              }
              digitalWrite(output5, HIGH);
            } else if (header.indexOf("GET /5/off") >= 0) {
              Serial.println("System 1 off");
              system1state = "off";
              digitalWrite(output5, LOW);
            } else if (header.indexOf("GET /4/on") >= 0) {
              Serial.println("GPIO 4 on");
              output4State = "on";
              digitalWrite(output4, HIGH);
            } else if (header.indexOf("GET /4/off") >= 0) {
              Serial.println("GPIO 4 off");
              output4State = "off";
              digitalWrite(output4, LOW);
            }

Hi,
It will be better if you post all your code, delete the SSID and PASSWORD ofcourse.
If it cannot be posted in code tags, please add the ino file as an attachment.

Thanks.. Tom... :slight_smile:

vutnguyen:
So just to be clear:

  1. I am able to view on/off buttons on local web page.
  2. By pressing on, the LED array turns on.
  3. While the LED state is on, I'd like to be able to say "while LED state is on, if the hour is 1400 or before 1700, fade down until brightness is at a 0.
  4. If time is between 600 and 1400, just remain on.

There are 24 hours in a day. What should happen between midnight and 600? what about 1700 - 2400?

It would simply be off! I was just utilizing a random window of time.

TomGeorge:
Hi,
It will be better if you post all your code, delete the SSID and PASSWORD ofcourse.
If it cannot be posted in code tags, please add the ino file as an attachment.

Thanks.. Tom... :slight_smile:

Here you go! Sorry I missed your message!

ButtonsButtons.ino (10.1 KB)

there seems to be a couple of issues with your code. There are several functions in your sketch that you never call so why are they there? unifiedSensorAPIRead(), advancedRead().... It just adds noise to what you are trying to accomplish.

Does this code inside loop()

            if (header.indexOf("GET /5/on") >= 0) {
              Serial.println("System 1 on");
              system1state = "on";
              for (fadeValue =0; fadeValue <= 255; fadeValue+=1){
                analogWrite(output5, fadeValue);
                delay(30);
              }

Not vade output5 from OFF to ON?

blh64:
there seems to be a couple of issues with your code. There are several functions in your sketch that you never call so why are they there? unifiedSensorAPIRead(), advancedRead().... It just adds noise to what you are trying to accomplish.

Does this code inside loop()

            if (header.indexOf("GET /5/on") >= 0) {

Serial.println("System 1 on");
              system1state = "on";
              for (fadeValue =0; fadeValue <= 255; fadeValue+=1){
                analogWrite(output5, fadeValue);
                delay(30);
              }



Not vade output5 from OFF to ON?

This fades it incrementally until it gets to 255 but the delay is what dictates how fast it’s fading up to that value. I wish I could implement my RTC correctly so that from let’s say 6AM to 2PM, the LED could just fade up at a steady pace. I don’t know what the most efficient way is just yet.

It seems like there are 2 competing things going on. If you get an "ON" message from a web client, you turn on the LED.

You also want to turn on the LED slowly from 6AM to 2PM based on a RTC. It is not clear how you deal with each of these. What should happen in a web client sends "ON" at 8AM? Does the system just turn completely on?

Or....
Do you want the web client to turn the system ON/OFF as a state and if the state is ON and the time is between 6am and 2pm, you fade on? Are these times configurable from a web client or hard coded into your program?

blh64:
It seems like there are 2 competing things going on. If you get an "ON" message from a web client, you turn on the LED.

You also want to turn on the LED slowly from 6AM to 2PM based on a RTC. It is not clear how you deal with each of these. What should happen in a web client sends "ON" at 8AM? Does the system just turn completely on?

Or....
Do you want the web client to turn the system ON/OFF as a state and if the state is ON and the time is between 6am and 2pm, you fade on? Are these times configurable from a web client or hard coded into your program?

Thanks for asking really good questions!

Now that you’ve guided me a bit with your questioning I can confidently say that I’d wish to be able to turn the system on and off using the web client. However from 6AM to 2PM, I want each hour interval to have a different level of brightness for the LED. For example if it’s 6AM, have the LED shine at an output of 180. At 7AM, have it 190 and so forth. The “fading” is more like an incremental adjustment to the light depending on time of day.

How should I go about coding this into my loop?

I think you will need to break this into two separate events. If your web client sends the "ON" command, you just need to set a variable true. If the web client sends the "OFF" command, set the variable to false.

Then, inside your loop() function, you look at the state of this variable and the time from your RTC and decide what to do and what level of brightness is appropriate. This code should get you started. You will need to add your RTC calculations

ButtonsButtons.ino (8.62 KB)

I seem to have figured it out! All I needed was to add a "break" to my code and now it's doing exactly what I want it to. However, I've ran into this one issue.

If I leave a conditional statement for the lux as below:

 while (system1state == "on"){
              if (lux > 70) {
                value = 70;
                warningstate = "Message: System will adjust lighting output to day-time conditions.";
                digitalWrite(redled, LOW);
                digitalWrite(yellowled, LOW);
                digitalWrite(greenled, HIGH);
              }
              if (lux < 5 && lux >= 0) {
                value = 0;
                warningstate = "Message: System will turn lights off due to night-time conditions.";
                digitalWrite(redled, LOW);
                digitalWrite(yellowled, HIGH);
                digitalWrite(greenled, LOW);
              }

              if (lux = -1) {
                value = -1;
                warningstate = "Check sensor.";
                digitalWrite(redled, HIGH);
                digitalWrite(yellowled, LOW);
                digitalWrite(greenled, LOW);
              }
              
              else if (lux > 5 && lux <70) {
                warningstate = "Message: System will dim lights due to minimal environmental light.";
                digitalWrite(redled, LOW);
                digitalWrite(yellowled, LOW);
                digitalWrite(greenled, HIGH);
              }
              value = lux;
              value = constrain(value, 0, 70);
              value = map(value, 0, 70, 0, 255);
              analogWrite(system1, value);
              // Break
              break;
              }

            while (system2state == "on"){
              if (lux > 70) {
                value = 70;
                warningstate = "Message: System will adjust lighting output to day-time conditions.";
                digitalWrite(redled, LOW);
                digitalWrite(yellowled, LOW);
                digitalWrite(greenled, HIGH);
              }
              if (lux = -1) {
                value = -1;
                warningstate = "Check sensor.";
                digitalWrite(redled, HIGH);
                digitalWrite(yellowled, LOW);
                digitalWrite(greenled, LOW);
              }
              if (lux < 5 && lux >= 0) {
                value = 0;
                warningstate = "Message: System will turn lights off due to night-time conditions.";
                digitalWrite(redled, LOW);
                digitalWrite(yellowled, HIGH);
                digitalWrite(greenled, LOW);
              }
              else if (lux > 5 && lux <70) {
                warningstate = "Message: System will dim lights due to minimal environmental light.";
                digitalWrite(redled, LOW);
                digitalWrite(yellowled, LOW);
                digitalWrite(greenled, HIGH);
              }
              value = lux;
              value = constrain(value, 0, 70);
              value = map(value, 0, 70, 0, 255);
              analogWrite(system2, value);
              // Break
              break;
            }
            if (system1state == "off"){
              if (system1state == system2state) {
                warningstate = "System is off.";
                digitalWrite(redled, HIGH);
                digitalWrite(yellowled, LOW);
                digitalWrite(greenled, LOW);
              }
            }
            if (system2state == "off"){
              if (system2state == system1state) {
                warningstate = "System is off.";
                digitalWrite(redled, HIGH);
                digitalWrite(yellowled, LOW);
                digitalWrite(greenled, LOW);
              }
            }

I get a return of:

lux: -1
"Check sensor."

Even though the conditions aren't met.

4112021.ino (11.8 KB)