Pages: [1] 2   Go Down
Author Topic: passing argument to a function  (Read 576 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I tried to write a simple function reading temperature with a single argument as the probe address:

static byte sonde_Ballon[8] = {0x28 , 0xA5 , 0xDD , 0x5A ,  0x03 , 0x00 , 0x00 , 0x3C } ;

function: 
void getTemperature( byte Sonde) {

call:
 getTemperature(*sonde_Ballon) ;
   I got these errors I don't really understand:

Monitor_Chaudiere.cpp: In function ‘void getTemperature(byte)’:
Monitor_Chaudiere.cpp:721:29: error: invalid conversion from ‘byte’ to ‘const uint8_t*’
Monitor_Chaudiere.cpp:721:29: error:   initializing argument 1 of ‘static uint8_t OneWire::crc8(const uint8_t*, uint8_t)’
Monitor_Chaudiere.cpp:721:41: error: invalid types ‘byte[int]’ for array subscript
Monitor_Chaudiere.cpp:722:12: error: return-statement with a value, in function returning 'void'
Monitor_Chaudiere.cpp:732:18: error: invalid conversion from ‘byte’ to ‘const uint8_t*’
Monitor_Chaudiere.cpp:732:18: error:   initializing argument 1 of ‘void OneWire::select(const uint8_t*)’
Monitor_Chaudiere.cpp:738:18: error: invalid conversion from ‘byte’ to ‘const uint8_t*’
Monitor_Chaudiere.cpp:738:18: error:   initializing argument 1 of ‘void OneWire::select(const uint8_t*)’
Monitor_Chaudiere.cpp:745:4: error: invalid type argument of unary ‘*’
Monitor_Chaudiere.cpp: At global scope:
Monitor_Chaudiere.cpp:841:1: error: expected declaration before ‘}’ token
Is there a problem with the byte variable. I don't find good example on the web.
Thanks for your help


     
Logged

North Queensland, Australia
Online Online
Edison Member
*
Karma: 74
Posts: 2211
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Make sure Arduino.h is included if this code isn't in the main sketch file, byte is declared in that header.

Those errors are from the code inside your function it looks like.

Post the code!
Logged


Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 221
Posts: 13848
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

from the snippet you posted I assume you are using the Dallas Temperature Control Library

use getTemperature(sonde_Ballon);  // without the *

as getTemperature expects a pointer to the address array.
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

with Arduino.h included and without the * pointer I got these same  errors messages: Yes, I use DS18B20.

Monitor_Chaudiere.cpp: In function ‘void getTemperature(byte)’:
Monitor_Chaudiere.cpp:722:29: error: invalid conversion from ‘byte’ to ‘const uint8_t*’
Monitor_Chaudiere.cpp:722:29: error:   initializing argument 1 of ‘static uint8_t OneWire::crc8(const uint8_t*, uint8_t)’
Monitor_Chaudiere.cpp:722:41: error: invalid types ‘byte[int]’ for array subscript
Monitor_Chaudiere.cpp:723:12: error: return-statement with a value, in function returning 'void'
Monitor_Chaudiere.cpp:733:18: error: invalid conversion from ‘byte’ to ‘const uint8_t*’
Monitor_Chaudiere.cpp:733:18: error:   initializing argument 1 of ‘void OneWire::select(const uint8_t*)’
Monitor_Chaudiere.cpp:739:18: error: invalid conversion from ‘byte’ to ‘const uint8_t*’
Monitor_Chaudiere.cpp:739:18: error:   initializing argument 1 of ‘void OneWire::select(const uint8_t*)’
Monitor_Chaudiere.cpp:746:4: error: invalid type argument of unary ‘*’
Monitor_Chaudiere.cpp: In function ‘void loop()’:
Monitor_Chaudiere.cpp:814:33: error: invalid conversion from ‘byte*’ to ‘byte’
Monitor_Chaudiere.cpp:814:33: error:   initializing argument 1 of ‘void getTemperature(byte)’
Monitor_Chaudiere.cpp: At global scope:
Monitor_Chaudiere.cpp:842:1: error: expected declaration before ‘}’ token

the complete subroutine is there:

