Converting Array to variable

Hi,
In the Arduino Reference is says that converting an array-value to a variable goes like:

x = mySensVals[4];

So variable x gets the value of the fift element (0…4) in the array.

In my case:


byte Returned[10];
for (byte k=0; k<10; k++)
Returned[k] = mySerial.read();
Serial.print("6: ");Serial.println(Returned[6],HEX); // prints the RIGHT value of the seventh element!

int x=(Returned[6],HEX);

// does not give variable x the same value…

Where do I go wrong? Pls help…

JeroenTeelen:
x = mySensVals[4];

So variable x gets the value of the fift element (0…4) in the array.

yes, unless the array is smaller than 5 elements, then you get a neighbour byte/int/… whatever type of the array

int x=(Returned[6],HEX);

is no meaningfull code.

Serial print has the option to print a number in different represenstations, no option is decimal, here it is chosen to do it hexadecimal.

You cannot assign a variable a hex number (note: numbers are all in some binary format intern)

int x=(Returned[6],HEX);

==>

int x = Returned[6];

Serial.println(x, HEX);

Your indenting is wrong. Use the control-T auto format in the Arduino editor. If it appears to scramble your code then your code was wrong.

And never write a multi-line statement such as for(){} without using braces {}

    for (byte k=0; k<10; k++) {
      Returned[k] = mySerial.read();    
    }   
    Serial.print("6: ");Serial.println(Returned[6],HEX); // prints the RIGHT value of the seventh element!

Presumably somewhere else you checked that there are actually 10 characters available to be read from Serial?

Hi Rob,

Thanks for your fast reply! I'll try to get it right...

Rgds,
Jeroen

Thanks, I was blind for the print-option (DEC, HEX)....
So, int x=(Returned[6]); did a splended job!

robtillaart:
int x=(Returned[6],HEX);

Ahh, the C comma operator! Bane of ne programmers. x will get set to the numeric value of HEX, whatever that might be.

16