Water level sensor with reed switches

Hi,

I'm hoping someone could help me with the code for a water level sensor.
I have reed switches with a float that will be installed in a water tank but I am a complete beginner.
The code I have so far just turns on the corresponding LED's on with each reed switch but I'm trying to get it so that the higher the water raises the more LED's lights up and stays lit until it detects either the next reed or previous reed switch changes.

any help would be very much appreciated

Darren.

const int numReedSwitches = 8;  // Number of reed switches
const int reedSwitchPins[] = {2, 3, 4, 5, 6, 7, 8, 9};  // GPIO pins connected to reed switches
const int ledPins[] = {10, 11, 12, 13, A0, A1, A2, A3};  // GPIO pins connected to LEDs

void setup() {
  Serial.begin(9600);  // Initialize serial communication

  for (int i = 0; i < numReedSwitches; i++) {
    pinMode(reedSwitchPins[i], INPUT_PULLUP);  // Set the reed switch pins as inputs with internal pull-up resistors
    pinMode(ledPins[i], OUTPUT);  // Set the LED pins as outputs
    digitalWrite(ledPins[i], HIGH);  // Turn off all LEDs initially
  }
}

void loop() {
  for (int i = 0; i < numReedSwitches; i++) {
    int waterLevel = digitalRead(reedSwitchPins[i]);  // Read the state of the reed switch (HIGH or LOW)
    if (waterLevel == LOW) {
      Serial.print("Reed switch ");
      Serial.print(i + 1);
      Serial.println(" closed");  // Output the reed switch status
      digitalWrite(ledPins[i], HIGH);  // Turn on the corresponding LED
    } else {
      Serial.print("Reed switch ");
      Serial.print(i + 1);
      Serial.println(" open");  // Output the reed switch status
      digitalWrite(ledPins[i], LOW);  // Turn off the corresponding LED
    }
  }

  delay(1000);  // Delay for 1 second before checking the reed switches again
}

Doesn't your existing code already do that? If switches 1-4 are closed, then LEDs 1-4 will be ON. Aren't your reed switched aligned lowest to highest so they will be closed in sequence?

The LED orientation (cathode/anode) in setup() (the line above) differs from loop() (the lines below).

aren't the reed switches indicating the water level?

if so, why not simply

void loop () {
    for (int i = 0; i < numReedSwitches; i++) {
        digitalWrite (ledPins[i], digitalRead (reedSwitchPins[i]));
    }
}

Reed switches respond to magnetic fields.

so just one switch is closed by something floating on the water and the goal is to turn on all the LEDs at that level or below?

Pretty much yeah, all the leds that correlate to the red switches below the water needs to be on

The switches are in parallel with just a floating magnet to active the one closest the water line

so once you detect a switch that is on, turn on all the LEDs up to that point (not sure of direction)

void loop () {
    for (int i = 0; i < numReedSwitches; i++) {
        if (LOW == digitalRead (reedSwitchPins[i]))  {
            for (int j = 0; j <=  i; j++) {
                digitalWrite (ledPins[j], ON);
            break;
        }
    }
}

Before you go too far, you should test your setup to see how many reeds are closed by your magnet. Also see if there is a position between reeds where the magnet has not closed any reeds.

He described that as a problem but logic over time says that the water level should be considered as close to the last indicated level since

if the level rises it should get to to the next switch and if it falls it should get to the last position. In the meantime, save the current level in a variable and start thinking more about arrangements of events over time.

His (guessing here) code should should light all the leds from bottom to last known level.

another approach is to simply turn on an LED up to and including the one corresponding to the reed switch that is on

Yeah there are gaps between the reed switches. So there will be times where the magnet wont be turning on any leds. So I’m just wondering if there’s a a way to keep the last led plus lower LEDs lit until another reed is closed

Yeah, that's pretty easy. But it gets difficult if there is power loss or batteries need changing, at a water level where the float is in a gap between reed switches. Then the Arduino will not know what level the float is at.

The Arduino's EEPROM memory could be used to save the last known water level, but that does not fix the problem if the water level changes before power is restored and the float is between reed switches when the power comes back on.

int level;

void loop () {
    for (int i = 0; i < numReedSwitches; i++) {
        if (LOW == digitalRead (reedSwitchPins[i]))  {
            level = i;
        }
    }

    for (int i = 0; i <= numReedSwitches; i++)  {
        if (i <= level)
            digitalWrite (ledPins[i], On);
        else
            digitalWrite (ledPins[i], Off);
    }
}
#include <EEPROM.h>
const int numReedSwitches = 8;  // Number of reed switches
const int reedSwitchPins[] = {2, 3, 4, 5, 6, 7, 8, 9};  // GPIO pins connected to reed switches
const int ledPins[] = {10, 11, 12, 13, A0, A1, A2, A3};  // GPIO pins connected to LEDs

int level;

void setup() {
  Serial.begin(9600);  // Initialize serial communication

  level = EEPROM.get(0);

  for (int i = 0; i < numReedSwitches; i++) {
    pinMode(reedSwitchPins[i], INPUT_PULLUP);  // Set the reed switch pins as inputs with internal pull-up resistors
    pinMode(ledPins[i], OUTPUT);  // Set the LED pins as outputs
  }
}

void loop() {
    for (int i = 0; i < numReedSwitches; i++) {
        if (LOW == digitalRead (reedSwitchPins[i]))  {
            level = i;
            EEPROM.update(0, level);
            }
        }
    }

    for (int i = 0; i <= numReedSwitches; i++)  {
        if (i < level)
            digitalWrite (ledPins[i], LOW);
        else
            digitalWrite (ledPins[i], HIGH);
    }
  delay(1000);  // Delay for 1 second before checking the reed switches again
}

This is what I would call a design consideration. In my experience it is best to flesh out the logic before starting to write code.

You could setup code to remember the last two led levels. This would allow you to include an indicator of the last known water level direction.

And when the magnet goes between, no sensor is ON.

When I wrote guessing here, it was about not knowing who wrote the code to say his, her's or what pronouns they prefer just as courtesy the anti-insult.

His (guessing here) code should should light all the leds from bottom to last known level.

Maybe multiple float balls or a cylindrical float that would span 2 reed switches?

in the code i posted, the level would remain the same value since the last time a sensor was detected and all the LEDs up to that level would remain on