store numbers 0 to 1023 into single bytes per number?

Hi all,

How can I store numbers 0 to 1023 into a single bytes each number?

Thanks,

Socratic.

a byte can represent an unsigned numeric value 0 to 255 (0 to 0xFF) you can store numbers greater than 255 in two bytes, e.g. a 16 bit int

A byte can store numbers from 0 through 255.

1023 can be stored into 10 bits. byte has 8 bits. you could divide by 4 to store into byte. of course you loose precision

Maybe OP is looking for BCD? If OP can clarify, it would be easier to advise what is required.

I am meaning is there any method to represent numbers 0 through 1023 using a single byte per number? Like an encoding method or compression algorithm? I need to fit numbers 0 to 1023 in EEPROM which has only 512 bytes of storage.

10 bits takes 10 bits.
512 bytes is 4096 bits of EEPROM, so you could store 409 ten bit values.

If the distribution of the values is not random, you could maybe use Huffman coding.

What are you REALLY trying to do? 512 bytes is a LOT of EEPROM.

By the way, someone who does not understand what fits into a byte certainly is not going to understand Huffman encoding.

You can store an index value in one byte, which can point to any of 256 different objects, such as a number between 0 and 1023.

huffman encoding looks like it is based on frequency of occurrence. Not what I'm looking for.

I am trying to record to 512 byte eeprom the positions on a potentiometer. analog read can only go with between 0 and 1023 for this so i am stuck with that resolution.

The potentiometer is made from a zig-zagging resistive ink (graphite) pattern printed on a piece of paper. the end of the line goes to 5v,and the start of line to ground. a probe can touch those points and goes to analog read.

This forms a crude drawing digitizer.

To store a single analogRead() you can just use EEPROM.put() and use 2-bytes. Do you really need to store 100ths of readings? If so, please tell use what you really want to do. Probably Maybe there is a better way.

Other option would be to cut the resolution of you analog read. Depending on the task a resolution of 8-bit might be more then enough.

socraticbot: I am trying to record to 512 byte eeprom the positions on a potentiometer. analog read can only go with between 0 and 1023 for this so i am stuck with that resolution.

Well, 512 bytes is 4096 bits, so you can store 409 ten-bit numbers. The code involves taking a 2-byte buffer (an int) and shifting the bits through it.

If the numbers aren't random, it might be possible to compress them. For instance, if the number from one sample to the next never changes by more than 3, then after the initial 10-bit reading you can just store the offsets as a four-bit signed number.

If the numbers usually change by no more than 3, then you can do the same thing, using a "difference of 4 to signal that the byte stream needs to be restarted with the full 10 bits.

If the numbers tend to form some sort of repeating or cyclic pattern, then LZ compression might work.

Alternatively, you could chop off the low-order 2 bits by dividing by 4 and store the samples on byte per sample, at the cost of some resolution.

But you cannot store 512 ten-bit samples in 512 8-bit bytes if the data isn't amenable to compression.

Thanks everyone. Based on the replies, I think I will have to just do

int probe = analogRead(styluspin);
valtowrite = map(probe, 0, 1023, 0, 512);

and stick with a 512 total pixels square image to be drawn.

socraticbot: Thanks everyone. Based on the replies, I think I will have to just do

int probe = analogRead(styluspin);
valtowrite = map(probe, 0, 1023, 0, 512);

and stick with a 512 total pixels square image to be drawn.

That won't work either. 512 doesn't fit in a byte either, and your conversion is wrong. To do it correctly, you would use [nobbc]valtowrite = probe >> (10 - 8);[/nobbc] where 10 is the ADC resolution, and 8 is the number of bits in a byte.

But why are you saving analog samples into EEPROM in the first place?

I'm sensing a giant XY-problem here.

Pieter

How many points do you have to store? You can always opt for external eeprom, fram or a sd card.

Or if it's a reference pattern, you can store it maybe in flash.

I am trying to make a drawing pad that can digitize a drawing into a coordinate system an arduino can store in eeprom.

I was thinking of some sort of grid that captures row position and column position. so for 512 bytes the first byte address can be the column number, and then next byte address the row number. This would repeat for each coordinate captured.

In the code, a counter would switch to the next row when some sort of differentiator between each column is detected. Not sure how it could detect column differences.

I was thinking some sort of resistive trackpad or traces like some have done with pencil, and paper connected to arduino analog read.

Damn, that's a pretty big project...

But I think I would go for an SD-card for that :)

I decided to go with an arduino compatible board with SD card module.

I'm now switched over to developing a processing app to convert an image into a list of coordinates indicating black pixel locations in text format to be stored on the sd card and read by the arduino without going through eeprom.

socraticbot: a list of coordinates indicating black pixel locations in text format

Oy gevalt.