Forgot the name of a thing..

Hi all

Newb question.

I have a value coming from a source.
I want to look up that value in a table and then output that new value.

As in my value is B.

My table is;

A = 0001
B = 0002
C = 0003

So I would get a result of "0002".

What is this process called?
Any links on basic dumbed down versions on how to implement it on Arduino?
Thanks

Why the fixation with octal?

Lookup?
Stringification?

Index into a table or array.
value = arrayName[index];

byte arrayName[] = {0,2,5,9,13,15,}; // 6 elements from arrayName[0] to arrayName[5]
say index was then 4, received from a Serial.read() perhaps.
value = arrayName[index] =arrayName[4] = 13 with the first element at location 0 and the 4th at location 5.

table look-up. in the arduino world, table almost always translates to 'array'.

If you want to use a letter as the index into an array, you need to consider the numeric vale of the letter, and reduce the offset to zero (to eliminate empty space). or some other mechanism to generate the array lookup value.

This is really confusing.
I guess I am not helping... :stuck_out_tongue:

So I have a sonar measuring in inches, and it is using "inches" as the variable.
This is a number from 0 to 100.

What I need it to do is take this variable "inches" and run it though this array?

So if "inches" is "1" this would output another variable as "0001".
If "inches" is 5, it would output "0005" as the other variable.

Does that make sense?

Does that make sense?

No. No not really. Are you formatting output? If you're using a string table to format output then you'll waste a great deal of RAM. Most of it sitting with 0s in it. Why can't you just use the inches variable? What type is it?

Jimmy60:
Does that make sense?

No. No not really. Are you formatting output? If you're using a string table to format output then you'll waste a great deal of RAM. Most of it sitting with 0s in it. Why can't you just use the inches variable? What type is it?

Yea formatting the output. Can't work out how to do it and most of the people I talked to say it is complicated.
So though this way would be easier as A = 1 and so on.

Formatting the output for what device ... a digital display?
Looks like you're trying to get 4 digits with leading zeros displayed - or is it ASCII?

You want to convert a number to a zero-padded decimal string?

Kind of.

I am trying to display Inches in a better format.

So "1" inch is shown as "0001" but 13 inches is shown as "0101".

So the first two digits are feet and the second are inches.

How about:

feet = depth / 12;
inches = depth % 12;

outsider:
How about:

feet = depth / 12;

inches = depth % 12;

It is more to do with formatting it into a 4 digit number than that.

So something like:

void show_inches (int inches)
{
  int feet = inches / 12 ;
  inches %= 12 ;
  print_2_digits (feet) ;
  print_2_digits (inches) ;
}

void print_2_digits (int val)
{
  if (val < 10)
    Serial.print ('0') ;
  Serial.print (val) ;
}

MarkT:
So something like:

void show_inches (int inches)

{
  int feet = inches / 12 ;
  inches %= 12 ;
  print_2_digits (feet) ;
  print_2_digits (inches) ;
}

void print_2_digits (int val)
{
  if (val < 10)
    Serial.print ('0') ;
  Serial.print (val) ;
}

Could you explain what the important lines do?
Finding it hard to work out what each bit does.

Thanks

  int feet = inches / 12 ;  //divides inches by 12, discards any remainder and puts the result in the feet variable (integer division)
  inches %= 12 ;            //divides inches by 12 and puts the remainder in the inches variable (modulo operator)

UKHeliBob:

  int feet = inches / 12 ;  //divides inches by 12, discards any remainder and puts the result in the feet variable (integer division)

inches %= 12 ;            //divides inches by 12 and puts the remainder in the inches variable (modulo operator)

Ah I get it now and that works.

Trying to format it using SprintF but getting a load of garbage in the serial out.

// UKHeliBob's math to work out Feet and Inches seperatly 
  int feet = inches / 12 ;  //divides inches by 12, discards any remainder and puts the result in the feet variable (integer division)
  inches %= 12 ;            //divides inches by 12 and puts the remainder in the inches variable (modulo operator)

// Test to see if above math works.
//  Serial.print("ft, "); 
//    Serial.println (feet) ;
 // Serial.print("in, ");
 //   Serial.println (inches) ;


// Format Feet and Inches from above into a buffer variable, then print via serial. 
int buffer;
sprintf (buffer, "%02d%02d", feet, inches);
Serial.print (buffer);

Managed to get it to work like this.

char buffer[4];
sprintf (buffer, "%02d%02d", feet, inches);
Serial.println (buffer);

Now I got to work out how to put it into the display.

Thank you all for your help! I might be asking more in a bit once I get stuck again on the monitor side xD

char buffer[4];You need an extra level in the array to store the terminating zero