Go Down

### Topic: store numbers 0 to 1023 into single bytes per number? (Read 577 times)previous topic - next topic

#### socraticbot

##### Jul 22, 2018, 06:54 am
Hi all,

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

Thanks,

Socratic.

#### horace

#1
##### Jul 22, 2018, 07:00 am
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

#### larryd

#2
##### Jul 22, 2018, 07:01 am
A byte can store numbers from 0 through 255.

No technical PMs.
The last thing you did is where you should start looking.

#### Juraj

#3
##### Jul 22, 2018, 08:48 am
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
You can't write an Arduino sketch if you didn't learn programming. Not the language, but the concepts of programming - algorithms and data types.

#### sterretje

#4
##### Jul 22, 2018, 09:45 am
Maybe OP is looking for BCD? If OP can clarify, it would be easier to advise what is required.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

#### socraticbot

#5
##### Jul 22, 2018, 06:05 pm
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.

#### AWOL

#6
##### Jul 22, 2018, 06:10 pmLast Edit: Jul 22, 2018, 06:11 pm by AWOL
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.
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

#### vaj4088

#7
##### Jul 22, 2018, 06:20 pm
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.

#### jremington

#8
##### Jul 22, 2018, 06:29 pm
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.

#### socraticbot

#9
##### Jul 22, 2018, 06:47 pm
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.

#### septillion

#10
##### Jul 22, 2018, 06:50 pm
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.
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake

#### PaulMurrayCbr

#11
##### Jul 22, 2018, 06:58 pm
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.
http://paulmurraycbr.github.io/ArduinoTheOOWay.html

#### socraticbot

#12
##### Jul 22, 2018, 07:10 pm
Thanks everyone. Based on the replies, I think I will have to just do

Code: [Select]

valtowrite = map(probe, 0, 1023, 0, 512);

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

#### PieterP

#13
##### Jul 22, 2018, 07:28 pmLast Edit: Jul 22, 2018, 07:29 pm by PieterP
Thanks everyone. Based on the replies, I think I will have to just do

Code: [Select]

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 valtowrite = probe >> (10 - 8); 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

#### sterretje

#14
##### Jul 22, 2018, 08:29 pmLast Edit: Jul 22, 2018, 08:31 pm by sterretje
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.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Go Up