Tactile Radio Buttons

Creator: Andrew Mascolo (HazardsMind)

This sketch will turn an array of tactile buttons into Radio buttons

CLICK ME to see what radio buttons are.

const byte Buttons[] = {2, 3, 4, 5};

#define SIZE(x) sizeof(x)/sizeof(x[0])

template<class T, size_t N>
byte RadioButtons(T(&_buttons)[N], byte State)
{
  static byte Output = 0;

  for (byte i = 0; i < N; i++)
  {
    if (digitalRead(_buttons[i]) == State)
    {
      Output = i + 1;
      break;
    }
  }
  return Output;
}

void setup() 
{
  // put your setup code here, to run once:

  Serial.begin(115200);
  for (byte i = 0, j = SIZE(Buttons); i < j; i++)
    pinMode(Buttons[i], INPUT_PULLUP);
}

void loop() 
{
  // put your main code here, to run repeatedly:

  // You can fix this part to only show the value if it has changed.
  Serial.println(RadioButtons(Buttons, LOW) );
}

Share