Problem to use float to char convertion.

Status
Not open for further replies.

Cleber

Member
Problem to convert float to char.

Hi all.
First, sorry for bad English. I use google translate.
I have a strange problem here.
I'm start to migrate my arduino Variometer to teensy 3.2 because Arduino is too low.
When I upload the sketch to the teensy using teensyduino it re-starts infinitely.
I found that this problem is caused when the sketch convert float vario to char vario_arr[6] using dtostrf .
The vario variable returns below.

-0.24
-0.24
-0.28
-0.29
-0.12
-0.04
-0.07
0.00
-0.02
0.07
0.05
0.14
0.15
0.13
0.16
0.02
0.12
0.27
0.31
0.29
0.45
0.54
0.57
0.52
0.55
0.49
0.49
0.46
0.37
0.39
0.43
0.38
0.34
0.23
0.12
0.06
0.03
0.06
0.05
0.11
0.06
0.11
-0.00
-0.08
-0.06
0.08
0.01
-0.05
-0.02
0.01
-0.02
-0.02
-0.02
-0.06
-0.12
-0.13
0.04
-0.03
0.02
-0.05
-0.00

Jump to the line //////////// DTOSTRF TEST //////////// in the code to see the test.

Code:
/*
Arduino Vario by Jaros, 2012 (dedicated to atmega328 based arduinos)
Part of the "GoFly" project
https://sites.google.com/site/jarosrwebsite/para-nav
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

Arduino board creates NMEA like protocol with variometer output and beping sound.
LK8000 EXTERNAL INSTRUMENT SERIES 1 - NMEA SENTENCE: LK8EX1
VERSION A, 110217

$LK8EX1,pressure,altitude,vario,temperature,battery,*checksum

Field 0, raw pressure in hPascal:hPA*100 (example for 1013.25 becomes 101325)
no padding (987.25 becomes 98725, NOT 098725)
If no pressure available, send 999999 (6 times 9)
If pressure is available, field 1 altitude will be ignored
Field 1, altitude in meters, relative to QNH 1013.25
If raw pressure is available, this value will be IGNORED (you can set it to 99999
but not really needed)!(if you want to use this value, set raw pressure to 999999)
This value is relative to sea level (QNE). We are assuming that currently at 0m
altitude pressure is standard 1013.25.If you cannot send raw altitude, then send
what you have but then you must NOT adjust it from Basic Setting in LK.
Altitude can be negative. If altitude not available, and Pressure not available, set Altitude
to 99999. LK will say "Baro altitude available" if one of fields 0 and 1 is available.
Field 2, vario in cm/s
If vario not available, send 9999. Value can also be negative.
Field 3, temperature in C , can be also negative.If not available, send 99
Field 4, battery voltage or charge percentage.Cannot be negative.If not available, send 999.
Voltage is sent as float value like: 0.1 1.4 2.3 11.2. To send percentage, add 1000.
Example 0% = 1000. 14% = 1014 . Do not send float values for percentages.
Percentage should be 0 to 100, with no decimals, added by 1000!
Credits:
(1) http://code.google.com/p/bmp085driver/                             //bmp085 library
(2) http://mbed.org/users/tkreyche/notebook/bmp085-pressure-sensor/    //more about bmp085 and average filter
(3) http://code.google.com/p/rogue-code/                               //helpfull tone library to make nice beeping without using delay
(4) http://www.daqq.eu/index.php?show=prj_sanity_nullifier             //how to make loud piezo speaker
(5) http://lk8000.it                                                   //everything because of that
(6) http://taturno.com/2011/10/30/variometro-la-rivincita/             //huge thanks for Vario algorithm
(7) http://code.google.com/p/tinkerit/wiki/SecretVoltmeter             //how to measure battery level using AVR ucontroller
*/

#include <i2c_t3.h>                      //i2c library
#include <MS5611.h>                    //bmp085 library, download from url link (1)
//#include <Tone.h>                      //tone library, download from url link (3)
//#include<avr_functions.h>                     //we need that to use dtostrf() and convert float to string
MS5611 ms5611;
/////////////////////////////////////////
///////////////////////////////////////// variables that You can test and try
short speaker_pin1 = 8;                //arduino speaker output -
short speaker_pin2 = 9;                //arduino speaker output +
float vario_climb_rate_start = 0.4;    //minimum climb beeping value(ex. start climbing beeping at 0.4m/s)
float vario_sink_rate_start = -0.4;    //maximum sink beeping value (ex. start sink beep at -1.1m/s)
#define SAMPLES_ARR 30                  //define moving average filter array size (2->30), more means vario is less sensitive and slower
#define UART_SPEED 9600                //define serial transmision speed (9600,19200, etc...)
/////////////////////////////////////////
///////////////////////////////////////////
//BMP085   bmp085 = BMP085();            //set up bmp085 sensor
//Tone     tone_out1;
//Tone     tone_out2;
long     Temperature = 0;
long     Pressure = 101325;
float    Altitude;
int      Battery_Vcc = 0;             //variable to hold the value of Vcc from battery
const float p0 = 101325;              //Pressure at sea level (Pa)
unsigned long get_time1 = millis();
unsigned long get_time2 = millis();
unsigned long get_time3 = millis();
boolean  thermalling = false;
int      my_temperature = 1;
char     altitude_arr[6];            //wee need this array to translate float to string
char     vario_arr[6];               //wee need this array to translate float to string
int      samples=40;
int      maxsamples=50;
float    alt[51];
float    tim[51];
float    beep;
float    Beep_period;
static long k[SAMPLES_ARR];

