Distance measuring code keeps crashing

Hello i have the following code to read out a distance above ground over a speaker using a Garmin LIDAR. Im using a OEM Arduino UNO to run this code.

The code works fine most of the time but after 10 to 20 mins or so the Arduino appears to freeze and I have to power it off and power back on again. To combate this i added a auto reset after every 10 mins or so but it still seems to freeze sometimes.

Is my code very poor and causing this? im completely self taught and consider myself a beginner



#include <stdint.h>
#include <Wire.h>
#include <LIDARLite_v3HP.h>

LIDARLite_v3HP myLidarLite;




#include <Arduino.h>

#include "Talkie.h"
#include "Vocab_US_Large.h"
#include "Vocab_Special.h"

Talkie voice;


 int staticfinal;
      int distancefinal;
     int  distance;
     int olddistance;
     int  distance1;
     int  distance2;
     int  distance3;
     
void sayNumber(long n);


void setup() {



    Serial.begin(115200);


  Wire.begin();

  //check if LIDAR will acknowledge over I2C
 // if (myLIDAR.begin() == false) {
  //  Serial.println("Device did not acknowledge! Freezing.");
  //  sayNumber(100);
  //  while(1);
//  }
 //Serial.println("LIDAR acknowledged!");
voice.say(sp2_ON);

  //Serial.println(F("Ranging started"));

    // Configure the LidarLite internal parameters so as to lend itself to
    // various modes of operation by altering 'configure' input integer to
    // anything in the range of 0 to 5. See LIDARLite_v3HP.cpp for details.
    myLidarLite.configure(5);
    
}

void(* resetFunc) (void) = 0;


void loop() {
  
 myLidarLite.waitForBusy();
myLidarLite.takeRange();
myLidarLite.waitForBusy();

  //getDistance() returns the distance reading in cm
  distance = myLidarLite.readDistance() / 30.5;

 //wing to ground distanc
distance = distance - 1.2;


//for negative distance
if (distance < 0) {
olddistance = distance;

//Serial.print(distance);
    delay(100);
    return;

}


    if (distance < 6 || distance == 10 || distance == 20 || distance == 50 || distance == 100) {
if (olddistance != distance){
      //Serial.print(distance);
 sayNumber(distance);

  
  }
}
  

  
    //Serial.print(distance);
    olddistance = distance;


    delay(100);


if ( millis()  >= 600000 ) resetFunc(); //call reset
    
}

/* Say any number between -999,999 and 999,999 */
void sayNumber(long n) {
    if (n < 0) {
        voice.say(sp2_MINUS);
        sayNumber(-n);
    } else if (n == 0) {
        voice.say(sp2_ZERO);
    } else {
        if (n >= 1000) {
            int thousands = n / 1000;
            sayNumber(thousands);
            voice.say(sp2_THOUSAND);
            n %= 1000;
            if ((n > 0) && (n < 100))
                voice.say(sp2_AND);
        }
        if (n >= 100) {
            int hundreds = n / 100;
            sayNumber(hundreds);
            voice.say(sp2_HUNDRED);
            n %= 100;
            if (n > 0)
                voice.say(sp2_AND);
        }
        if (n > 19) {
            int tens = n / 10;
            switch (tens) {
            case 2:
                voice.say(sp2_TWENTY);
                break;
            case 3:
                voice.say(sp2_THIR_);
                voice.say(sp2_T);
                break;
            case 4:
                voice.say(sp2_FOUR);
                voice.say(sp2_T);
                break;
            case 5:
                voice.say(sp2_FIF_);
                voice.say(sp2_T);
                break;
            case 6:
                voice.say(sp2_SIX);
                voice.say(sp2_T);
                break;
            case 7:
                voice.say(sp2_SEVEN);
                voice.say(sp2_T);
                break;
            case 8:
                voice.say(sp2_EIGHT);
                voice.say(sp2_T);
                break;
            case 9:
                voice.say(sp2_NINE);
                voice.say(sp2_T);
                break;
            }
            n %= 10;
        }
        switch (n) {
        case 1:
            voice.say(sp2_ONE);
            break;
        case 2:
            voice.say(sp2_TWO);
            break;
        case 3:
            voice.say(sp2_THREE);
            break;
        case 4:
            voice.say(sp2_FOUR);
            break;
        case 5:
            voice.say(sp2_FIVE);
            break;
        case 6:
            voice.say(sp2_SIX);
            break;
        case 7:
            voice.say(sp2_SEVEN);
            break;
        case 8:
            voice.say(sp2_EIGHT);
            break;
        case 9:
            voice.say(sp2_NINE);
            break;
        case 10:
            voice.say(sp2_TEN);
            break;
        case 11:
            voice.say(sp2_ELEVEN);
            break;
        case 12:
            voice.say(sp2_TWELVE);
            break;
        case 13:
            voice.say(sp2_THIR_);
            voice.say(sp2__TEEN);
            break;
        case 14:
            voice.say(sp2_FOUR);
            voice.say(sp2__TEEN);
            break;
        case 15:
            voice.say(sp2_FIF_);
            voice.say(sp2__TEEN);
            break;
        case 16:
            voice.say(sp2_SIX);
            voice.say(sp2__TEEN);
            break;
        case 17:
            voice.say(sp2_SEVEN);
            voice.say(sp2__TEEN);
            break;
        case 18:
            voice.say(sp2_EIGHT);
            voice.say(sp2__TEEN);
            break;
        case 19:
            voice.say(sp2_NINE);
            voice.say(sp2__TEEN);
            break;
        }
    }
}

type or paste code here

type or paste code here

You are probably running out of memory. Post the memory usage summary that appears in the Arduino IDE when you upload the program.

as far as memory usage goes it says "sketch uses 10938 bytes (33%) of program storage space, max is 32256 bytes. global variables use 582 bytes (28%) of dynamic memory, leaving 1466 bytes for local variables. max is 2038 bytes

I don't see anything wrong with the code.

Bad connections, external interference, unstable or noisy power supplies and overloading the 5V output or other outputs would be the next suspects.

Please post a wiring diagram, a photo of the setup and post links to everything else that is connected to the Uno. In particular, post the details of how the audio amplifier is connected.

If you connected a speaker to a digital pin, stop. That is a serious problem.

Try this at 9600. Maybe a buffer is being choked.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.