Level sensors code

Hi all,

I'm trying to control a pump using 2 level sensors. The working conditions are as follow;
low sensor unblock and high sensor unblock -> do nothing
low sensor block and high sensor unblock -> do nothing
low sensor block and high sensor block -> turn on the pump until the low sensor is unblock.

My code is the follow but i'm having problems with the condition do while. Any suggestions?

 int pump = 11;
    int highsen = 7;
    int lowsen = 8;
    int highsig;
    int lowsig;
    
    void setup() {
      pinMode(highsen, INPUT);
      pinMode(lowsen, INPUT);
      pinMode(pump, OUTPUT);
    
    }
    void loop() {
      highsig = digitalRead(highsen);
      lowsig = digitalRead(lowsen);
        
      if ( lowsig == 0 & highsig == 0) //both unblocked
      {
        digitalWrite(pump, LOW);
      }
    
      if ( lowsig == 1 && highsig == 0) //lower one unblocked and upper one blocked
      {
       digitalWrite(pump, LOW);
      }
     
      if ( lowsig == 0 & highsig == 1) //lower one blocked and upper one unblocked
      {
       digitalWrite(pump, LOW);
      }
      
      if (lowsig == 1 & highsig == 1) //both blocked
      {   
        do{digitalWrite(pump, HIGH);} while(lowsig == 1);
      }
    }

Open the serial port and put some serial prints in to follow the program flow and monitor variable values.

Do you need logical and (&&) instead of bitwise and (&)?

Do you need to read the lowsen pin to be able to exit the do-while loop?

Code could be a LOT shorter and simpler, but nobody’s going to help develop a ‘bomb trigger’, lest they become partially liable for your mistakes.

From your logic description It's not clear whether the pump shall fill or empty the container.

Make it simpler: if the Start (high?) sensor is active start the pump, and if the other sensor is active stop the pump. Else do nothing. No while required.

Hi, @Ihuaw
Welcome to the forum.

Can you please post a circuit diagram of your project?
A picture of a hand drawn circuit will be fine, please include pin and component labels as well as your power supply.

How have you got your sensors wired?
Are you using pullup or pulldown resistors on the signal line?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

Hi, Sorry for my grammar mistakes. I'm using 2 reflective sensors to monitor an air bag. When the the bag is completely filled (2 sensors are block), it's necessary to activate the pump to compress it until the low sensor is unblock (the air bag needs to mantain certain volume).

Looks like you have to keep track of if the pump is running or not.

const byte pump = 11;
const byte highsen = 7;
const byte lowsen = 8;

boolean PumpIsRunning = false;

void setup()
{
  pinMode(highsen, INPUT);
  pinMode(lowsen, INPUT);
  pinMode(pump, OUTPUT);
}

void loop()
{
  int highsig = digitalRead(highsen);
  int lowsig = digitalRead(lowsen);

  // low sensor block and high sensor block ->
  // turn on the pump until the low sensor is unblock.
  if (lowsig == HIGH && highsig == HIGH) //both blocked
  {
    digitalWrite(pump, HIGH);
    PumpIsRunning = true;
  }

  if (PumpIsRunning && lowsig == LOW) //lower one unblocked
  {
    digitalWrite(pump, LOW);
    PumpIsRunning = false;
  }
}
1 Like

Thank you so much John. That's exactly what I tried to do. I'm self-learning to program on arduino, so I was stuck (It seems that I still have a lot to learn).