LED doesn't trigger + wrong message

Hi there, Arduino family!

Today I need some help with my code because I can’t seem to figure out why it’s behaving like this. I’m quite stuck…

I’ve run into the issue of getting a message when conditions aren’t even met. I know that when I unplug the GND cable from my TSL2591, the reading returned is a -1. Knowing that, I set a value = -1 and made an if statement for it to print back “Sensor error.” However, as everything is hooked up correctly, if I include this conditional statement, when the system is running, all it returns is -1 even though cables are connected. I notice that when I remove this conditional statement, the program runs smoothly + gives me back correct message.

The second thing I’ve noticed is that when both of my buttons are “off”, the red LED never turns on.

Below I have a snippet of my code where the conditional statements are in question. I’ve also attached the .INO file too.

            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){
                warningstate = "Sensor error.";
                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 < 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){
                warningstate = "Sensor error.";
                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(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);
              }
            }

Does anyone see what might be causing this?

4112022.ino (11.8 KB)

Attache the wiring diafram and the entire code.
When You unplugged GND, was the system powered up or not?

Then create a wiring diagram.
The code You show starts with " while (system1state == "on"){". That's not the entire code. Attaching .ino files is not according to Forum rules. Many helpers can't or don't want to download .inos.

Again, When You unplugged GND, was the system powered up or not?

// Load Wi-Fi library
#include <SPI.h>
#include <WiFiNINA.h>
#include <TimeLib.h>
#include <TimeAlarms.h>
#include <Wire.h>
#include <SparkFunDS1307RTC.h>
#include <DS1307RTC.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_TSL2591.h"
// Pass in a number for the sensor identifier
Adafruit_TSL2591 tsl = Adafruit_TSL2591(2591); 
// Replace with your network credentials
const char* ssid     = "";
const char* password = "";
// Set web server port number to 80
WiFiServer server(80);
// Variable to store the HTTP request
String header;
// Auxiliar variables to store the current output state
String system1state = "off";
String system2state = "off";
String warningstate = "";

// Initialize pins to each array.
const int system1 = 9;
const int system2 = 3;
int value = 0;

// Initialize pins to flag lights 
const int greenled = 7;
const int yellowled = 12;
const int redled = 13;

// Current time
unsigned long currentTime = millis();
// Previous time
unsigned long previousTime = 0; 
// Define timeout time in milliseconds (example: 2000ms = 2s)
const long timeoutTime = 2000;

void configureSensor(void)
{
  // You can change the gain on the fly, to adapt to brighter/dimmer light situations
  //tsl.setGain(TSL2591_GAIN_LOW);    // 1x gain (bright light)
    tsl.setGain(TSL2591_GAIN_MED);      // 25x gain
  //tsl.setGain(TSL2591_GAIN_HIGH);   // 428x gain  
  // Changing the integration time gives you a longer time over which to sense light
  // longer timelines are slower, but are good in very low light situtations!
   tsl.setTiming(TSL2591_INTEGRATIONTIME_100MS);  // shortest integration time (bright light)
  // tsl.setTiming(TSL2591_INTEGRATIONTIME_200MS);
  // tsl.setTiming(TSL2591_INTEGRATIONTIME_300MS);
  // tsl.setTiming(TSL2591_INTEGRATIONTIME_400MS);
  // tsl.setTiming(TSL2591_INTEGRATIONTIME_500MS);
  // tsl.setTiming(TSL2591_INTEGRATIONTIME_600MS);  // longest integration time (dim light)
}
void advancedRead(void)
{
  // More advanced data read example. Read 32 bits with top 16 bits IR, bottom 16 bits full spectrum
  // That way you can do whatever math and comparisons you want!
  uint32_t lum = tsl.getFullLuminosity();
  uint16_t ir, full;
  ir = lum >> 16;
  full = lum & 0xFFFF;
  Serial.print(F("[ ")); Serial.print(millis()); Serial.print(F(" ms ] "));
  Serial.print(F("Lux: ")); Serial.println(tsl.calculateLux(full, ir), 6);
  int lux = tsl.calculateLux(full, ir);
}
void setup() {
  // put your setup code here, to run once:
    
  Serial.begin(9600);
  pinMode(system1, OUTPUT);
  pinMode(system2, OUTPUT);
  pinMode(redled, OUTPUT);
  pinMode(greenled, OUTPUT);
  pinMode(yellowled, OUTPUT);

    // Connect to Wi-Fi network with SSID and password
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
    // Print local IP address and start web server
  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  server.begin();
}

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 to 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();

            // Sensor reading //
            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);
            // The requests to turn the arrays on and off.
            if (header.indexOf("GET /5/on") >= 0) {
              Serial.println("System 1 on");
              system1state = "on";
            } else if (header.indexOf("GET /5/off") >= 0) {
              Serial.println("System 1 off");
              system1state = "off";
              digitalWrite(system1, LOW);
            } else if (header.indexOf("GET /4/on") >= 0) {
              Serial.println("System 2 on");
              system2state = "on";
            } else if (header.indexOf("GET /4/off") >= 0) {
              Serial.println("System 2 off");
              system2state = "off";
              digitalWrite(system2, LOW);
            }

            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){
                warningstate = "Sensor error.";
                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 < 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){
                warningstate = "Sensor error.";
                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);
              }

