Clicking spike relay while on

My team and I have been trying to get the compressor programmed, but it usually clicks when the light is green switching to red for a split second before going back to normal. It does it almost randomly, and does seem to be linked to our code. We have been using a simple on/off trigger so that we can turn the relay on and off, but the clicking happens when the spike should be on. our code is:

void compressorTest(Compressor *compr, Relay *rel)
static bool isOn = false;

UINT32 psvalve = compr->GetPressureSwitchValue();

if (0 == psvalve)
if (false == isOn)
isOn = true;
if (true == isOn)
isOn = false;

When all of the isOn {…} code is commented out, then the clicking does not happen, and it behaves correctly.

Can anyone help?

the lighting for a spike is as follows:
for a motor:
Orange - OFF / Brake Condition (default)
Green - Motor rotates in one direction
Red - Motor rotates in opposite direction
Off - OFF / Brake Condition

for a solenoid:
Orange - Both Solenoids OFF (default)
Green - Solenoid connected to M+ is ON
Red - Solenoid connected to M- is ON
Off - Both Solenoids ON

Basicly when it switches from green to red the spike is reversing the direction.

from the code what appears to be happening is that it starts off as isOn = false. Then it enters the first part of the code and isOn changes so now isOn = true. then it looks like the program then goes into the next part of the code and then changes it back to false. Are you sure that you are not looping it because the first time it would go through it would do the first half of the code and the next time through it would switchback.

no, it is programmed right, and it goes orange not red (my mistake), but it clicks back and forth, almost too fast too see, just a split second switch when it is only programmed to be on

First thing please indent and comment your code.
Indenting code makes it a lot easier to read and look for mistakes especially in next if’s , while etc statements.

Also by commenting your code you make it easier for people to see what you mean.

By reading a comment of what you want to do, another person can look at the syntax of your code and see if what you want to do is the same as the code you have written will do.

Off the top of my head I don’t know exactly what the mistake is but I would try a few things.

Did you set up a compressor object elsewhere in your code?
Because if you have then this runs in the background and automaticaly turns the compressor on /off.

I think your rel->Set(Relay::kOn); rel->Set(Relay::kOff);
maybe trying to take control from the compressor object.

I would be looking for instances of where the the compressor relay is set on/off. It may be other examples rel-Set() but I think it more likely to be a compressor object.

Hope this helps.