Getting data from a function and putting it in a variable.

Hey there,

I found this code online:

#include <OneWire.h>
OneWire ds(6);                    // OneWire bus on digital pin 6
void setup()
{
  Serial.begin (9600);
  delay(250);
}
void loop()
{
  byte i;           // This is for the for loops
  boolean present;  // device present varj
  byte data[8];     // container for the data from device
  byte crc_calc;    //calculated CRC
  byte crc_byte;    //actual CRC as sent by DS2401
  String test;
  //1-Wire bus reset, needed to start operation on the bus,
  //returns a 1/TRUE if presence pulse detected
  present = ds.reset();
  if (present == TRUE)
  {
    Serial.println("---------- Device present ----------");
    ds.write(0x33);  //Send Read data command
    data[0] = ds.read();
    Serial.print("Family code: 0x");
    PrintTwoDigitHex (data[0], 1);
    Serial.print("Hex ROM data: ");
    for (i = 1; i <= 8; i++)
    {
      data[i] = ds.read(); //store each byte in different position in array
      test = PrintTwoDigitHex (data[i], 0);
      Serial.print(" ");
    }
    Serial.println();
    Serial.println(test);
    Serial.println();
    crc_byte = ds.read(); //read CRC, this is the last byte
    crc_calc = OneWire::crc8(data, 7); //calculate CRC of the data
    Serial.print("Calculated CRC: 0x");
    PrintTwoDigitHex (crc_calc, 1);
    Serial.print("Actual CRC: 0x");
    PrintTwoDigitHex (crc_byte, 1);
  }
  else //Nothing is connected in the bus
  {
    Serial.println("xxxxx Nothing connected xxxxx");
  }
  delay(8000);
}
String PrintTwoDigitHex (byte b, boolean newline)
{
  Serial.print(b/16, HEX);
  Serial.print(b%16, HEX);
  if (newline) Serial.println();
}

And i want the HEX rom data in a string variable complete. but the way it’s being built up is: it loops 6 times thrue the PrintTwoDigitHex function. And i have no idea how i can have a variable String test for example where the value would be: FF FF FF FF FF FF. Can anyone help me figure this out? Thanks in advance :slight_smile:

Regards,

memcmp?

AWOL:
memcmp?

AWOL:
memcmp?

Would it be something like this then in my function?

String PrintTwoDigitHex (byte b, boolean newline)
{
  char var1[] = b/16, HEX;
  char var2[] = b%16, HEX;
  if (newline) Serial.println();
  return var1, var2;
}

edit: accidently posted it before finishing my code.. :frowning:

B19920:
Would it be something like this then in my function?

String PrintTwoDigitHex (byte b, boolean newline)

{
  char var1 = b/16, HEX;
  char var2 = b%16, HEX;
  if (newline) Serial.println();
  return var1, var2;
}





edit: accidently posted it before finishing my code.. :(

And then in my loop i do something like:

String test;
test=memcmp ( var1, var2, sizeof(var1) );

B19920:
And then in my loop i do something like:

String test;

test=memcmp ( var1, var2, sizeof(var1) );

Hi,

I dont think that works, that is, the return var1,var2; type of statement because i think the only thing returned is the var2 then.

What you could do is either use a struct or an array. Call the function and have the function load the array, then read the array after the return.
You can pass the array if you want to do it that way, or just declare it outside of any functions and just load and read it as you would any other array.
If you prefer, use a struct.

Since you are calling it Print something, you may be able to just print it right there in the function and then you dont have to return anything.

  char var1[] = b/16, HEX;

What are the values in the array after that statement is executed? How are those values meaningful?

The comma operator is being misused.