Arduino Programming: hydraulic shock control from pushbutton

This was a response to an online request for help with code. A single ¬†pushbutton cycles through three hydraulic damper valve settings. The code was too long to post on the site, so I’m showing it here.

As of today, I don’t know if it will work, but it explains the concept. I will get it working as time permits

// Digital output definitions
#define RED_OUTPUT 0
#define BLUE_OUTPUT 1
#define GREEN_OUTPUT 2

// Digital input definitions
#define SWITCH_INPUT 3

// Define states
#define RED 800
#define BLUE 801
#define GREEN 802

// Define message types
#define NONE 901
#define CLICK 902
#define PRESS 903
   
// Global state
int _state = GREEN;

// One-time call to initialize system
void setup()
{
   pinMode(SWITCH_INPUT, INPUT_PULLUP);
   pinMode(RED_OUTPUT, OUTPUT);
   pinMode(BLUE_OUTPUT, OUTPUT);
   pinMode(GREEN_OUTPUT, OUTPUT);
   // Enable GREEN state
   digitalWrite(GREEN_OUTPUT, HIGH);
}

// Continuously called by Arduino runtime
void loop()
{
   // Read pushbutton
   int message = readSwitch();
   // Update LEDs and hydraulic valve if state changed
   if (processState(message))
   {
      activateOutput(_state);
   }
}

// Read pushbutton and return the result
int readSwitch()
{
   const int CLICK_UPPER_LIMIT = 200;
   const int CLICK_LOWER_LIMIT = 25;
   const int PRESS_LOWER_LIMIT = 1000;
   const int PRESS_UPPER_LIMIT = 2000;

   int switchMessage = NONE;
   unsigned long start = micros();
   while (digitalRead(SWITCH_INPUT) == LOW);
   unsigned long duration = micros() - start;
   if (duration < CLICK_UPPER_LIMIT && duration > CLICK_LOWER_LIMIT)
   {
      switchMessage = CLICK;
   }
   else if (duration > PRESS_LOWER_LIMIT && duration < PRESS_UPPER_LIMIT)
   {
      switchMessage = PRESS;
   }
   return switchMessage;
}

bool processState(int message)
{
   static int lastState = GREEN;
   int retVal = 0;

   // PRESS overrides CLICK in all states
   if (message == PRESS)
   {
      _state = GREEN;
   }
   else
   {
      // Process states
      switch (_state)
      {
         case RED:
            if (message == CLICK)
            {
               _state = BLUE;
            }
         break;
            
         case BLUE:
            if (message == CLICK)
            {
               _state = RED;
            }
         break;
            
         case GREEN:
            if (message == CLICK)
            {
               _state = RED;
            }
         break;
            
         default:
         break;
      }
   }
   // Did state change?
   retVal = (lastState == _state);
   // Remember this state
   lastState = _state;
   // Return state changed information
   return retVal;
}

void activateOutput(int thisState)
{
   // Since this is only called on state change, OK to turn everything off first
   digitalWrite(RED_OUTPUT, LOW);
   digitalWrite(GREEN_OUTPUT, LOW);
   digitalWrite(BLUE_OUTPUT, LOW);
   // Turn on only the output corresponding to the state
   switch (thisState)
   {
       case RED:
         digitalWrite(RED_OUTPUT, HIGH);
         break;         
       case BLUE:
         digitalWrite(BLUE_OUTPUT, HIGH);
         break;      
       case GREEN:
         digitalWrite(GREEN_OUTPUT, HIGH);
         break;
      default:
         break;
   }   
}