static long Averaging_Filter(long input);
static long Averaging_Filter(long input) // moving average filter function
{
  long sum = 0;
  for (int i = 0; i < SAMPLES_ARR; i++) {
    k[i] = k[i+1];
  }
  k[SAMPLES_ARR - 1] = input;
  for (int i = 0; i < SAMPLES_ARR; i++) {
    sum += k[i];
  }
  return ( sum / SAMPLES_ARR ) ;
}

void play_welcome_beep()                 //play only once welcome beep after turning on arduino vario
{
  for (int aa=300;aa<=1500;aa=aa+100)
  {
    tone(8,aa,200);             // play beep on pin 8 (note,duration)
 //   tone_out2.play(aa+3,200);           // play beep on pin 9 (note,duration), it is louder if we move aplitude phase
    delay(100);
  }

}



void setup()                // setup() function to setup all necessary parameters before we go to endless loop() function
{
  Serial.begin(UART_SPEED);       // set up arduino serial port
//  Wire.begin();             // lets init i2c protocol
Wire.begin(I2C_MASTER, 0x00, I2C_PINS_18_19, I2C_PULLUP_EXT, 400000);
ms5611.begin();
//  tone_out1.begin(speaker_pin1);       // piezo speaker output pin8 -
//  tone_out2.begin(speaker_pin2);       // piezo speaker output pin9 +
 // bmp085.init(MODE_ULTRA_HIGHRES, p0, false);
                            // BMP085 ultra-high-res mode, 101325Pa = 1013.25hPa, false = using Pa units
                            // this initialization is useful for normalizing pressure to specific datum.
                            // OR setting current local hPa information from a weather station/local airport (QNH).
  play_welcome_beep();      //everything is ready, play "welcome" sound
}

void loop(void)
{
  float tempo=millis();
  float vario=0;
  float N1=0;
  float N2=0;
  float N3=0;
  float D1=0;
  float D2=0;
  Pressure = ms5611.readPressure();                                   //get one sample from BMP085 in every loop
  long average_pressure = Averaging_Filter(Pressure);                   //put it in filter and take average
  Altitude = (float)44330 * (1 - pow(((float)Pressure/p0), 0.190295));  //take new altitude in meters
  //Serial.println(Battery_Vcc);
  for(int cc=1;cc<=maxsamples;cc++){                                   //samples averaging and vario algorithm
    alt[(cc-1)]=alt[cc];
    tim[(cc-1)]=tim[cc];
  };
  alt[maxsamples]=Altitude;
  tim[maxsamples]=tempo;
  float stime=tim[maxsamples-samples];
  for(int cc=(maxsamples-samples);cc<maxsamples;cc++){
    N1+=(tim[cc]-stime)*alt[cc];
    N2+=(tim[cc]-stime);
    N3+=(alt[cc]);
    D1+=(tim[cc]-stime)*(tim[cc]-stime);
    D2+=(tim[cc]-stime);
  };

  vario=1000*((samples*N1)-N2*N3)/(samples*D1-D2*D2);
  if ((tempo-beep)>Beep_period)                          // make some beep
  {
    beep=tempo;
    if (vario>vario_climb_rate_start && vario<15 )
    {
      Beep_period=350-(vario*5);
      tone(8,(1000+(100*vario)),300-(vario*5)); //when climbing make faster and shorter beeps
 //     tone_out2.play((1003+(100*vario)),300-(vario*5));
      thermalling = true;                               //ok,we have thermall in our hands
    } else if ((vario < 0 ) && (thermalling == true))   //looks like we jump out the thermall
    {
      //Beep_period=200;
     // play_siren();                                   //oo, we lost thermall play alarm
      thermalling = false;
    } else if (vario< vario_sink_rate_start){           //if you have high performace glider you can change sink beep to -0.95m/s ;)
      Beep_period=200;
     tone(8,300,340);
      thermalling = false;
    }
  }

  if (millis() >= (get_time2+1000))      //every second get temperature and battery level
  {
 //  Temperature =  bmp085.getTemperature(&Temperature); // get temperature in celsius from time to time, we have to divide that by 10 to get XY.Z
    my_temperature = ms5611.readTemperature();
    Battery_Vcc = 999;//(readVcc()/42)+1000;    // get voltage and prepare in percentage
    get_time2 = millis();
  }

//////////// DTOSTRF TEST ////////////

dtostrf(vario,0,0,vario_arr);
Serial.println(vario_arr);

//////////////////////////////////////

/*
if (millis() >= (get_time3+333))       //every 1/3 second send NMEA output over serial port
  {
    String str_out =                                                                   //combine all values and create part of NMEA data string output
      String("LK8EX1"+String(",")+String(Pressure,DEC)+ String(",")+String(dtostrf(Altitude,0,0,altitude_arr))+String(",")+
     String(dtostrf(vario,0,0,vario_arr))+String(",")+String(my_temperature,DEC)+String(",")+String(Battery_Vcc,DEC)+String(","));
    unsigned int checksum_end,ai,bi;                                                 // Calculating checksum for data string
    for (checksum_end = 0, ai = 0; ai < str_out.length(); ai++)
    {
      bi = (unsigned char)str_out[ai];
      checksum_end ^= bi;
    }
    //creating now NMEA serial output for LK8000. LK8EX1 protocol format:
    //$LK8EX1,pressure,altitude,vario,temperature,battery,*checksum
    Serial.print("$");                     //print first sign of NMEA protocol
    Serial.print(str_out);                 // print data string
    Serial.print("*");                     //end of protocol string
    Serial.println(checksum_end,HEX);      //print calculated checksum on the end of the string in HEX
    get_time3 = millis();
}
*/

}


