remove my Delay :S

good morning world
this is my first time using C++

i think my delay is causing problems, as i understand this will stop the programs from running.

i would like to read the waterlevel all the time, so if its below low level it will make the 3 out puts (pins 10, 11, 12) LOW , and then return ;

now i have return; in 3 places after each analogRead of waterlevel
can i read code so its only once, and that at any time it get low water level.

//reads the state of water level and displays it on the screen

//define the pins
int levelPin = 1;
int alarmPin=10;
int pumpOne=11;
int pumpTwo=12;

int lowLevel=102.3; //high water level
int highLevel=920.7; //low water level
int waitTime=30000; // 30 seconds waiting time
boolean firstPump=HIGH; //declare state of the first pump too run (alternately)

void setup()
{
//set the level pin as an input & alarm pump1 pump2 pins as output
pinMode(levelPin, INPUT);
pinMode(alarmPin, OUTPUT);
pinMode(pumpOne, OUTPUT);
pinMode(pumpTwo, OUTPUT);

//allows to listen to serial communications from the arduino
Serial.begin(9600);
}

void loop()
{
int val = analogRead(levelPin);

if(val > highLevel)
{
firstPump = !firstPump; //alternate first pump to run
digitalWrite(pumpOne,firstPump);
digitalWrite(pumpTwo,!firstPump);
}
if(val <lowLevel)
{
digitalWrite(pumpOne,LOW);
digitalWrite(pumpTwo,LOW);
return ;
}

//start second pump after 30 sec if first pump is running

delay(waitTime);
if((val > highLevel) && pumpOne == HIGH);
{
digitalWrite(pumpTwo, HIGH);
}

if((val > highLevel) && pumpTwo == HIGH);
{
digitalWrite(pumpOne, HIGH);
}
if(val <lowLevel)
{
digitalWrite(pumpOne,LOW);
digitalWrite(pumpTwo,LOW);
return ;
}

//start alarm after 30 sec if pump 1 & 2 are running
delay(waitTime);
if(digitalRead((pumpOne) == HIGH)&&(pumpTwo) == HIGH);
{
digitalWrite(alarmPin, HIGH);
}

if(val <lowLevel)
{
digitalWrite(pumpOne,LOW);
digitalWrite(pumpTwo,LOW);
digitalWrite(alarmPin,LOW);
}

//print the level state to a serial terminal
Serial.println(analogRead(levelPin));
delay(1000);
//wait one second, then print again.
}

Hi SUV79,

Rather than putting a delay in your loop, consider using a timer like the following (really rough code):

...add at beginning of your sketch:

unsigned long startTime=0;

...in your loop, when you start your pump:

startTime = millis();

...in your loop when you want to see if time has elapsed 30 seconds:

if (millis() - startTime > 30000) do something...

ok i will look at this again, i looked at unsigned long yesterday but did not really under how it worked,
i will watch some more youtube about this :slight_smile:
thank you

int waitTime=30000; // 30 seconds waiting time
unsigned long startTime=0;

void setup()

startTime = millis();

void loop()

if (millis() - startTime > waitTime);

if((val > highLevel) && pumpOne == HIGH);
{
do something....
}