Sending a function an array and returning a float - my syntax is wrong

Hi everyone,

I am embarrassed to ask this but I just cannot get my syntax right.

From my loop() I want to send a function an array [size of 8 bytes] And return a float value to my loop()

The best I can do is my code below but it will not compile without errors. I am amazed I got this far.

  float MyTemp;
  float Myresult;
  byte THeat_Addr[8];
  
  void setup() {
   THeat_Addr[0]=0x28; // physical address of the thermocouple
   THeat_Addr[1]=0xA9;
   THeat_Addr[2]=0xB7;
   THeat_Addr[3]=0x58;
   THeat_Addr[4]=0x07;
   THeat_Addr[5]=0x00;
   THeat_Addr[6]=0x00;
   THeat_Addr[7]=0x74;
// there are a few addresses and I want to reuse the code

}

void loop() {
  MyTemp = float Readtemps(THeat_Addr);
  // do stuff with MyTemp value returned from the function

}

// Location of a function requiring an array of 8 byte
// And a return of a float value back to the loop() function

Readtemps(byte Addr){ // I don't know the correct syntax

byte MyNewAddress(Addr); // use the array pointing to the address here.

// do other stuff and put the floating value into Myresult
  return Myresult;

}

I am hoping someone can see the mistakes just looking at it. :)

There are a host of problems with the code but I am not clear what you are trying to do anyway.

A couple of the problems :
The function has no type. If you are going to return a value from a function than the type must precede the name of the function otherwise precede it with void to indicate that no value will be returned…

You try to return Myresult from the function but it has not been given a value in the function
byte MyNewAddress(Addr);I have no idea what you are trying to do here.

Please explain what you want to do.
It looks like you want to call a function with an index to an array and get back the value at that index. If so then you don’t need a function, just an array.

Maybe you want to have something like

byte THeat_Addr[][8] = {
  { 0x28, 0xA9, 0xB7, 0x58, 0x07, 0x00, 0x00, 0x74 },
  { 0x29, 0xA9, 0xB7, 0x58, 0x07, 0x00, 0x00, 0x74 },
};

float Temp;

void setup() {
  Temp = readTemps(THeat_Addr[0]);
  // do stuff with MyTemp value
}

float readTemps(byte* Addr) {
  float Result = 19.34;

  if (Addr[0] == 0x28) { // use the array pointing to the address here.
    // do other stuff and put the floating value into Myresult
  }
  return Result;
}

void loop() {}

UKHeliBob: There are a host of problems with the code

Thanks. Yes it is confusing. :)

I have up until recently just written all code in the void loop() section and I need some discipline to make it logical.

The array is a #onewire address to a thermocouple. I have several thermocouples and wanted to improve my spaghetti code with some logical steps.

Hence my goal to understand Functions and Returns. I send a function the 8 byte of address. I read the thermocouple data and move it to a float and return the float value.

The addresses sent to the function will change for each onewire thermocouple. The result will be the thermocouple temperature as a float.

Dave

Whandall: Maybe you want to have something like

byte THeat_Addr[][8] = { { 0x28, 0xA9, 0xB7, 0x58, 0x07, 0x00, 0x00, 0x74 }, { 0x29, 0xA9, 0xB7, 0x58, 0x07, 0x00, 0x00, 0x74 }, };

....

float readTemps(byte* Addr)

Awesome, I think I understand this.

The address allocation is awesome. thanks. much more orderly.

Byte* - I was reading a post trying to understand this the other day and it was 2 guys arguing the proper use / mechanics of the asterix.

Is it correct to think of it as an array wildcard? Is there a thread where this has a good explanation?

Is it correct to think of it as an array wildcard

Absolutely not. The * indicates that the variable is a pointer to a value rather than the value itself.