The strange thing is that this sketch works fine

Code:
char vario_arr[6];
float vario = 0.22;


void setup() {

Serial.begin(9600);
}

void loop() {

dtostrf(vario,0,0,vario_arr);
String str_out = vario_arr;
Serial.println(str_out);

}

This is te verbose from teensyduino.

Code:
19:18:01: HID/win32:  vid:045E pid:006D ver:0110
19:18:01: HID/win32:  vid:045E pid:006D ver:0110
19:18:02: redraw, image 9
19:18:04: Device came online, code_size = 262144
19:18:04: Board is: Teensy 3.2 (MK20DX256), version 1.03
19:18:04: File "scanner.ino.hex". 36664 bytes, 14% used
19:18:04: set background IMG_ONLINE
19:18:04: File "scanner.ino.hex". 36664 bytes, 14% used
19:18:04: elf size appears to be 262144
19:18:04: elf binary data matches hex file
19:18:04: Code size from .elf file = 262144
19:18:04: begin operation
19:18:04: flash, block=0, bs=1024, auto=1
19:18:04: flash, block=1, bs=1024, auto=1
19:18:04: flash, block=2, bs=1024, auto=1
19:18:04: flash, block=3, bs=1024, auto=1
19:18:04: HID/win32: waiting for device
19:18:04: HID/win32: waiting for device
19:18:04: flash, block=4, bs=1024, auto=1
19:18:04: flash, block=5, bs=1024, auto=1
19:18:04: flash, block=6, bs=1024, auto=1
19:18:04: flash, block=7, bs=1024, auto=1
19:18:04: flash, block=8, bs=1024, auto=1
19:18:04: flash, block=9, bs=1024, auto=1
19:18:04: flash, block=10, bs=1024, auto=1
19:18:04: flash, block=11, bs=1024, auto=1
19:18:05: flash, block=12, bs=1024, auto=1
19:18:05: flash, block=13, bs=1024, auto=1
19:18:05: flash, block=14, bs=1024, auto=1
19:18:05: flash, block=15, bs=1024, auto=1
19:18:05: flash, block=16, bs=1024, auto=1
19:18:05: flash, block=17, bs=1024, auto=1
19:18:05: flash, block=18, bs=1024, auto=1
19:18:05: flash, block=19, bs=1024, auto=1
19:18:05: flash, block=20, bs=1024, auto=1
19:18:05: flash, block=21, bs=1024, auto=1
19:18:05: flash, block=22, bs=1024, auto=1
19:18:05: flash, block=23, bs=1024, auto=1
19:18:05: flash, block=24, bs=1024, auto=1
19:18:05: flash, block=25, bs=1024, auto=1
19:18:05: flash, block=26, bs=1024, auto=1
19:18:05: flash, block=27, bs=1024, auto=1
19:18:05: flash, block=28, bs=1024, auto=1
19:18:05: flash, block=29, bs=1024, auto=1
19:18:05: flash, block=30, bs=1024, auto=1
19:18:05: flash, block=31, bs=1024, auto=1
19:18:05: flash, block=32, bs=1024, auto=1
19:18:05: flash, block=33, bs=1024, auto=1
19:18:05: flash, block=34, bs=1024, auto=1
19:18:05: flash, block=35, bs=1024, auto=1
19:18:05: sending reboot
19:18:05: begin wait_until_offline
19:18:06: offline, waited 9
19:18:06: end operation
19:18:06: set background IMG_REBOOT_OK
19:18:06: redraw timer set, image 14 to show for 1200 ms
19:18:06: HID/win32:  vid:045E pid:006D ver:0110
19:18:06: HID/win32:  vid:045E pid:006D ver:0110
19:18:06: HID/win32:  vid:045E pid:006D ver:0110
19:18:06: HID/win32:  vid:045E pid:006D ver:0110
19:18:07: redraw, image 9
19:18:08: Device came online, code_size = 262144
19:18:08: Board is: Teensy 3.2 (MK20DX256), version 1.03
19:18:08: File "scanner.ino.hex". 36664 bytes, 14% used
19:18:08: set background IMG_ONLINE
19:18:08: File "scanner.ino.hex". 36664 bytes, 14% used
19:18:08: elf size appears to be 262144
19:18:08: elf binary data matches hex file
19:18:08: Code size from .elf file = 262144
19:18:08: begin operation
19:18:08: flash, block=0, bs=1024, auto=1
19:18:08: flash, block=1, bs=1024, auto=1
19:18:08: flash, block=2, bs=1024, auto=1
19:18:08: flash, block=3, bs=1024, auto=1
19:18:08: HID/win32: waiting for device
19:18:08: HID/win32: waiting for device
19:18:08: flash, block=4, bs=1024, auto=1
19:18:08: flash, block=5, bs=1024, auto=1
19:18:08: flash, block=6, bs=1024, auto=1
19:18:08: flash, block=7, bs=1024, auto=1
19:18:08: flash, block=8, bs=1024, auto=1
19:18:08: flash, block=9, bs=1024, auto=1
19:18:08: flash, block=10, bs=1024, auto=1
19:18:08: flash, block=11, bs=1024, auto=1
19:18:08: flash, block=12, bs=1024, auto=1
19:18:08: flash, block=13, bs=1024, auto=1
19:18:08: flash, block=14, bs=1024, auto=1
19:18:08: flash, block=15, bs=1024, auto=1
19:18:09: flash, block=16, bs=1024, auto=1
19:18:09: flash, block=17, bs=1024, auto=1
19:18:09: flash, block=18, bs=1024, auto=1
19:18:09: flash, block=19, bs=1024, auto=1
19:18:09: flash, block=20, bs=1024, auto=1
19:18:09: flash, block=21, bs=1024, auto=1
19:18:09: flash, block=22, bs=1024, auto=1
19:18:09: flash, block=23, bs=1024, auto=1
19:18:09: flash, block=24, bs=1024, auto=1
19:18:09: flash, block=25, bs=1024, auto=1
19:18:09: flash, block=26, bs=1024, auto=1
19:18:09: flash, block=27, bs=1024, auto=1
19:18:09: flash, block=28, bs=1024, auto=1
19:18:09: flash, block=29, bs=1024, auto=1
19:18:09: flash, block=30, bs=1024, auto=1
19:18:09: flash, block=31, bs=1024, auto=1
19:18:09: flash, block=32, bs=1024, auto=1
19:18:09: flash, block=33, bs=1024, auto=1
19:18:09: flash, block=34, bs=1024, auto=1
19:18:09: flash, block=35, bs=1024, auto=1
19:18:09: sending reboot
19:18:09: begin wait_until_offline
19:18:10: offline, waited 11
19:18:10: end operation
19:18:10: set background IMG_REBOOT_OK
19:18:10: redraw timer set, image 14 to show for 1200 ms
19:18:10: HID/win32:  vid:045E pid:006D ver:0110
19:18:10: HID/win32:  vid:045E pid:006D ver:0110
19:18:10: HID/win32:  vid:045E pid:006D ver:0110
19:18:10: HID/win32:  vid:045E pid:006D ver:0110
19:18:11: redraw, image 9
19:18:14: Device came online, code_size = 262144
19:18:14: Board is: Teensy 3.2 (MK20DX256), version 1.03
19:18:14: File "scanner.ino.hex". 36664 bytes, 14% used
19:18:14: set background IMG_ONLINE
19:18:14: File "scanner.ino.hex". 36664 bytes, 14% used
19:18:14: elf size appears to be 262144
19:18:14: elf binary data matches hex file
19:18:14: Code size from .elf file = 262144
19:18:14: begin operation
19:18:14: flash, block=0, bs=1024, auto=1
19:18:14: flash, block=1, bs=1024, auto=1
19:18:14: flash, block=2, bs=1024, auto=1
19:18:14: flash, block=3, bs=1024, auto=1
19:18:14: HID/win32: waiting for device
19:18:14: HID/win32: waiting for device
19:18:14: HID/win32: waiting for device
19:18:14: flash, block=4, bs=1024, auto=1
19:18:14: flash, block=5, bs=1024, auto=1
19:18:14: flash, block=6, bs=1024, auto=1
19:18:14: flash, block=7, bs=1024, auto=1
19:18:14: flash, block=8, bs=1024, auto=1
19:18:14: flash, block=9, bs=1024, auto=1
19:18:14: flash, block=10, bs=1024, auto=1
19:18:14: flash, block=11, bs=1024, auto=1
19:18:14: flash, block=12, bs=1024, auto=1
19:18:14: flash, block=13, bs=1024, auto=1
19:18:14: flash, block=14, bs=1024, auto=1
19:18:14: flash, block=15, bs=1024, auto=1
19:18:14: flash, block=16, bs=1024, auto=1
19:18:14: flash, block=17, bs=1024, auto=1
19:18:14: flash, block=18, bs=1024, auto=1
19:18:14: flash, block=19, bs=1024, auto=1
19:18:14: flash, block=20, bs=1024, auto=1
19:18:14: flash, block=21, bs=1024, auto=1
19:18:14: flash, block=22, bs=1024, auto=1
19:18:14: flash, block=23, bs=1024, auto=1
19:18:14: flash, block=24, bs=1024, auto=1
19:18:14: flash, block=25, bs=1024, auto=1
19:18:14: flash, block=26, bs=1024, auto=1
19:18:14: flash, block=27, bs=1024, auto=1
19:18:14: flash, block=28, bs=1024, auto=1
19:18:14: flash, block=29, bs=1024, auto=1
19:18:14: flash, block=30, bs=1024, auto=1
19:18:14: flash, block=31, bs=1024, auto=1
19:18:14: flash, block=32, bs=1024, auto=1
19:18:14: flash, block=33, bs=1024, auto=1
19:18:14: flash, block=34, bs=1024, auto=1
19:18:14: flash, block=35, bs=1024, auto=1
19:18:14: sending reboot
19:18:14: begin wait_until_offline
19:18:15: offline, waited 4
19:18:15: end operation
19:18:15: set background IMG_REBOOT_OK
19:18:15: redraw timer set, image 14 to show for 1200 ms
19:18:15: HID/win32:  vid:045E pid:006D ver:0110
19:18:15: HID/win32:  vid:045E pid:006D ver:0110
19:18:15: HID/win32:  vid:045E pid:006D ver:0110
19:18:15: HID/win32:  vid:045E pid:006D ver:0110
19:18:16: redraw, image 9
19:18:18: Device came online, code_size = 262144
19:18:18: Board is: Teensy 3.2 (MK20DX256), version 1.03
19:18:18: File "scanner.ino.hex". 36664 bytes, 14% used
19:18:18: set background IMG_ONLINE
19:18:18: File "scanner.ino.hex". 36664 bytes, 14% used
19:18:18: elf size appears to be 262144
19:18:18: elf binary data matches hex file
19:18:18: Code size from .elf file = 262144
19:18:18: begin operation
19:18:18: flash, block=0, bs=1024, auto=1
19:18:18: flash, block=1, bs=1024, auto=1
19:18:18: flash, block=2, bs=1024, auto=1
19:18:18: flash, block=3, bs=1024, auto=1
19:18:18: HID/win32: waiting for device
19:18:18: HID/win32: waiting for device
19:18:18: HID/win32: waiting for device
19:18:18: flash, block=4, bs=1024, auto=1
19:18:18: flash, block=5, bs=1024, auto=1
19:18:18: flash, block=6, bs=1024, auto=1
19:18:18: flash, block=7, bs=1024, auto=1
19:18:18: flash, block=8, bs=1024, auto=1
19:18:19: flash, block=9, bs=1024, auto=1
19:18:19: flash, block=10, bs=1024, auto=1
19:18:19: flash, block=11, bs=1024, auto=1
19:18:19: flash, block=12, bs=1024, auto=1
19:18:19: flash, block=13, bs=1024, auto=1
19:18:19: flash, block=14, bs=1024, auto=1
19:18:19: flash, block=15, bs=1024, auto=1
19:18:19: flash, block=16, bs=1024, auto=1
19:18:19: flash, block=17, bs=1024, auto=1
19:18:19: flash, block=18, bs=1024, auto=1
19:18:19: flash, block=19, bs=1024, auto=1
19:18:19: flash, block=20, bs=1024, auto=1
19:18:19: flash, block=21, bs=1024, auto=1
19:18:19: flash, block=22, bs=1024, auto=1
19:18:19: flash, block=23, bs=1024, auto=1
19:18:19: flash, block=24, bs=1024, auto=1
19:18:19: flash, block=25, bs=1024, auto=1
19:18:19: flash, block=26, bs=1024, auto=1
19:18:19: flash, block=27, bs=1024, auto=1
19:18:19: flash, block=28, bs=1024, auto=1
19:18:19: flash, block=29, bs=1024, auto=1
19:18:19: flash, block=30, bs=1024, auto=1
19:18:19: flash, block=31, bs=1024, auto=1
19:18:19: flash, block=32, bs=1024, auto=1
19:18:19: flash, block=33, bs=1024, auto=1
19:18:19: flash, block=34, bs=1024, auto=1
19:18:19: flash, block=35, bs=1024, auto=1
19:18:19: sending reboot
19:18:19: begin wait_until_offline
19:18:20: offline, waited 8
19:18:20: end operation
19:18:20: set background IMG_REBOOT_OK
19:18:20: redraw timer set, image 14 to show for 1200 ms
19:18:20: HID/win32:  vid:045E pid:006D ver:0110
19:18:20: HID/win32:  vid:045E pid:006D ver:0110
19:18:20: HID/win32:  vid:045E pid:006D ver:0110
19:18:20: HID/win32:  vid:045E pid:006D ver:0110
19:18:21: redraw, image 9
19:18:22: Device came online, code_size = 262144
19:18:22: Board is: Teensy 3.2 (MK20DX256), version 1.03
19:18:22: File "scanner.ino.hex". 36664 bytes, 14% used
19:18:22: set background IMG_ONLINE
19:18:22: File "scanner.ino.hex". 36664 bytes, 14% used
19:18:22: elf size appears to be 262144
19:18:22: elf binary data matches hex file
19:18:22: Code size from .elf file = 262144
19:18:22: begin operation
19:18:22: flash, block=0, bs=1024, auto=1
19:18:22: flash, block=1, bs=1024, auto=1
19:18:22: flash, block=2, bs=1024, auto=1
19:18:22: flash, block=3, bs=1024, auto=1
19:18:22: HID/win32: waiting for device
19:18:22: HID/win32: waiting for device
19:18:22: flash, block=4, bs=1024, auto=1
19:18:22: flash, block=5, bs=1024, auto=1
19:18:22: flash, block=6, bs=1024, auto=1
19:18:22: flash, block=7, bs=1024, auto=1
19:18:22: flash, block=8, bs=1024, auto=1
19:18:22: flash, block=9, bs=1024, auto=1
19:18:22: flash, block=10, bs=1024, auto=1
19:18:22: flash, block=11, bs=1024, auto=1
19:18:22: flash, block=12, bs=1024, auto=1
19:18:22: flash, block=13, bs=1024, auto=1
19:18:22: flash, block=14, bs=1024, auto=1
19:18:23: flash, block=15, bs=1024, auto=1
19:18:23: flash, block=16, bs=1024, auto=1
19:18:23: flash, block=17, bs=1024, auto=1
19:18:23: flash, block=18, bs=1024, auto=1
19:18:23: flash, block=19, bs=1024, auto=1
19:18:23: flash, block=20, bs=1024, auto=1
19:18:23: flash, block=21, bs=1024, auto=1
19:18:23: flash, block=22, bs=1024, auto=1
19:18:23: flash, block=23, bs=1024, auto=1
19:18:23: flash, block=24, bs=1024, auto=1
19:18:23: flash, block=25, bs=1024, auto=1
19:18:23: flash, block=26, bs=1024, auto=1
19:18:23: flash, block=27, bs=1024, auto=1
19:18:23: flash, block=28, bs=1024, auto=1
19:18:23: flash, block=29, bs=1024, auto=1
19:18:23: flash, block=30, bs=1024, auto=1
19:18:23: flash, block=31, bs=1024, auto=1
19:18:23: flash, block=32, bs=1024, auto=1
19:18:23: flash, block=33, bs=1024, auto=1
19:18:23: flash, block=34, bs=1024, auto=1
19:18:23: flash, block=35, bs=1024, auto=1
19:18:23: sending reboot

