Automated Air Compressor code

In case anyone needed some help with putting on an automated air compressor, here is some code that may be able to help. In order to use this, you would need a single pressure switch attached to a relay and an input, and a relay for the air compressor.

In Initialize:

InitPressureSwitch(psInput, psRelay);

And in a new function:

void PressureSwitch(void)
   unsigned char pswitch;

   pswitch = GetDigitalInput(psInput);
   if (pswitch)
      SetRelay(airpump, 1, 0);
      SetRelay(airpump, 0, 0);

You also need to appropriately set the values of airpump, psInput, and psRelay.

You don’t need both you only need to call InitPressureSwitch(psInput, psRelay); in the Initialize function.

It will take care of everything after that.

Well, the point is not only to set up a pressure switch, but to get that pressure switch to trigger an air compressor to activate, filling up our tanks if the pressure is too low.

If that IS what the InitPressureSwitch function would do, then please feel free to let me know, and I’ll fix my code. :o

Why not use the KISS method (Keep it simple silly!)

relay08_fwd != rc_dig_in18;
relay08_rev = 0;

It uses less memory, and doesn’t cause the program to jump to another section of the program.

The function of InitPressureSwitch is to turn on the compressor when the air pressure falls below the maximum air pressure as indicated by the pressure switch.

That sounds both confusing and unhelpful. First, shouldn’t it turn on the compressor when the pressure falls below the minimum? The pressure switch doesn’t give a “falls below maximum” indication. Second, you still haven’t made it clear whether InitPressureSwitch has to be called regularly in order for it to work, or whether the single call in the Initialize function will set up some hidden background code to handle the compressor automatically without any further user action.

Naming it beginning with “Init” implies that it is intended to be run only once, at the start of the program. The name also implies that it initializes things that need to be processed later, but there doesn’t seem to be an associated “ProcessPressureSwitch” function. Perhaps the tutorial explains the proper use, though I don’t recall seeing anything about it.

From the help file…

Use the Pressure Switch block to automatically regulate a compressor in a pneumatic system.

The Pressure Switch function should be called a single time prior to your use of the compressor in a pneumatic system. The best place to use this block may be in ‘Initialize’. The function will automatically monitor the state of the pressure switch (every 500ms), and will activate the spike to power the compressor whenever the pressure switch closes (at about 95 psi). The compressor will stop when it reaches it’s maximum pressure of 115 psi.

edit: Also Alan if you read my earlier post

You don’t need both, you only need to call InitPressureSwitch(psInput, psRelay); in the Initialize function.

It will take care of everything after that.

My post you quoted made more sence.

I assume that the pressure switch should be wired between DIO SIG and GND for the EasyCPro InitPressureSwitch() function to work correctly. Correct?

The InitPressureSwitch function causes the timer interrupt service routine to read the pressure sensor every millisecond (probably overkill, but it was an easy place to put it). If the pressure is low the supplied relay port will be turned on, and if it’s high it will be turned off.

So you only need to call the function once, and for the rest of your program, both autonomous and operator control the compressor will operate on its own.

We are having problems with ours also…

We tried the code:

relay07_fwd != rc_dig_in07;
relay07_rev = 0;

and the pressure switch is connected to digital input 7 with the signal on one side of the switch and ground on the other side of the switch, the spike controlling the compressor (spike 7) is on all the time and the air compressor will continue pumping until after I disconnect it at 125 psi… it should shut off at 120 I thought but when I connect an ohm meter to this switch, it is open regardless of the air pressure (< 90 or > 120)…

I thought I read that the presure switch is normally closed but opens when the pressure dips down below 90 psi and then remains open until it reaches 120 psi… is this correct?

Is this a bad pressure switch or is there something that we don’t have connected correctly?


The code you posted is for MPLAB, while this is a sub-forum for EasyC Pro discussions.

It sounds as if you are wired correctly, so concentrate on the behavior of the pressure switch first, but test it in isolation.
Disconnect one of the pressure switch terminals so there is no possibility of a short along the wire or grounded through the RC.
Then take a ohm measurement of the pressure switch with your multi-meter when there is no pressure in the system, then allow the system to run up to 120psi and take another measurement.
If there is no change then it’s the pressure switch, otherwise…
reconnect the pressure switch, but disconnect the cable from the RC, and repeat your measurements at the RC end of the cable.

Work step-by-step on narrowing down the possibilities.

For EasyC Pro, we just have InitPressureSwitch(18,8) in the Initialize section of project. 18 for Digital input 18, Relay 8.

Yeah… oops. Disregard my original post.

I guess that this did serve one purpose: To give people a place to go to figure out similar mistakes.