Binary Decoder - Help Adding Values

Hello,

I recently developed a binary code to communicate, and would like to make an arduino-based decoder. The code is simple, A-Z starting with 00001 and ending with 11010. It is a 5-bit code, and I am having trouble decoding it using a DFROBOT LCD Keypad Shield. I have already mapped all of the buttons, but I don’t know how to decode the message by tacking on numbers.(ex. 1101 + 0 = 11010). I have attached the code below, and any help would be appreciated. Thanks!

//Sample using LiquidCrystal library
#include <LiquidCrystal.h>

// select the pins used on the LCD panel
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

// define some values used by the panel and buttons
int lcd_key     = 0;
int x = 0; // initial value of decoder
int adc_key_in  = 0;
#define btnRIGHT  0
#define btnUP     1
#define btnDOWN   2
#define btnLEFT   3
#define btnSELECT 4
#define btnNONE   5
// read the buttons
int read_LCD_buttons()
{
 adc_key_in = analogRead(0);      // read the value from the sensor 
 if (adc_key_in > 1000) return btnNONE; 
 if (adc_key_in < 50)   return btnRIGHT;  
 if (adc_key_in < 195)  return btnUP; 
 if (adc_key_in < 380)  return btnDOWN; 
 if (adc_key_in < 555)  return btnLEFT; 
 if (adc_key_in < 790)  return btnSELECT;   
 return btnNONE;  // Return if others fail
}
void setup()
{
 Serial.begin(9600);
 lcd.begin(16, 2);              // start the LCD
 lcd.setCursor(0,0);
}
void loop()
{

 lcd.setCursor(0,0);            // move to the top left
 lcd_key = read_LCD_buttons();
  Serial.println();// read the buttons
 switch (lcd_key)               // depending on which button was pushed, we perform an action
 {
   case btnRIGHT:
     {
      lcd.print(x,BIN);
     x=;
     break;
     }
   case btnLEFT:
     {
   
     break;
     }
   case btnUP:
     {
    
     break;
     }
   case btnDOWN:
     {
    
     break;
     }
   case btnSELECT:
     {
     
     break;
     }
     case btnNONE:
     {
     
     break;
     }
 }

}

Is that ALL the code?

I have already mapped all of the buttons

To what are they mapped? What is supposed to happen when you push "btnLEFT"?

I don't know how to decode the message

What message?

What is the purpose of a line like this?

    x=;

Please explain what you are actually trying to do, in simple, straightforward terms.

Finally, has it escaped your notice that your "code" is just the ASCII value of the upper case letter, minus the ASCII value for 'A', plus 1?

Do you mean you want each button to represent a position in a binary number: 11001 for example?

jremington:
Is that ALL the code?
To what are they mapped? What is supposed to happen when you push "btnLEFT"?
What message?

What is the purpose of a line like this?

    x=;

Please explain what you are actually trying to do, in simple, straightforward terms.

Finally, has it escaped your notice that your "code" is just the ASCII value of the upper case letter, minus the ASCII value for 'A', plus 1?

boolrules:
Do you mean you want each button to represent a position in a binary number: 11001 for example?

I am sorry for not being specific enough.

Yes, that is my entire code. They are mapped to an LCD Keypad Shield, but that should not really matter for this part of the project. When I mean "mapped," I mean that I have a case for each button. The two buttons that I would like to use are the "btnRIGHT" and "btnLEFT".

I would like to decode encoded messages (A secret code from the regular alphabet to a 5-bit binary code, and this would be the instrument to decode it).

To decode the encoded messages, you press the left button to add a 0 or the right one to add a 1. So, if you had 01001, you press LEFT RIGHT LEFT LEFT RIGHT, and the decoder translates that into an alphabetical value above the 5 digits.

To answer your question about "x=;", that was just a start of a function that I do not know how to complete (displaying the numbers in a line as opposed to adding them).

Thanks,

Opencircut74

Just for kicks, try this magic program:

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  for (int i = 1; i < 27; i++) {
    Serial.print(i, BIN);
    Serial.print(" : ");
    Serial.println((char) ('A' + i - 1));
  }
}

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

Create an array of 5 characters.
In a for loop of length 5, record your key press as appropriate.
Read the array data successively multiplying by 16, 8, 4, 2 and 1 accumulating their sum.
Add 0x40 to the sum.

boolrules:
Create an array of 5 characters.
In a for loop of length 5, record your key press as appropriate.
Read the array data successively multiplying by 16, 8, 4, 2 and 1 accumulating their sum.
Add 0x40 to the sum.

Thanks for figuring that out for me. Because I don't have much experience with arrays, could you mind telling me what to put for the array, and how to record and display the characters? Thank you.

Another approach to using the keypad to enter a 5 binary digit number:

int x=0;
for (int i=1; i<6; i++) { //loop over 5 key presses
x = 2*x;  //binary weighting of previous entries
if(get_key() == 1) x=x+1; //get_key waits until a valid key has been pressed, then returns either 1 or 0
}
Serial.println((char) (x+'A'-1));

jremington:
Another approach to using the keypad to enter a 5 binary digit number:

int x=0;

for (int i=1; i<6; i++) { //loop over 5 key presses
x = 2*x;
if(get_key() == 1) x=x+1; //get_key waits until a valid key has been pressed, then returns either 1 or 0
}
Serial.println((char) (x+‘A’-1));

I entered the code, but “get_key” has no variable, so it gives me an error. What should I do about this?

opencircut74:
I entered the code, but "get_key" has no variable, so it gives me an error. What should I do about this?

Write a function called get_key() that does what that comment says it should do.

byte get_key(){

while (neither button is pressed) ;  // do nothing

if (left button is pressed) return 0;
return 1;   // right button must be pressed
}

Array is not really required.
Change names/values to fit your hardware.
Untested.

void loop( ) {
    // probably need some housekeepping here
    char decode = get_value( );
    // print decode
}
    
char get_value( ) {
    char decoded = 0;
    char multi = 16;
    
    while( multi ) {
        if( digitalRead( LEFT_BUTTON ) == 1 ) {     // this is a "0"
            multi = multi >> 1;
            debounce( LEFT_BUTTON );                // you can write your own debounce
        }
        if( digitalRead( RIGHT_BUTTON ) == 1 ) {    // this is a "1"
            decoded += multi;
            multi = multi >> 1;
            debounce( RIGHT_BUTTON );               // you can write your own debounce
        }
    }
    return decoded += 0x40;
}