I use arduino 1.6.9 and teensyduino 1.29

Any one know whats happening here ?

Thanks.
 
Last edited:
Try it making both of these larger - like 16 instead of 6 just to see if it is trashing the data area. AFAIK - The only time I got Teensy to do a boot/reboot was writing beyond the end of an array's allocated area. There may be something else in the code moving from 8 bit AVR to 32 bit ARM - look for verbose compiler warnings.

Code:
char     altitude_arr[6];            //wee need this array to translate float to string
char     vario_arr[6];               //wee need this array to translate float to string

It could be an anomaly with STRING variables - a fix was recently made - what version of TeensyDuino is being used? That was in TD_1.29 or TD_1.30 - make sure you have TD_1.30. Try doing your testing without String perhaps concatenate into a character array of good size?
Code:
String str_out =                                                                   //combine all values and create part of NMEA data string output
      String("LK8EX1"+String(",")+String(Pressure,DEC)+ String(",")+String(dtostrf(Altitude,0,0,altitude_arr))+String(",")+
     String(dtostrf(vario,0,0,vario_arr))+String(",")+String(my_temperature,DEC)+String(",")+String(Battery_Vcc,DEC)+String(","));
 
Thanks for your help Defragster.
I expand the vario_arr char from 6 to 16 caracter, but the problem persist. After, the version of the teensyduino was updated from version 1.29 to version 1.30 and the problem persist.

About string str_out, it's commented and does not run.

Do you have any other idea ?

Thanks.
 
Odd - can you post a current version of your code - maybe if you can simplify and remove any devices that would prevent it from running for anyone wanting to see it run? Maybe return simulated values for the ms5611 ... etc and unlink that real code.

// take this out // #include <i2c_t3.h> //i2c library
// take this out // #include <MS5611.h> //bmp085 library, download from url link (1)
// take this out // MS5611 ms5611;
// take this out // Wire.begin(I2C_MASTER, 0x00, I2C_PINS_18_19, I2C_PULLUP_EXT, 400000);
// take this out // ms5611.begin();
my_temperature = ms5611.readTemperature() // create a ms5611__readTemperature() and return known range of values
Pressure = ms5611.readPressure(); // create a ms5611__readPressure() and return known range of values

If you can do that and still see it fail it will rule out the hardware and make it runnable by anyone
 
This is the clean code.

//#include <i2c_t3.h> //i2c library
//#include <MS5611.h> //bmp085 library, download from url link (1)
//MS5611 ms5611;

short speaker_pin1 = 8; //arduino speaker output -
float vario_climb_rate_start = 0.4; //minimum climb beeping value(ex. start climbing beeping at 0.4m/s)
float vario_sink_rate_start = -0.4; //maximum sink beeping value (ex. start sink beep at -1.1m/s)
#define SAMPLES_ARR 30 //define moving average filter array size (2->30), more means vario is less sensitive and slower
#define UART_SPEED 9600 //define serial transmision speed (9600,19200, etc...)