Code:
void getTemperature( byte Sonde) {
    
// boolean getTemperature(float *temp)
  byte data[9], addr[8];
  // data : Données lues depuis le scratchpad
  // addr : adresse du module 1-Wire détecté
  
  
  if (OneWire::crc8(Sonde, 7) != Sonde[7]) // Vérifie que l'adresse a été correctement reçue
    return false;                        // Si le message est corrompu on retourne une erreur
 
  /* if (Sonde[0] != DS18B20) // Vérifie qu'il s'agit bien d'un DS18B20
    return false;         // Si ce n'est pas le cas on retourne une erreur
   // Serial.print("0x");
        for (int i=0; i<8; i++) {
          if (Sonde[i]<0x10) {Serial.print("0");}
          Serial.print(Sonde[i],HEX);
          Serial.print(" "); } */
  ds.reset();             // On reset le bus 1-Wire
  ds.select(Sonde);        // On sélectionne le DS18B20
  
  ds.write(0x44, 1);      // On lance une prise de mesure de température
  delay(800);             // Et on attend la fin de la mesure
  
  ds.reset();             // On reset le bus 1-Wire
  ds.select(Sonde);        // On sélectionne le DS18B20
  ds.write(0xBE);         // On envoie une demande de lecture du scratchpad
 
  for (byte i = 0; i < 9; i++) // On lit le scratchpad
    data[i] = ds.read();       // Et on stock les octets reçus
  
  // Calcul de la température en degré Celsius
  *temp = ((data[1] << 8) | data[0]) * 0.0625;
    
  // Pas d'erreur
  // return true;
 }

 this routine work perfectly without argument and  a  ds.select(address);  anyone ds18B20 plugged.

Thanks
Dominique

update moderator: added code tags
« Last Edit: February 16, 2014, 12:29:04 pm by robtillaart » Logged

East Anglia (UK)
Online Online
Faraday Member
**
Karma: 117
Posts: 4329
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That code looks really pretty mostly in italics and the cool smiley makes a change from normal boring looking code too..
Please post it again in code tags as suggested in the stickies at the top of this forum.  That will stop it being mangled when it comes across
Code:
[i]
in the code which turns on italics, and will prevent 
Code:
8)
turning into a smiley.

Once we can read the code we may be able to give some help.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

here is the example which works, followed bythe example which works:

Code:
#include <OneWire.h> // Inclusion de la librairie OneWire
 
#define DS18B20 0x28     // Adresse 1-Wire du DS18B20
#define BROCHE_ONEWIRE 10 // Broche utilisée pour le bus 1-Wire
 
OneWire ds(BROCHE_ONEWIRE); // Création de l'objet OneWire ds
byte sonde_Ballon[8] = {0x28 , 0xA5 , 0xDD , 0x5A ,  0x03 , 0x00 , 0x00 , 0x3C } ;
 
// Fonction récupérant la température depuis le DS18B20
// Retourne true si tout va bien, ou false en cas d'erreur
boolean getTemperature(float *temp){
  byte data[9], addr[8];
  // data : Données lues depuis le scratchpad
  // addr : adresse du module 1-Wire détecté
  if (!ds.search(sonde_Ballon)) {
    // Serial.println("pas trouvee") ;
   // if (!ds.search(addr)) { // Recherche un module 1-Wire
    ds.reset_search();    // Réinitialise la recherche de module
    return false;         // Retourne une erreur
  }
   
  if (OneWire::crc8(sonde_Ballon, 7) != sonde_Ballon[7]) // Vérifie que l'adresse a été correctement reçue
    return false;                        // Si le message est corrompu on retourne une erreur
 
  if (sonde_Ballon[0] != DS18B20) // Vérifie qu'il s'agit bien d'un DS18B20
    return false;         // Si ce n'est pas le cas on retourne une erreur
   // Serial.print("0x");
        for (int i=0; i<8; i++) {
          if (sonde_Ballon[i]<0x10) {Serial.print("0");}
          Serial.print(sonde_Ballon[i],HEX);
          Serial.print(" "); }
  ds.reset();             // On reset le bus 1-Wire
  ds.select(sonde_Ballon);        // On sélectionne le DS18B20
   
  ds.write(0x44, 1);      // On lance une prise de mesure de température
  delay(800);             // Et on attend la fin de la mesure
   
  ds.reset();             // On reset le bus 1-Wire
  ds.select(sonde_Ballon);        // On sélectionne le DS18B20
  ds.write(0xBE);         // On envoie une demande de lecture du scratchpad
 
  for (byte i = 0; i < 9; i++) // On lit le scratchpad
    data[i] = ds.read();       // Et on stock les octets reçus
   
  // Calcul de la température en degré Celsius
  *temp = ((data[1] << 8) | data[0]) * 0.0625;
   
  // Pas d'erreur
  return true;
}
 
// setup()
void setup() {
  Serial.begin(9600); // Initialisation du port série
}
 
