Two issues with a program I wrote, would like help if possible.

Project Summary:

I am using two 12 volt pumps to exchange oil from an engines oil pan to a reservoir and back from the reservoir. I am using one pump to drain from the oil pan to the reservoir and a second pump to fill from the reservoir to the engine oil pan. I am using a single normally closed float switch that becomes "open" or low at engine oil pan being at the full mark.

My goal of the project is this:

Arduino receives power when the engine starts.

Short delay before running any pumps to allow engine to become at idle.

If engine oil pan is at the full level, float switch "low" turn on empty pump for 12 seconds then turn off.

If engine oil pan is not at the full level, float switch "high" turn on fill pump for 12 seconds then turn off.

Wait 15 minutes aka 900,000 seconds, then check status of float switch. Repeat program continually.

Issues I am seeing:

System keeps cycling the empty pump every cycle and seems to be getting a bad read from the float switch causing the engine oil pan to run low. This is not a common issue but I have seen it on some equipment, I am thinking it has to do with bad hardware or wiring not necessarily the program.

Second issue is on initial power up the empty pump seems to run even if the float switch is in what looks to be the not at full position.

Please review my program and post any input or ask any questions. I am fairly new to programming but am good at wiring and reading electrical prints. I can supply pictures, or diagrams when I get back to the office if these items would be of assistance.

Thanks for all the help and your time.

//2016.8 revision added delay before for first of system to allow oil to warm up before starting transfers


#define empty 4
#define fill 5
#define upper 2


int val1=0;

// the setup routine runs once when you press reset:
void setup() {        
Serial.begin(9600);  
  // initialize the digital pin as an output.
  pinMode(empty, OUTPUT); 
pinMode(fill, OUTPUT);
pinMode(upper,INPUT);
delay (90000);
}

// the loop routine runs over and over again forever:
void loop() {


digitalWrite (fill,LOW);
digitalWrite (empty,LOW);
digitalRead(upper);

val1= digitalRead(upper);                                       //upper oil level switch 1 equals full sump
  
Serial.println("1 Equals Below Full, 0 Equals At Full");        //sends to serial monitor listed text  
  Serial.println(val1);                                           //sends to serial moniotr status of val1 if its a 1 the switch is not at full if its a 0 the sump has tripped the full switch

digitalRead(upper);

if (val1==HIGH)               //if oil sump is below switch trip point add oil

{
  digitalWrite (fill,HIGH);       //turns fill pump on
  delay (12000);                  //leaves fill pump on for 10 seconds
  digitalWrite (fill,LOW);        //turns fill pump off
}
  
 if (val1==LOW)                 //if oil sump is at full mark 

   { digitalWrite (empty,HIGH);         //turns on empty pump
    delay (12000);                      //leaves pump on for 12 seconds
    digitalWrite (empty,LOW);           //turns off empty pump
    
  
   }


delay (900000);     //length of time the machines waits between cycles 900,000 ms=15 minutes
}

Why is this all by itself? digitalRead(upper);

Don't use the delay() function, use the BWD BlinkWithoutDelay technique. .

Some suggestions that will make it easier for us to read your code:

  1. Symbolic constants using #define are usually uppercase letters

  2. Variable names should reflect the purpose of the variable. Perhaps EnginePanPump and ReservoirPump might be better variable names. Often, variables assume the traits of nouns and operators are verbs. You've given your pump names a verb connotation.

  3. Make sure comments tell the truth: delay (12000); //leaves fill pump on for 10 seconds

  4. User Ctrl-T in the IDE to reformat your code to a common C style before you post it.

  5. Use if-else where applicable:

if (val1==HIGH)               //if oil sump is below switch trip point add oil

{
  digitalWrite (fill,HIGH);       //turns fill pump on
  delay (12000);                  //leaves fill pump on for 10 seconds
  digitalWrite (fill,LOW);        //turns fill pump off
}
  
 if (val1==LOW)                 //if oil sump is at full mark 

   { digitalWrite (empty,HIGH);         //turns on empty pump
    delay (12000);                      //leaves pump on for 12 seconds
    digitalWrite (empty,LOW);           //turns off empty pump

If val1 is HIGH, is there a need to test it again when the state of val1 can't change before the second test? Wouldn't an if-else help here?

You don't use INPUT_PULLUP on your input pin. Which brings the question how the switch is wired. From the description, it's wired from pin to ground. If so, do you have an external pull-up resistor? If not, you can use pinMode(upper, INPUT_PULLUP);.

If the switch is wired between pin and Vcc (in case I misread the description), you need an external pull-down resistor.

If there is no resistor, your input pin is floating and reading it can result in random values. That will explain the random behaviour that you observe.

Next you should consider what happens if you are in the delay and the switch indicates full. The pump will keep on pumping. See the remark about blink-without-delay. You can also look at Demonstration code for several things at the same time.

PS By the way, 900,000 might be interpreted at fifteen-thousand seconds (depending on the locale).

By the way, 900,000 might be interpreted at fifteen-thousand seconds (depending on the locale).

Your calculator needs new batteries. Minutes, maybe. 8)

LarryD: Why is this all by itself? digitalRead(upper);

Don't use the delay() function, use the BWD BlinkWithoutDelay technique. .

Larry,

That is a typo from when I was troubleshooting. Thanks for catching that. I will also look into the BWD technique. I was unaware of the function.

Thanks for the response.

Adam

PaulS: Your calculator needs new batteries. Minutes, maybe. 8)

No, it needed more coffee :D