"My company makes a device with a movable wheel that's connected to a computer," Geoff writes, "the device incorporates a design where notches in the wheel encode a binary pattern that can be read by sensors. The computer determines the position of the wheel by reading the sensors from a digital I/O card."

"It's actually a clever way of putting an impromptu encoder on the wheel. The software is, unfortunately, not so clever."

#define POS_0     0
#define POS_1    01
#define POS_2    10
#define POS_3    11
#define POS_4   100
#define POS_5   101
#define POS_6   110
#define POS_7   111
#define POS_8  1000
#define POS_9  1001

...snip...

int bit0 = DIO_Input & 1;
int bit1 = DIO_Input & 2;
int bit2 = DIO_Input & 4;
int bit3 = DIO_Input & 8;

// normalize
if (bit0 != 0) bit0 = 1;
if (bit1 != 0) bit1 = 1;
if (bit2 != 0) bit2 = 1;
if (bit3 != 0) bit3 = 1;

Position = 1000*bit3 + 100*bit2 + 10*bit1 + bit0;

...snip...

// Which position is engaged?
switch (Position)
{
   case POS_0: {
       pWidget->nCurrentPosition = 0;
       pWidget->sCurrentPosition = pWidget->m_sPositionName_0;
       break;
       }
   case POS_1: {
       pWidget->nCurrentPosition = 1;
       pWidget->sCurrentPosition = pWidget->m_sPositionName_1;
       break;
       }
  ...snip...
   case POS_9: {
       pWidget->nCurrentPosition = 9;
       pWidget->sCurrentPosition = pWidget->m_sPositionName_9;
       break;
       }
   }
[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!