// loop()
void loop() {
  float temp;
   
  // Lit la température ambiante à ~1Hz
  if(getTemperature(&temp)) {
     

and the exemple which works:
    // Affiche la température
    Serial.print("Temperature : ");
    Serial.print(temp);
    Serial.write(176); // caractère °
    Serial.write('C');
    Serial.println();
  }

 and my routine, I just want to pass the id of DS18B20 as an argument:

[code]void getTemperature( byte Sonde) {
   
// boolean getTemperature(float *temp)
  byte data[9], addr[8];
  // data : Données lues depuis le scratchpad
  // addr : adresse du module 1-Wire détecté
 
   
  if (OneWire::crc8(Sonde, 7) != Sonde[7]) // Vérifie que l'adresse a été correctement reçue
    return false;                        // Si le message est corrompu on retourne une erreur
 
  /* if (Sonde[0] != DS18B20) // Vérifie qu'il s'agit bien d'un DS18B20
    return false;         // Si ce n'est pas le cas on retourne une erreur
   // Serial.print("0x");
        for (int i=0; i<8; i++) {
          if (Sonde[i]<0x10) {Serial.print("0");}
          Serial.print(Sonde[i],HEX);
          Serial.print(" "); } */
  ds.reset();             // On reset le bus 1-Wire
  ds.select(Sonde);        // On sélectionne le DS18B20
   
  ds.write(0x44, 1);      // On lance une prise de mesure de température
  delay(800);             // Et on attend la fin de la mesure
   
  ds.reset();             // On reset le bus 1-Wire
  ds.select(Sonde);        // On sélectionne le DS18B20
  ds.write(0xBE);         // On envoie une demande de lecture du scratchpad
 
  for (byte i = 0; i < 9; i++) // On lit le scratchpad
    data[i] = ds.read();       // Et on stock les octets reçus
   
  // Calcul de la température en degré Celsius
  *temp = ((data[1] << 8) | data[0]) * 0.0625;
   
  // Pas d'erreur
  // return true;
 }
: [/code]
Logged

New Jersey
Online Online
Faraday Member
**
Karma: 70
Posts: 3727
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Rather than:
Code:
void getTemperature( byte Sonde)
try:
Code:
void getTemperature( byte *Sonde)
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

nothing new with *Sonde:

Monitor_Chaudiere.cpp: In function ‘void getTemperature(byte*)’:
Monitor_Chaudiere.cpp:722:12: error: return-statement with a value, in function returning 'void'
Monitor_Chaudiere.cpp:745:4: error: invalid type argument of unary ‘*’
Monitor_Chaudiere.cpp: In function ‘void loop()’:
Monitor_Chaudiere.cpp:813:34: error: invalid conversion from ‘byte’ to ‘byte*’
Monitor_Chaudiere.cpp:813:34: error:   initializing argument 1 of ‘void getTemperature(byte*)’
Monitor_Chaudiere.cpp: At global scope:
Monitor_Chaudiere.cpp:841:1: error: expected declaration before ‘}’ token
Logged

New Jersey
Online Online
Faraday Member
**
Karma: 70
Posts: 3727
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That's because you have unrelated errors. The first of them refers to trying to return a boolean from a function you've declared to be void rather than bool.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

sorry in the code I attached I mixed the example followed by my routine. In the example there is a return boolean but in my code there is not I comment.
Logged

New Jersey
Online Online
Faraday Member
**
Karma: 70
Posts: 3727
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you're still having trouble, probably best if you post your latest code.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 633
Posts: 50201
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
If you're still having trouble, probably best if you post your latest code.
All of it, and NOTHING else.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

code too big (>9500) I try to attach the  file .ino

* Monitor_Chaudiere.ino (21.08 KB - downloaded 11 times.)
Logged

New Jersey
Online Online
Faraday Member
**
Karma: 70
Posts: 3727
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Still the same issue (among others):
Code:
void getTemperature( byte *Sonde) {
  byte data[9], addr[8];
  if (OneWire::crc8(Sonde, 7) != Sonde[7]) // Vérifie que l'adresse a été correctement reçue
    return false;                        // Si le message est corrompu on retourne une erreur
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 221
Posts: 13848
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

if you remove these 2 lines
Code:
  if (OneWire::crc8(Sonde, 7) != Sonde[7]) // Vérifie que l'adresse a été correctement reçue
    return false;                        // Si le message est corrompu on retourne une erreur
does it return a meaningful temperature?.
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Pages: [1] 2   Go Up
Jump to: