operation impossible entre float et int

Bonjour à tous,

Voici le code qui me pose un problème:

#include <math.h>

// Default height units, in case no valid settings are found: 3.281 for feet, 1.0 for metres. Defaults to feet.
#define HEIGHT_UNITS_DEFAULT 3.281

// -- low voltage alarm
// default LVA threshold if no valid settings are found.
#define LOW_VOLTAGE_THRESHOLD_DEFAULT 4.7
// when the battery alarm goes off, the voltage will need to come up to the threshold plus this voltage
// before the alarm will switch off. This stops the alarm from switching on and off repeatedly when the
// voltage is very close to threshold.
#define BATTERY_MONITOR_HYSTERESIS 0.2
// --- lipo options
// this is the voltage that will be used to distinguish between 2s and 3s packs.
#define LIPO_CELL_DETECT_THRESHOLD 8.6
/*
4.20v = 100%
4.03v = 76%
3.86v = 52%
3.83v = 42%
3.79v = 30%
3.70v = 11%
3.6?v = 0%
==>2S = 8.4v
==>3S = 12.6v
*/

// this is the voltage that will be used to distinguish between 4xNIxx and 5xNIxx packs.
#define NIXX_CELL_DETECT_THRESHOLD 6
/*
1.5v = 100%
0.9v = 0%
==>4 = 7.0v
==>5 = 7.5v
*/

enum BatteryType { BATTERY_TYPE_NIMH = 0, BATTERY_TYPE_LIPO = 1, BATTERY_TYPE_NONE = 2 };

BatteryType batteryType;
  
int battery_numberOfCells()
{
  int numberOfCells;
  float v = GetExternalVoltage();
  if (batteryType == BATTERY_TYPE_LIPO)
  {
    // measure the LIPO voltage and return the number of cells
    numberOfCells = ((v < LIPO_CELL_DETECT_THRESHOLD) ? 2 : 3);
  }
  else if (batteryType == BATTERY_TYPE_NIMH)
  {
    // measure the NIXX voltage and return the number of cells   
    numberOfCells = ((v < NIXX_CELL_DETECT_THRESHOLD) ? 4 : 5);
  }
  return numberOfCells;
}

// There is a small amount of hysteresis in this function to stop the alarm from
// intermittently switching on and off near the voltage threshold.
boolean battery_isLow()
{
  if (batteryType == BATTERY_TYPE_NONE) return false;
  // TODO: would be better to abstract the hysteresis calculation here
  boolean low, isLow = false;
  double v = GetExternalVoltage();
  float threshold;
  
  if (batteryType == BATTERY_TYPE_LIPO)
  {
    if (isLow) low = ((v / battery_numberOfCells*1.0) < (threshold + BATTERY_MONITOR_HYSTERESIS));
    else low = ((v / battery_numberOfCells) < threshold);
  }
  if (batteryType == BATTERY_TYPE_NIMH)
  {
    if (isLow) low = (v < (threshold + BATTERY_MONITOR_HYSTERESIS));
    else low = (v < threshold);
  }
  isLow = low;
  return low;
}

void battery_Informations()
{
  Serial << F("Batterie type: ");
  switch (batteryType)
  {
    case BATTERY_TYPE_NONE:
      Serial << F("no battery : ");
      break;
    case BATTERY_TYPE_NIMH:
      Serial << F("NIMH : ");
      break;
    case BATTERY_TYPE_LIPO:
      Serial << F("LIPO : ");
      break;
  }
  Serial << _FLOAT(GetExternalVoltage(),3) << F("v (")<< battery_numberOfCells() << F(" elements)") << endl;
}

float GetExternalVoltage()
{
  /*
  http://skyduino.wordpress.com/2012/08/09/arduino-mesurer-la-tension-dun-batterie/
  Celui ci a été calculé pour permettre la mesure d’une plage de tension variant entre 0v et 20v.
  La formule classique d’un pont diviseur de tension est la suivante (je passe la partie théorie, loi des mailles, etc) :
  Vs = Vin * (R2 / (R1 + R2))
  Ici R1 = 3300 ohms (3k3) et R2 = 1100 ohms (1k1), on obtient donc pour Vin = 20v (maximum admissible) :
  Vs = 20 * (1100 / (3300 + 1100)) = 20 * (1100 / 4400) = 20 * 0.25 = 5
  Vs = 5v pour Vin = 20v, soit le maximum que peut mesurer le convertisseur analogique -> numérique de l’arduino (alimenté en 5v bien sûr).
  */
  /*
  Le code couleur des résistances :
  3k3 = Orange Orange Rouge Or
  1k1 = Marron Marron Rouge Or
  */
  static float coeff_division = 4.0;
  /* Mesure de la tension brute */
  unsigned int raw_bat = analogRead(A5); //attention a I2C qui utilise les pins A4 et A5  
  /* Calcul de la tension réel */
  float real_bat = ((raw_bat * (5.0 / 1024)) * coeff_division);
//  /* Affichage */
//  Serial.println(real_bat);
//  delay(1000);
  return (real_bat);
}

La fonction 'battery_IsLow' me retourne l'erreur suivante:
error: invalid operands of types 'double' and 'int()' to binary 'operator/'

Je ne trouve pas de solution .

Pour info, le code permet de suivre le voltage d'un pack d'accus NIMH ou LIPO ...

Merci par avance de votre aide !

bonjour,
je dirais a vue de nez que tu demande

  double v = GetExternalVoltage();

pour

float GetExternalVoltage()

donc ca coince
jette un oeil ici
http://forum.arduino.cc/index.php/topic,44216.0.html

je viens de tester, car tout n'était pas mis dans l'erreur
voilà la ligne qui pose pb

    if (isLow) low = ((v / battery_numberOfCells*1.0) < (threshold + BATTERY_MONITOR_HYSTERESIS));

sketch_oct10a.ino: In function ‘boolean battery_isLow()’:
sketch_oct10a:70: error: invalid operands of types ‘float’ and ‘int ()()’ to binary ‘operator/’
sketch_oct10a:71: error: invalid operands of types ‘float’ and ‘int ()()’ to binary ‘operator/’
sketch_oct10a.ino: In function ‘void battery_Informations()’:
sketch_oct10a:84: error: no match for ‘operator<<’ in ‘Serial << (const __FlashStringHelper*)({...})’
sketch_oct10a:88: error: no match for ‘operator<<’ in ‘Serial << (const __FlashStringHelper*)({...})’
sketch_oct10a:91: error: no match for ‘operator<<’ in ‘Serial << (const __FlashStringHelper*)({...})’
sketch_oct10a:94: error: no match for ‘operator<<’ in ‘Serial << (const __FlashStringHelper*)({...})’
sketch_oct10a:97: error: ‘_FLOAT’ was not declared in this scope
sketch_oct10a:97: error: ‘endl’ was not declared in this scope

Merci à tout le monde,

Je viens effectivement de modifier les deux lignes ainsi et cela ce compile très bien avec l'IDE 1.5.7 !!!

if (isLow) low = ((v / (battery_numberOfCells())) < (threshold + BATTERY_MONITOR_HYSTERESIS));
else low = ((v / battery_numberOfCells()) < threshold);

Merci encore de votre aide :slight_smile: