Arduino powered with 18650 freezes randomly

Hey everyone, I'm trying to make an arduino sensor network, it consists on 3 analog sensors, an xbee module to send the data and a relay to turn off the sensors and save battery. My problem is that the arduino will randomly freeze and keep the relay on and stop sending data and I'm finding it impossible to find the problem. This is my code:

#include <stdio.h>
#include <SoftwareSerial.h>
#include "LowPower.h"    
#define xbeerx 0                                          
#define xbeetx 1 
#define rx 5
#define tx 4


                                           

byte leaf_moisture_sensor_pin = A4;
byte vcc = 10;
byte soil_moisture_sensor_pin = A6;
byte soil_resistivity_sensor_pin = A7;
SoftwareSerial XBee(xbeerx, xbeetx); // RX, TX


void setup()
{
     pinMode(vcc, OUTPUT);
     digitalWrite(vcc, 1);
     XBee.begin(9600);                                //set baud rate for the software serial port to 9600         
}

String read_leaf_moisture_sensor() {
  delay(500);
  int value = analogRead(leaf_moisture_sensor_pin);
  delay(500);
  value = value + analogRead(leaf_moisture_sensor_pin);
  delay(500);
  value = value + analogRead(leaf_moisture_sensor_pin);
  value = value/3;
  return String(value);
}


String read_soil_moisture_sensor() {
  delay(500);
  int value = analogRead(soil_moisture_sensor_pin);
  delay(500);
  value = value + analogRead(soil_moisture_sensor_pin);
  delay(500);
  value = value + analogRead(soil_moisture_sensor_pin);
  value = value/3;
  value = 1023-value;
  return String(value);
}


String read_soil_resistivity_sensor() {
  delay(500);
  int value = analogRead(soil_resistivity_sensor_pin);
  delay(500);
  value = value + analogRead(soil_resistivity_sensor_pin);
  delay(500);
  value = value + analogRead(soil_resistivity_sensor_pin);
  value = value/3;
  value = 1023-value;
  return String(value);
}




void loop(){
  void(* resetFunc) (void) = 0;
  delay(200);
  digitalWrite(vcc, 0);
  delay(1000);
  XBee.print("field3="+read_leaf_moisture_sensor()+"/");  
  delay(15000);
/*  XBee.print("field2="+read_soil_resistivity_sensor()+"/");  
  delay(15000);
  XBee.print("field1="+read_soil_moisture_sensor()+"/"); */
  delay(2000);
  digitalWrite(vcc, 1);
  delay(1000);
  delay(20000);
  resetFunc();
 }

Thanks in advance!

Edit: I forgot to say that I'm using an arduino nano and 2 18650 batteries in series to power the arduino and I have everything connected to the 5v port of the arduino except for the relay which is connected to VIN.

void(* resetFunc) (void) = 0;

What is this? And what is its purpose?

arduino_new:

void(* resetFunc) (void) = 0;

What is this? And what is its purpose?

I got it from this page: https://www.instructables.com/id/two-ways-to-reset-arduino-in-software/

I use it to reset the arduino once the loop is done so I make sure it isn't a memmory related issue.

I don't see anything in the code that could cause the problem beside your "creative" loophole to make use of String. By "creative" i meant dumb.

Consider rewrite your code using cstring (char array with null-terminated) instead.

arduino_new:
I don't see anything in the code that could cause the problem beside your "creative" loophole to make use of String. By "creative" i meant dumb.

Consider rewrite your code using cstring (char array with null-terminated) instead.

So if the problem isn't in the code it must be hardware related, I was considering using a 555 timer to reset the arduino but that would not mean that the problem is solved, it just would ignore it.

Is the relay only used to connect/disconnect power to the resistive sensors.?
Can't you power the sensors with an output pin (no relay).
Post a diagram.
Leo..

This should not be INSIDE loop():

void(* resetFunc) (void) = 0;

Try it like this:

#include <stdio.h>
#include <SoftwareSerial.h>
#include "LowPower.h"   
#define xbeerx 0                                         
#define xbeetx 1
#define rx 5
#define tx 4


                                           

byte leaf_moisture_sensor_pin = A4;
byte vcc = 10;
byte soil_moisture_sensor_pin = A6;
byte soil_resistivity_sensor_pin = A7;
SoftwareSerial XBee(xbeerx, xbeetx); // RX, TX


void setup()
{
     pinMode(vcc, OUTPUT);
     digitalWrite(vcc, 1);
     XBee.begin(9600);                                //set baud rate for the software serial port to 9600         
}

String read_leaf_moisture_sensor() {
  delay(500);
  int value = analogRead(leaf_moisture_sensor_pin);
  delay(500);
  value = value + analogRead(leaf_moisture_sensor_pin);
  delay(500);
  value = value + analogRead(leaf_moisture_sensor_pin);
  value = value/3;
  return String(value);
}


String read_soil_moisture_sensor() {
  delay(500);
  int value = analogRead(soil_moisture_sensor_pin);
  delay(500);
  value = value + analogRead(soil_moisture_sensor_pin);
  delay(500);
  value = value + analogRead(soil_moisture_sensor_pin);
  value = value/3;
  value = 1023-value;
  return String(value);
}


String read_soil_resistivity_sensor() {
  delay(500);
  int value = analogRead(soil_resistivity_sensor_pin);
  delay(500);
  value = value + analogRead(soil_resistivity_sensor_pin);
  delay(500);
  value = value + analogRead(soil_resistivity_sensor_pin);
  value = value/3;
  value = 1023-value;
  return String(value);
}




void loop(){
  delay(200);
  digitalWrite(vcc, 0);
  delay(1000);
  XBee.print("field3="+read_leaf_moisture_sensor()+"/"); 
  delay(15000);
/*  XBee.print("field2="+read_soil_resistivity_sensor()+"/"); 
  delay(15000);
  XBee.print("field1="+read_soil_moisture_sensor()+"/"); */
  delay(2000);
  digitalWrite(vcc, 1);
  delay(1000);
  delay(20000);
  resetFunc();
 }
void(* resetFunc) (void) = 0;
#include <SoftwareSerial.h>
#include "LowPower.h"   
#define xbeerx 0                                         
#define xbeetx 1

Why are you doing software serial on the hardware serial pins?

Making you read_ functions return Strings is stupid. The print() method of the hardware or software serial class can convert an int to string without your ham-fisted "help".

What it can't do is ADD a string and an int. Quit being lazy and use two print statements.