WATER LEVEL INDICATOR WITH MPX5010DP

In this project we use the MPX5010DP sensor to detect the depth of water in the tank. The depth signal we visualize with a series of 16 LEDs with IC74HC595

water level indicator

The code can be downloaded at:

https://github.com/triantara/ARDUINO-TRIANTARA/blob/main/Water_level_indicator.zip

int latchPin = 8;
int clockPin = 12;
int dataPin = 11;
byte leds = 0;
byte leds1 = 0;
const int sensorMin = 70; // sensor minimum, discovered through experiment
const int sensorMax = 250; // sensor maximun, discovered through experiment

void setup()
{
Serial.begin(9600);
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
}

void loop()
{
int sensorReading = analogRead(A0);
// map the sensor range to a range of four options:
int range = map(sensorReading, sensorMin, sensorMax, 0, 16);
Serial.println(sensorReading);

switch (range) {
case 0:

leds = 0;
leds1 = 0;
updateShiftRegister();
break;
case 1:
leds = 0;
leds1 = 0;
updateShiftRegister();
bitSet(leds, 0);
updateShiftRegister();
break;

case 2:

leds = 0;
leds1 = 0;
updateShiftRegister();
bitSet(leds, 0);
bitSet(leds, 1);
updateShiftRegister();
break;
case 3:

leds = 0;
leds1 = 0;
updateShiftRegister();
bitSet(leds, 0);
bitSet(leds, 1);
bitSet(leds, 2);
updateShiftRegister();
break;
case 4:

leds = 0;
leds1 = 0;
updateShiftRegister();
bitSet(leds, 0);
bitSet(leds, 1);
bitSet(leds, 2);
bitSet(leds, 3);
updateShiftRegister();
break;
case 5:

leds = 0;
leds1 = 0;
updateShiftRegister();
bitSet(leds, 0);
bitSet(leds, 1);
bitSet(leds, 2);
bitSet(leds, 3);
bitSet(leds, 4);
updateShiftRegister();
break;
case 6:
leds = 0;
leds1 = 0;
updateShiftRegister();
bitSet(leds, 0);
bitSet(leds, 1);
bitSet(leds, 2);
bitSet(leds, 3);
bitSet(leds, 4);
bitSet(leds, 5);
updateShiftRegister();
break;
case 7:

leds = 0;
leds1 = 0;
updateShiftRegister();
bitSet(leds, 0);
bitSet(leds, 1);
bitSet(leds, 2);
bitSet(leds, 3);
bitSet(leds, 4);
bitSet(leds, 5);
bitSet(leds, 6);
updateShiftRegister();
break;
case 8:

leds = 0;
leds1 = 0;
updateShiftRegister();
bitSet(leds, 0);
bitSet(leds, 1);
bitSet(leds, 2);
bitSet(leds, 3);
bitSet(leds, 4);
bitSet(leds, 5);
bitSet(leds, 6);
bitSet(leds, 7);
updateShiftRegister();
break;
case 9:

///

leds = 0;
leds1 = 0;
updateShiftRegister();
bitSet(leds, 0);
bitSet(leds, 1);
bitSet(leds, 2);
bitSet(leds, 3);
bitSet(leds, 4);
bitSet(leds, 5);
bitSet(leds, 6);
bitSet(leds, 7);
bitSet(leds1, 0);
updateShiftRegister();
break;
case 10:

leds = 0;
leds1 = 0;
updateShiftRegister();
bitSet(leds, 0);
bitSet(leds, 1);
bitSet(leds, 2);
bitSet(leds, 3);
bitSet(leds, 4);
bitSet(leds, 5);
bitSet(leds, 6);
bitSet(leds, 7);
bitSet(leds1, 0);
bitSet(leds1, 1);
updateShiftRegister();
break;
case 11:

leds = 0;
leds1 = 0;
updateShiftRegister();
bitSet(leds, 0);
bitSet(leds, 1);
bitSet(leds, 2);
bitSet(leds, 3);
bitSet(leds, 4);
bitSet(leds, 5);
bitSet(leds, 6);
bitSet(leds, 7);
bitSet(leds1, 0);
bitSet(leds1, 1);
bitSet(leds1, 2);
updateShiftRegister();
break;
case 12:

leds = 0;
leds1 = 0;
updateShiftRegister();
bitSet(leds, 0);
bitSet(leds, 1);
bitSet(leds, 2);
bitSet(leds, 3);
bitSet(leds, 4);
bitSet(leds, 5);
bitSet(leds, 6);
bitSet(leds, 7);
bitSet(leds1, 0);
bitSet(leds1, 1);
bitSet(leds1, 2);
bitSet(leds1, 3);
updateShiftRegister();
break;
case 13:

leds = 0;
leds1 = 0;
updateShiftRegister();
bitSet(leds, 0);
bitSet(leds, 1);
bitSet(leds, 2);
bitSet(leds, 3);
bitSet(leds, 4);
bitSet(leds, 5);
bitSet(leds, 6);
bitSet(leds, 7);
bitSet(leds1, 0);
bitSet(leds1, 1);
bitSet(leds1, 2);
bitSet(leds1, 3);
bitSet(leds1, 4);
updateShiftRegister();
break;
case 14:

leds = 0;
leds1 = 0;
updateShiftRegister();
bitSet(leds, 0);
bitSet(leds, 1);
bitSet(leds, 2);
bitSet(leds, 3);
bitSet(leds, 4);
bitSet(leds, 5);
bitSet(leds, 6);
bitSet(leds, 7);
bitSet(leds1, 0);
bitSet(leds1, 1);
bitSet(leds1, 2);
bitSet(leds1, 3);
bitSet(leds1, 4);
bitSet(leds1, 5);
updateShiftRegister();
break;
case 15:

leds = 0;
leds1 = 0;
updateShiftRegister();
bitSet(leds, 0);
bitSet(leds, 1);
bitSet(leds, 2);
bitSet(leds, 3);
bitSet(leds, 4);
bitSet(leds, 5);
bitSet(leds, 6);
bitSet(leds, 7);
bitSet(leds1, 0);
bitSet(leds1, 1);
bitSet(leds1, 2);
bitSet(leds1, 3);
bitSet(leds1, 4);
bitSet(leds1, 5);
bitSet(leds1, 6);
updateShiftRegister();
break;
case 16:

leds = 0;
leds1 = 0;
updateShiftRegister();
bitSet(leds, 0);
bitSet(leds, 1);
bitSet(leds, 2);
bitSet(leds, 3);
bitSet(leds, 4);
bitSet(leds, 5);
bitSet(leds, 6);
bitSet(leds, 7);
bitSet(leds1, 0);
bitSet(leds1, 1);
bitSet(leds1, 2);
bitSet(leds1, 3);
bitSet(leds1, 4);
bitSet(leds1, 5);
bitSet(leds1, 6);
bitSet(leds1, 7);
updateShiftRegister();
break;

leds = 0;
leds1 = 0;
updateShiftRegister();

//delay(200);
}
}

void updateShiftRegister()
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, leds1);//slave
shiftOut(dataPin, clockPin, MSBFIRST, leds);//master
//MSBFIRST is LED trigger from no 1
//LSBFIRST is LED trigger from no 8
digitalWrite(latchPin, HIGH);
}