Variable in a pin name?

I have a project where I'm moving water between a measurement tank and a storage reservoir based on the user's level request, levels 1 through 10, and there are ten water level sensors in the tank that determine what action is taken when a request happens (fill, drain, or nothing).

Btw, I'm very new to this Arduino stuff, I've only been doing this a few weeks. My code is primitive, but it all works. I'm hoping here to tighten up the code and make future refinements and updates easier. I'm using an Arduino Mega (Elegoo).

The code below is the method that's ran when water level 1 is requested. There are ten water levels in my project, and so far I've been using ten separate methods, one for each water level. Any time I refine the code for level 1, I then have to propagate those changes to the other nine. I'd like to have one method for all ten and use a variable instead.

Here's a demonstration of what I'd like to do:
Instead of
if(digitalRead(levelsensor1) == HIGH && digitalRead(levelsensor2) == LOW){
I'd like to do something like
if(digitalRead(levelsensor variable) == HIGH && digitalRead(levelsensor variable +1) == LOW){

I would then set the variable prior to running the method, instead of setting it within the method as I'm doing now so I would only need one method for any requested water level.

Is this even possible?
Thanks for any suggestions!

void goto_level1(){

#include <LCD_I2C.h>
LCD_I2C lcd(0x27, 16, 2);

lcd.backlight();
digitalWrite(gobuttonled, LOW);

lvl = 1; //sets variable to current level request

  //check for defective level sensor
  //the requested water level sensor is dry, but somehow a higher level sensor is wet
  if(digitalRead(levelsensor1) == HIGH && digitalRead(levelsensor2) == LOW){
       sensor_defect(); //stops pump, closes valves, displays error message on lcd
       return; }

  //water level less than requested
  if(digitalRead(levelsensor1) == HIGH && digitalRead(levelsensor2) == HIGH){
       display_filling(); //display message that tank is filling
       
       while(digitalRead(levelsensor1) == HIGH){
       active_fill(); //open valves to allow filling
       if(digitalRead(levelsensor1) == HIGH && digitalRead(levelsensor2) == LOW){
       sensor_defect();
       return; }
       delay(50);}
       }

  //water level at requested, higher water level not detected
  if(digitalRead(levelsensor1) == LOW && digitalRead(levelsensor2) == HIGH){
       display_achieved();
       }

  //water level already above requested
  if(digitalRead(levelsensor2) == LOW){
       display_draining();
       active_drain(); //open valves to allow draining

       //check for defective level sensor      
       while(digitalRead(levelsensor2) == LOW){
         if(digitalRead(levelsensor1) == HIGH){
         sensor_defect();
         return; }
       }//defective sensor not found, moving on
       
       //when requested level sensor becomes dry, stop draining
       digitalRead(levelsensor1);
         while(digitalRead(levelsensor1) == LOW){
           active_drain();

           digitalRead(levelsensor1);
           delay(50);}
       display_achieved();
       }

lvl = 0; //reset level request variable
turnoff_leds();
active_stop(); //stops pump, closes valves
buzzer_achieved(); //audible tone when action is complete
delay(3000);
}

Yes. consider using an array of pin numbers.
int levelsensors[10] = {2,3,4,5,6,7,8,9,10,11};
Reference them thusly:
if(digitalRead(levelsensors[0]) == HIGH && digitalRead(levelsensors[1]) == LOW){
//do something
Obviously, you'd only do 9 such checks.
Your next thought should be, "but I could make that index a variable in a for loop...

Keep in mind that an array like levelsensors[] above, dimensioned 10, and initialized to contain 10 pin numbers, is indexed from 0 to 9.

1 Like

Hello msp987

Welcome to the world's best Arduino forum ever.

Yes, do you have experience with programming in CPP?

My recommendation is to use a structured array.

This structured array contains all information about the pins and a service to debounce the pins and a service to detect state changes.
The results of the state change detection can be further processed with a switch/case statement, including the mentioned pin error handling.

Have a nice day and enjoy coding in C++.

You can do this using arrays.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.