EasyVr + DS18B20 Temp sensor

Hi all

I’m currently doing a small project and I will like to read the DS18B20 dat only once.
I found a program that read it but it make my easy vr program fail.
Does anyone know how can I ask him to read only one time the data. Thk

Here is my code.
(code tags added by Moderator)

#if defined(ARDUINO) && ARDUINO >= 100
  #include "Arduino.h"
  #include "SoftwareSerial.h"
  SoftwareSerial port(12,13);
   
#else // Arduino 0022 - use modified NewSoftSerial
  #include "WProgram.h"
  #include "NewSoftSerial.h"
  NewSoftSerial port(12,13);
  NewSoftSerial port(3,4);
  
  
#endif

int TEMP_Pin = 2;
#include <OneWire.h> 

OneWire ds(TEMP_Pin);

#include "EasyVR.h"
EasyVR easyvr(port);

//Groups and Commands
enum Groups
{
  GROUP_0  = 0,
  GROUP_1  = 1,
};

enum Group0 
{
  G0_VAL = 0,
  G0_UNNAMED_COMMAND_1 = 1,
};

enum Group1 
{
  G1_ON = 0,
  G1_OFF = 1,
};


EasyVRBridge bridge;

int8_t group, idx;

void setup()
{
  // bridge mode?
  if (bridge.check())
  {
    cli();
    bridge.loop(0, 1, 12, 13);
  }
  // run normally
  Serial.begin(9600);
  port.begin(9600);

  if (!easyvr.detect())
  {
    Serial.println("EasyVR not detected!");
    for (;;);
  }

  easyvr.setPinOutput(EasyVR::IO1, LOW);
  Serial.println("EasyVR detected!");
  easyvr.setTimeout(5);
  easyvr.setLanguage(5);

  group = EasyVR::TRIGGER; //<-- start group (customize)
}

void action();

float getTemp(){
 //returns the temperature from one DS18S20 in DEG Celsius

 byte data[12];
 byte addr[8];

 if ( !ds.search(addr)) {
   //no more sensors on chain, reset search
   ds.reset_search();
   return -1000;
 }

 if ( OneWire::crc8( addr, 7) != addr[7]) {
   Serial.println("CRC is not valid!");
   return -1000;
 }

 if ( addr[0] != 0x10 && addr[0] != 0x28) {
   Serial.print("Device is not recognized");
   return -1000;
 }

 ds.reset();
 ds.select(addr);
 ds.write(0x44,1); // start conversion, with parasite power on at the end

 byte present = ds.reset();
 ds.select(addr);  
 ds.write(0xBE); // Read Scratchpad

 
 for (int i = 0; i < 9; i++) { // we need 9 bytes
  data[i] = ds.read();
 }
 
 ds.reset_search();
 
 byte MSB = data[1];
 byte LSB = data[0];

 float tempRead = ((MSB << 8) | LSB); //using two's compliment
 float TemperatureSum = tempRead / 16;
 
 return TemperatureSum;
 
}

void loop()
{
  easyvr.setPinOutput(EasyVR::IO1, HIGH); // LED on (listening)
  Serial.print("Say a command in Group ");
  Serial.println(group);
  easyvr.recognizeCommand(group);
 
  do
  {
    // can do some processing while waiting for a spoken command
  }
  while (!easyvr.hasFinished());
  
  easyvr.setPinOutput(EasyVR::IO1, LOW); // LED off

  idx = easyvr.getWord();
  if (idx >= 0)
  {
    // built-in trigger (ROBOT)
    // group = GROUP_X; <-- jump to another group X
    return;
  }
  idx = easyvr.getCommand();
  if (idx >= 0)
  {
    // print debug message
    uint8_t train = 0;
    char name[32];
    Serial.print("Command: ");
    Serial.print(idx);
    if (easyvr.dumpCommand(group, idx, name, train))
    {
      Serial.print(" = ");
      Serial.println(name);
    }
    else
      Serial.println();
    easyvr.playSound(0, EasyVR::VOL_FULL);
    // perform some action
    action();
  }
  else // errors or timeout
  {
    if (easyvr.isTimeout())
      Serial.println("Timed out, try again...");
    int16_t err = easyvr.getError();
    if (err >= 0)
    {
      Serial.print("Error ");
      Serial.println(err, HEX);
    }
  }
}

void action()
{
    int temperature = getTemp();
    switch (group)
    {
    case GROUP_0:
      switch (idx)
      {
      case G0_VAL:
        // write your action code here
        group = GROUP_1;
        // group = GROUP_X; <-- or jump to another group X for composite commands
        break;
      case G0_UNNAMED_COMMAND_1:
        // write your action code here
        group = GROUP_1;
        // group = GROUP_X; <-- or jump to another group X for composite commands
        break;
      }
      break;
    case GROUP_1:
      switch (idx)
      {
      case G1_ON:
        // write your action code here
         group = GROUP_0;
        // group = GROUP_X; <-- or jump to another group X for composite commands
         digitalWrite(11, HIGH);   // set the LED on
         Serial.println(" La led est allume. ");
         Serial.println(temperature);
         break;
        
      case G1_OFF:
        // write your action code here
         group = GROUP_0;
        // group = GROUP_X; <-- or jump to another group X for composite commands
        digitalWrite(11, LOW);   
        break;
      }
      break;
    }
}

Look at this: Reading DS18B20 temperatures. Program requires OneWire.h and DallasTemperature.h (download latest version)

I saw it but the problem is not the precision but the loop. I dont want the Arduino to check in loop the temperature. I want him to check it only once when I ask him in the action. But when I ask him in the action to go in another fonction, the arduino crash.

I think, you can not use return; inside loop()! If you want to interrupt while(), use break; instead of return;

MathB: Does anyone know how can I ask him to read only one time the data. Thk

Put the call to read the temperature in code that only executes when you want to read the temperature.

If you only want to read it once during setup(), put the call in setup(). If you want to read it when some other event occurs, locate (or add, as necessary) code which detects when the event has occurred and use that to call the code to read it.

K5CZ: I think, you can not use return; inside loop()!

You can return from any function - whether it is a logically sensible thing to do depends on what you are trying to achieve, but you can certainly use return inside loop().