long Temperature = 0;
long Pressure = 101325;
float Altitude;
int Battery_Vcc = 0; //variable to hold the value of Vcc from battery
const float p0 = 101325; //Pressure at sea level (Pa)
unsigned long get_time1 = millis();
unsigned long get_time2 = millis();
unsigned long get_time3 = millis();
boolean thermalling = false;
int my_temperature = 1;
char altitude_arr[6]; //wee need this array to translate float to string
char vario_arr[16]; //wee need this array to translate float to string
int samples=40;
int maxsamples=50;
float alt[51];
float tim[51];
float beep;
float Beep_period;
static long k[SAMPLES_ARR];

static long Averaging_Filter(long input);
static long Averaging_Filter(long input) // moving average filter function
{
long sum = 0;
for (int i = 0; i < SAMPLES_ARR; i++) {
k = k[i+1];
}
k[SAMPLES_ARR - 1] = input;
for (int i = 0; i < SAMPLES_ARR; i++) {
sum += k;
}
return ( sum / SAMPLES_ARR ) ;
}

void play_welcome_beep() //play only once welcome beep after turning on arduino vario
{
for (int aa=300;aa<=1500;aa=aa+100)
{
tone(8,aa,200); // play beep on pin 8 (note,duration)
// tone_out2.play(aa+3,200); // play beep on pin 9 (note,duration), it is louder if we move aplitude phase
delay(100);
}

}

