How to use do-while

im using arduino to control on/off relay based on water level sensor. there are 2 water level sensor, the first sensor is put in high 0 m and another sensor is put in high 1.2 m.

if water level start high from 0 m until below 1.2m, relay is off.
if water level above 1.2 m, relay is on untill water level reach 0 m again.

here's the code that i made

#define POWER_PIN  7
#define SIGNAL_PIN A0

#define POWER_PIN2  6
#define SIGNAL_PIN2 A1
int RelayPin = 3;


int value = 0; // variable to store the sensor value for 0m
int value2 = 0; // variable to store the sensor value for 1.2m


void setup() {
  Serial.begin(9600);
  pinMode(POWER_PIN, OUTPUT);   // configure D7 pin as an OUTPUT
  digitalWrite(POWER_PIN, LOW); // turn the sensor OFF

  pinMode(POWER_PIN2, OUTPUT);   // configure D7 pin as an OUTPUT
  digitalWrite(POWER_PIN2, LOW); // turn the sensor OFF

  pinMode(RelayPin, OUTPUT);
  digitalWrite(RelayPin, HIGH);
}

void loop() {
  digitalWrite(POWER_PIN, HIGH);  // turn the sensor ON
  digitalWrite(POWER_PIN2, HIGH);  // turn the sensor ON
 
  delay(10);                      // wait 10 milliseconds
  
  value = analogRead(SIGNAL_PIN); // read the analog value from sensor
  value2 = analogRead(SIGNAL_PIN2); // read the analog value from sensor
 
  
  digitalWrite(POWER_PIN, LOW);   // turn the sensor OFF
  digitalWrite(POWER_PIN2, LOW);   // turn the sensor OFF
  

  if((value < 400) && (value2 < 400))
    {
      digitalWrite(RelayPin, HIGH);
      Serial.println("high < 0 m ");
      
    }
  else if((value >= 400) && (value2 < 400))
    {
      digitalWrite(RelayPin, HIGH);
      Serial.println("high between 0 and 1.2 m");
      
    }
  else if((value >= 400) && (value2 >= 400))
    {
      do{
        digitalWrite(RelayPin, LOW);
        Serial.println(" high > 1.2 m");
        Serial.println(value);
        Serial.println(value2);
        }
        while(value >= 400);
    }
  Serial.println(value);
  Serial.println(value2);
  delay(1000);
}

im using do while. when value >400 and value2> 400, relay is on just while value >400.
and after that, i tried to take out the water level sensor 1.2 m dan water level sensor 0m, relay keep on.

is there any wrong code in my code?

thank you

Since you never update value, how do you expect things to change ?

     do{
       digitalWrite(RelayPin, LOW);
       Serial.println(" high > 1.2 m");
       Serial.println(value);
       Serial.println(value2);
       
       value = analogRead(SIGNAL_PIN);    // <-----<<<<
       value2 = analogRead(SIGNAL_PIN2);  // <-----<<<<
       }
       while(value >= 400);

You do realize do/while creates blocking code ?

It's best you avoid blocking code.

Study how to use a State Machine.

Hello farhan562

Welcome to the worldbest Arduino forum ever.

This is a nice project to get started.

Do you have experience with programming in C++?

The task can easily be realised with an object.
A structured array contains all information, such as pin addresses for the I/O devices, as well as the information for the timing.
A single service takes care of this information and initiates the intended action.
The structured array makes the sketch scalable until all I/O pins are used up without having to adapt the code for the service.
It is cool stuff, isn´t it?

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

  • why is relay on when level > 1.2 instead of being turned off (?)
  • why 2 sensors?
  • why do/while loop

look this over

#define POWER_PIN  7
#define SIGNAL_PIN A0

#define POWER_PIN2  6
#define SIGNAL_PIN2 A1

int RelayPin = 3;

enum { SenOff = LOW,  SenOn = HIGH };
enum { RelayOff = HIGH, RelayOn = LOW };

int relayState = RelayOff;

// -----------------------------------------------------------------------------
void loop ()
{
    int level = analogRead (SIGNAL_PIN);
    Serial.println (level);

    if (RelayOn == relayState) {
        if (level <=  0)
            relayState = RelayOff;
    }
    else {
        if (level >=  400)
            relayState = RelayOn;
    }
    digitalWrite (RelayPin, relayState);

    delay (1000);       // check once a second
}

// -----------------------------------------------------------------------------
void setup () {
    Serial.begin (9600);

    pinMode      (POWER_PIN, OUTPUT);
    digitalWrite (POWER_PIN, SenOn);

    pinMode      (RelayPin, OUTPUT);
    digitalWrite (RelayPin, RelayOff);
}

I saw

if((value < 400) && (value2 < 400) && (value3 < 400))... 

@farhan562 never compares the analogRead() values from the sensors to anything but 400.

It is reasonable to conclude that they are being used in a quasi-digital manner, like wet or not wet converted to a level sensor. Water above or below the sensor.

So with two sensors and a glance at the first code version posted, it appears to be an attempt at a system that will fill until full, then stop and let emptying happen until empty.

A classic hysteresis controlled system.

The code actually looks very close, this

    if the tank is full, turn off the motor

    if the tank is empty, turn on the motor

is all that takes - as long as there is some distance between the sensor that can report full and the sensor that reports empty.

In between, the motor is running if the tank is on the way to being filled, and is not running if the tank is on the way to being empty, and there is no need to specifically address the in-between condition in the code.

And there is no need for any looping statement within the loop() function. The intent of the loop in there is to keep filling until full; that will be taken care of by the loop() itself looping, and the hysteresis that controls the change to the motor on/off state.

I have no idea what the third sensor is for, or does.

a7

Always show us a good schematic of your proposed circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.

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