Need a little assistance with binary to text changes

Hi,

ok.. firstly allow me to explain what i am attempting, in short a simple typewriter system that uses shift registers to detect if the incomming signal is a 1 or 0, which is then transfered to the arduino (note i know there will be an easier and much more economic way of doing this, im a newbie, learning and trying)

anyway, i have an arduino script which currently works, it takes the reading from the shift register and prints it on the lcd screen that is on the same system. so i end up with a row of binary numbers

now what i am wanting to do, is set different values for each of those binary numbers, for example

00000001 = yes
00000010 = no
00000100 = maybe

etc, now I know there is a code that you can use as a form of lookup table, just cant find any resource for it, any help would be appreciated.

the outcome - Instead of printing 00000001 on the lcd screen, i want the word Yes instead.
etc
etc

Thank you for your help, a link or even assistance will help no end, thank you for your time

something like this?

if (answer == 0b00000001) {
 Serial.print("Yes!");
}
else if ( answer == 0b00000010) {
 Serial.print("No!");
}

or like this

switch(answer) {
  case 0b00000001:
   Serial.print("Yes");
   break:
 
  case 0b00000010:
   Serial.print("No");
   break:
}

consider a data driven approach


struct Code {
    byte        val;
    const char *text;
};

Code codes [] = {
    { 0b0000001, "yes" },
    { 0b0000010, "no" },
    { 0b0000101, "maybe" },
};
#define N_CODE  (sizeof(codes)/sizeof(Code))

const char *
decode (
    byte   val )
{
    for (unsigned i = 0; i < N_CODE; i++)
        if (val == codes [i].val)
            return codes [i].text;
    return "unknown code";
}


// -----------------------------------------------------------------------------
enum { OK, Err0 = -1, Err1 = -2 };
int
textToBin (
    char *buf )
{
    if (8 > strlen (buf))
        return Err0;
    byte val = 0;
    for (int i = 0; i < 8; i++)  {
        val <<= 1;
        if ('1' == buf [i])
            val  |= 1;
        else if ('0' != buf [i])
            return Err1;
    }
        
    return val;
}

// -----------------------------------------------------------------------------
char buf [80];

void loop ()
{
    if (Serial.available ())  {
        int n = Serial.readBytesUntil ('\n', buf, sizeof(buf));
        buf [n] = '\0';

        int val = textToBin (buf);
        if (0 > val)
            Serial.println ("invalid input");
        
        else
            Serial.println (decode (val));
    }
}

// -----------------------------------------------------------------------------
void setup()
{
    Serial.begin (9600);
}
1 Like

Most simple computer algorithms just duplicate "what you would do". Except that the human mind does a lot of things by habit, you need to slow down a bit and understand what you are doing. Then you can code it fairly easily.

First, forget about the computer and imagine your target, a lookup table (columns of number). Ask yourself, how you would perform a lookup...

  1. start at the top
  2. is the first number the one you are looking for? Yes - look across to the next column and there is your value. No - try next number
  3. look at the next number
  4. Reach the end? Then the lookup failed - not found, must be out of range.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.