void setup() // setup() function to setup all necessary parameters before we go to endless loop() function
{
Serial.begin(UART_SPEED); // set up arduino serial port
// Wire.begin(); // lets init i2c protocol
//Wire.begin(I2C_MASTER, 0x00, I2C_PINS_18_19, I2C_PULLUP_EXT, 400000);
//ms5611.begin();

play_welcome_beep(); //everything is ready, play "welcome" sound
}

void loop(void)
{
float tempo=millis();
float vario=0;
float N1=0;
float N2=0;
float N3=0;
float D1=0;
float D2=0;
Pressure = 99650; //ms5611.readPressure(true); //get one sample from BMP085 in every loop
long average_pressure = Averaging_Filter(Pressure); //put it in filter and take average
Altitude = (float)44330 * (1 - pow(((float)Pressure/p0), 0.190295)); //take new altitude in meters
//Serial.println(Battery_Vcc);
for(int cc=1;cc<=maxsamples;cc++){ //samples averaging and vario algorithm
alt[(cc-1)]=alt[cc];
tim[(cc-1)]=tim[cc];
};
alt[maxsamples]=Altitude;
tim[maxsamples]=tempo;
float stime=tim[maxsamples-samples];
for(int cc=(maxsamples-samples);cc<maxsamples;cc++){
N1+=(tim[cc]-stime)*alt[cc];
N2+=(tim[cc]-stime);
N3+=(alt[cc]);
D1+=(tim[cc]-stime)*(tim[cc]-stime);
D2+=(tim[cc]-stime);
};

vario=1000*((samples*N1)-N2*N3)/(samples*D1-D2*D2);
if ((tempo-beep)>Beep_period) // make some beep
{
beep=tempo;
if (vario>vario_climb_rate_start && vario<15 )
{
Beep_period=350-(vario*5);
tone(8,(1000+(100*vario)),300-(vario*5)); //when climbing make faster and shorter beeps
// tone_out2.play((1003+(100*vario)),300-(vario*5));
thermalling = true; //ok,we have thermall in our hands
} else if ((vario < 0 ) && (thermalling == true)) //looks like we jump out the thermall
{
//Beep_period=200;
// play_siren(); //oo, we lost thermall play alarm
thermalling = false;
} else if (vario< vario_sink_rate_start){ //if you have high performace glider you can change sink beep to -0.95m/s ;)
Beep_period=200;
tone(8,300,340);
thermalling = false;
}
}

if (millis() >= (get_time2+1000)) //every second get temperature and battery level
{
// Temperature = bmp085.getTemperature(&Temperature); // get temperature in celsius from time to time, we have to divide that by 10 to get XY.Z
my_temperature = 22; //ms5611.readTemperature(true);
Battery_Vcc = 999;//(readVcc()/42)+1000; // get voltage and prepare in percentage
get_time2 = millis();
}

//////////// DTOSTRF TEST ////////////

dtostrf(vario,0,0,vario_arr);
Serial.println(vario_arr);

//////////////////////////////////////

}
}