Part 2 of the code:

              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);
              }
            }
            
            // Display the HTML web page
            client.println("<!DOCTYPE html><html>");
            client.println("<HEAD><meta http-equiv=\"refresh\" content=\"2\"></HEAD>");
            client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
            client.println("<link rel=\"icon\" href=\"data:,\">");
            // CSS to style the on/off buttons 
            // Feel free to change the background-color and font-size attributes to fit your preferences
            client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
            client.println(".button { background-color: #195B6A; border: none; color: white; padding: 16px 40px;");
            client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
            client.println(".button2 {background-color: #77878A;}</style></head>");

            // Web Page Heading
            client.println("<body><h1>Intelligent Lighting System Web Server</h1>");

            // Display current state, and ON/OFF buttons for SYSTEM 1  
            client.println("<p>Light Level: "); client.println(lux); client.print("lux");
            client.println("<p>" + warningstate + "</p>"); 
            client.println("<p>System 1 Status: " + system1state + "</p>");
            // If the system1state is off, it displays the ON button       
            if (system1state=="off") {
              client.println("<p><a href=\"/5/on\"><button class=\"button\">ON</button></a></p>");
            } else {
              client.println("<p><a href=\"/5/off\"><button class=\"button button2\">OFF</button></a></p>");
            } 

            // Display current state, and ON/OFF buttons for SYSTEM 2  
            client.println("<p>System 2 Status: " + system2state + "</p>");
            // If the output4State is off, it displays the ON button       
            if (system2state=="off") {
              client.println("<p><a href=\"/4/on\"><button class=\"button\">ON</button></a></p>");
            } else {
              client.println("<p><a href=\"/4/off\"><button class=\"button button2\">OFF</button></a></p>");
            }
            client.println("</body></html>");           
            // The HTTP response ends with another blank line
            client.println();
            // Break out of the while loop
            break;
          } else { // if you got a newline, then clear currentLine
            currentLine = "";          
        }
      } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
      }
    }
  }
  // Clear the header variable
    header = "";
    // Close the connection
    client.stop();
    Serial.println("Client disconnected.");
    Serial.println("");
  }
}

I’m going to work on creating that wiring diagram! But yes, the system was most definitely powered on.

I see Your code is to large to fit in the window. Then attaching an .ino file is ok. I appologize. But it reduces the number of helpers.

In Your post You tell about "buttons" but I don't see any controller pin being defined as INPUT.

Removing a GND while power is on... You ought to be put behind bars accused for attempted murder on electronical devices…. Don't ever change any connection unless everything is powered down.

Railroader:
I see Your code is to large to fit in the window. Then attaching an .ino file is ok. I appologize. But it reduces the number of helpers.

In Your post You tell about “buttons” but I don’t see any controller pin being defined as INPUT.

Removing a GND while power is on… You ought to be put behind bars accused for attempted murder on electronical devices…. Don’t ever change any connection unless everything is powered down.

Sorry, sir. I’m still learning. Super noob… super super noob…

These “Buttons” that I’m referring to are ON/OFF buttons that can be found on the web interface hosted by the Arduino from my code. When I click on, the LED array turns on, and vice versa.

