Sent different sized variables over serial

I'm using an ADC wich is 24-bits resolution. So I save each byte in a different addres of the EEPROM. Now I want to read the data from the EEPROM and show it out using the arduino serial port monitor. So I take the 3 bytes and mix them to get a 24-bit numer. For example:

byte 1=1F
byte 2=2F

Hello,

I'm using an ADC with a 24-bits resolution. So I save each byte in a different EEPROM address. Now I want to read the data from the EEPROM and show it out using the arduino serial port monitor. So I take the 3 bytes and mix them to get a 24-bit numer. For example:

byte 1=1F
byte 2=2F
byte 3=FF

So I get----> 1F 2F FF

I'm using this code:

#include <EEPROM.h>

int address = 1;
byte value;
bool flag=0;
byte Valor[3];
int Val = 0;

void loop()
{
// read a byte from the current address of the EEPROM
for(int i=1;i<=3;i++){
value = EEPROM.read(address);
Valor*=value;*

  • address = address + 1;*
  • }*

Val=(Valor[1] << 16)+(Valor[2] << 8)+Valor[3];

  • Serial.print("Val=");*

  • Serial.print(Val);*

  • Serial.print(";Valor[1]=");*

  • Serial.print(Valor[1]);*

  • Serial.print(";Valor[2]=");*

  • Serial.print(Valor[2]);*

  • Serial.print(";Valor[3]=");*

  • Serial.print(Valor[3]);*

  • Serial.println();*
    if (address == EEPROM.length()) {

  • flag=1;*

  • while(flag=1){}*

  • }*

  • delay(5);*
    }
    I notice that if the variable I'm sending through the serial port is more than 16 bits does not work(the value I visulaize is "0"). There is any possibility to visualize a 24 bit variable in the serial port monitor?
    Thank you in advance

The way you read your EEPROM is wrong, look at this approach:

for(int i = 0;i < 3; i++) Valor[i] = EEPROM.read(address + i);

EDIT: You could also just do:

EEPROM.get(address, Valor);

Your valor array has three elements but their indices are 0,1 and 2 not 1,2 and 3.

You're trying to push 24 bits into a sixteen bit int. Best make val a long instead.

Val=(Valor[1] << 16)+(Valor[2] << 8)+Valor[3];

What is your 8 bit value going to look like after you've shifted it 16 places to the left?

You need to cast Valor[n] to an unsigned long BEFORE applying the shift operator to it.

Danois90:
The way you read your EEPROM is wrong, look at this approach:

for(int i = 0;i < 3; i++) Valor[i] = EEPROM.read(address + i);

EDIT: You could also just do:

EEPROM.get(address, Valor);

I don't have problems in reading the EEPROM(I saved some values in the EEPROM first and I read them well). In fact if I do what you propose I would be reading all the time the same three registers address. My address start at 1 so the sequens would be: 1,2,3 and start again 1,2,3. I need to increment the address by one every time I read from the EEPROM.

Danois90:
The way you read your EEPROM is wrong, look at this approach:

for(int i = 0;i < 3; i++) Valor[i] = EEPROM.read(address + i);

EDIT: You could also just do:

EEPROM.get(address, Valor);

PaulS:

Val=(Valor[1] << 16)+(Valor[2] << 8)+Valor[3];

What is your 8 bit value going to look like after you've shifted it 16 places to the left?

You need to cast Valor[n] to an unsigned long BEFORE applying the shift operator to it.

Yes, you are right I did not think about that...I declared Valor[n] as unsigned long but now the serial port monitor is not working...
Do you know the reason?

thank you

It's not valor that needs to be unsigned long but rather val. You're still using the wrong indices too.

Joaremi:
I don't have problems in reading the EEPROM(I saved some values in the EEPROM first and I read them well). In fact if I do what you propose I would be reading all the time the same three registers address. My address start at 1 so the sequens would be: 1,2,3 and start again 1,2,3. I need to increment the address by one every time I read from the EEPROM.

You do know that the first byte in EEPROM start at address 0? If you want to incrementally read 3 bytes for each iteration of "loop()" then just do:

EEPROM.get(address, Valor);
address += 3;
if (address >= EEPROM.length()) {
  //Finished reading
}

But this code is not safe (neither is the code you posted), you may end up reading beyond size of EEPROM. You could avoid this by making the first line in "loop()" look like this:

if (address + 3 >= EEPROM.length()) return;

wildbill:
It's not valor that needs to be unsigned long but rather val. You're still using the wrong indices too.

I changed both(val,valor) before send the las post(I did not say it, sorry). I don't understand why the indices are wrong, I start "i" with 1 and end it with 3, so I get three values: Valor[1],Valor[2] and Valor[3]...

C/C++ arrays are indexed from zero, not one. Your use of valor[3] is outside the bounds of the array.

Multiply the bytes to get the correct value into a single variable - probably a long variable - and then print the variable.

Something like

long totalValue = highByte * 256 * 256 + middleByte * 256 + lowByte
Serial.println(totalValue);

Or it might be more logical (and faster) to do it with left shift instructions - like this

long totalValue = (highByte << 16) + (middleByte <<8) + lowByte;

...R

...except that you can't shift left a byte variable by 8 or 16 places :wink:
You need to cast the byte to an unsigned int, or unsigned long before shifting.

long totalValue = ((unsigned long)highByte << 16) + ((unsigned long)middleByte <<8) + lowByte;

CtrlAltElite:
...except that you can't shift left a byte variable by 8 or 16 places :wink:

Thank you for spotting that.

...R

@Joaremi, why have you started a second Thread with the same question. I gave you an answer in your earlier Thread.

...R

Robin2:
@Joaremi, why have you started a second Thread with the same question. I gave you an answer in your earlier Thread.

...R

Robin2:
@Joaremi, why have you started a second Thread with the same question. I gave you an answer in your earlier Thread.

...R

Sorry, I sent the question before I finished to write it by error...the question was unfinished...and I didn't know how to delete it

MOderator merged related topics.