I change the value from sensor to a fixed value.
The problem persists

I left a private message.
 
how about just printing out vario with Serial.println(vario);
maybe there is a divide by zero
(i'm away from home and don't have a way to test ...)
 
Last edited:
This is the result from vario

-0.01
-0.00
0.00
0.00
-0.00
0.00
0.00
0.00
-0.00
0.00
0.00
-0.00
-0.00
-0.00
-0.00
-0.01
-0.00
-0.01
-0.00
0.00
0.00
-0.00
0.00
0.00
0.00
-0.00
0.00
0.00
-0.00
-0.00
-0.00
-0.00
-0.01
-0.00
 
The problem is here:
vario = 1000 * ((samples * N1) - N2 * N3) / (samples * D1 - D2 * D2);

This apparently resolves to divide by zero. You need to check your logic / condition the variables.

Making this change with "+1" allows it to run:
vario = 1000 * ((samples * N1) - N2 * N3) / ((samples * D1 - D2 * D2) + 1);
 
Yes, the problem was solved.

$LK8EX1,93661,659,-27,22,999,*3
$LK8EX1,93662,659,-3,22,999,*36
$LK8EX1,93661,659,21,22,999,*28
$LK8EX1,93661,658,-8,22,999,*3F
$LK8EX1,93662,658,-8,22,999,*3C
$LK8EX1,93661,658,9,22,999,*13
$LK8EX1,93661,659,21,22,999,*28
$LK8EX1,93661,659,-12,22,999,*5
$LK8EX1,93661,658,-17,22,999,*1
$LK8EX1,93661,658,33,22,999,*2A
$LK8EX1,93661,658,-11,22,999,*7
$LK8EX1,93661,659,-4,22,999,*32
$LK8EX1,93661,659,-7,22,999,*31
$LK8EX1,93661,658,18,22,999,*23
$LK8EX1,93661,658,2,22,999,*18
$LK8EX1,93661,658,6,22,999,*1C
$LK8EX1,93661,659,-2,22,999,*34
$LK8EX1,93661,658,12,22,999,*29
$LK8EX1,93661,658,43,22,999,*2D
$LK8EX1,93661,658,-26,22,999,*3
$LK8EX1,93661,658,-4,22,999,*33
$LK8EX1,93662,659,-3,22,999,*36
$LK8EX1,93661,658,-21,22,999,*4

Why this vario = 1000 * ((samples * N1) - N2 * N3) / (samples * D1 - D2 * D2); work fine on arduino and teensy not Defragster ?
 
I can only assume it is the math resolution as calculated and stored because of the different processors from the tool chains in use.
 
Indeed a divide by zero is handled - putting that calc from the failed line into a new float var then printing the var also works.

But this raw line after using those same values causes the Teensy to go offline and reset as modified.
Code:
  vario = 1000 * ((samples * N1) - N2 * N3) / (samples * D1 - (D2 * D2));

Code:
  float seeme=0;
  seeme = (samples * D1 - (D2 * D2));

Serial.print("samples >> "); Serial.println(samples);
Serial.print("D1 >> "); Serial.println(D1);
Serial.print("D2 >> "); Serial.println(D2);
Serial.print("D2 * D2 >> "); Serial.println(D2 * D2);
Serial.print("(samples * D1 - (D2 * D2)) >> "); Serial.println(seeme);

samples >> 40
D1 >> 0.00
D2 >> 0.00
D2 * D2 >> 0.00
(samples * D1 - (D2 * D2)) >> 0.00
 
Status
Not open for further replies.
Back
Top