I’ve attached a picture that my partner drew up last week in the meantime. The reason I wouldn’t use this is because the wires coming from the TSL2591 for SDA/SCL has since been updated to share the SDA/SCL pin with the RTC.

Thanks for the wiring! I’m sure I see a mistake.
On an Arduino UNO the analog pins A4 and A5 are used for I2C. That means You can’t use SDA and CLK at the same time.
Move the A4 and A5 to other A-pins!

Railroader:
Thanks for the wiring! I'm sure I see a mistake.
On an Arduino UNO the analog pins A4 and A5 are used for I2C. That means You can't use SDA and CLK at the same time.
Move the A4 and A5 to other A-pins!

That's what I was referring to when I say the drawing has been updated since. I'm trying to create a new drawing where we've already moved the A4/A5 to SDA/SCL.

Unless you were referring to not being able to share SDA/SCL for two two modules?

You can not move from A4/A5 to SDA/CLK because I2S, SDA/CLK uses A4/A5. You only have one I2C channel on an UNO.

Railroader:
You can not move from A4/A5 to SDA/CLK because I2S, SDA/CLK uses A4/A5. You only have one I2C channel on an UNO.

Ah I see. This alone would cause for the sensor to spit back a reading for a conditional not met?
The only reason I'm asking is because I find it strange that the moment I remove that condition for

"if (lux = -1) {
/// do this
} "

Then the sensor reads perfectly + gives back correct lux on the display.

I'll try to rewire and adjust the code to A4/A5 and see what happens. But I just wanted for you to keep in mind that everything works the way it should until I add that condition.

Railroader:
You can not move from A4/A5 to SDA/CLK because I2S, SDA/CLK uses A4/A5. You only have one I2C channel on an UNO.

I know this might sound a little dumb, but this includes UNO Wifi Rev 2?

A4/A5 are hard wired together. I2C is a bus handling several items, using different addresses.
I don't grab Your GND disconnect and the coding, and how You get any useful data not having every circuit at GND.

Railroader:
A4/A5 are hard wired together. I2C is a bus handling several items, using different addresses.
I don't grab Your GND disconnect and the coding, and how You get any useful data not having every circuit at GND.

Gosh darn it, it's been a long day. I understand your confusion now. Like, "Why the hell is this kid pulling the GND anyway?"

Simply put I was trying to simulate a sensor disconnect and add a condition for a known reading upon disconnection. Instead of GND, if I were to pull the SCL cable, the reading will display "-1".

But good news, while pondering over the connection I realize that my if and else if statements are used incorrectly. I've since revised to else if statements for each condition and now my program works the way it should! If I pull the SCL cable out, then my display reads back -1. Before, regardless if I pulled it or not, it would just give me a -1.

I'm going to try and use what I just learned to see if I can troubleshoot the condition statements for the red LED turning on/off depending on if BOTH buttons are off.

Well done!
But….. DON'T connect or disconnect anyting while power is on! I have roasted several circuits that way! I know. Slow down....

Good luck,

Railroader:
Well done!
But….. DON'T connect or disconnect anyting while power is on! I have roasted several circuits that way! I know. Slow down....

Good luck,

I appreciate you so much for your quick response and being easy to approach.

Thanks! Not soo many questions this time of hour....
Working and listening members are nice to meet.
Night and bed is closing in here…...

Railroader:
Thanks! Not soo many questions this time of hour…
Working and listening members are nice to meet.
Night and bed is closing in here……

I’m still running into the issue of not being able to digitalWrite my redLED when a condition is met. Can’t seem to figure this out. You would think all it takes is an if statement such as:

if (lux = -1){
digitalWrite(redled, HIGH)
}

and call it a day… however, this doesn’t seem to do anything. redLED is not burnt out, I just tested it. I have it wired to pin 13. Green and yellow come on when their conditions are met. Not understanding what’s going wrong here.

Pin 13 on an UNO is connected to [to a comparator, LM358, that drives] an onboard LED.
Put that if-statement in the right Place and use the right test-comparison.

Use Serial Monitor and Serial.println to gossip from inside the code what is happening. It's really effective!

This needs 2 == for a comparison.

if (lux = -1){