You have to add a (global) variable that stores the last send command (to open/close).
When you send a command, you save it in the global variable.
And you only send a command when it is different than the command send the last time, so only CHANGES are sent.
The best way to do this is to make a specific function, e.g. SetSolenoid(bool open);
#define SOLENOID_OPEN TRUE
#define SOLENOID_CLOSE FALSE
bool _currentSolenoidStatus = false; // Assume closed
In your loop:
...
if ... condition when solenoid should be be opened
{
SetSolenoid(SOLENOID_OPEN);
}
...
if ... condition when solenoid should be be closed
{
SetSolenoid(SOLENOID_CLOSE);
}
Function to open/close solenoid open (only when changed):
bool SetSolenoid(bool status)
{
if (_currentSolenoidStatus != status)
{
// Send status to solenoid
_solenoidStatus = status;
}
}