Code stops working after awhile

Hi! I made a combined sensor with NPK sensors and capacitive soil moisture sensor with an LCD screen that displays the values of both sensors. For some reason the Arduino stops printing the values and the entire thing just freezes after a certain amount of time (it varies). I have to restart the whole thing just to print the values again but it does freeze again after a certain amount of time.

#include <SoftwareSerial.h>
#include <Wire.h>
#include <Arduino.h>
#include <U8x8lib.h>
 
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif

U8X8_SSD1306_128X64_NONAME_HW_I2C u8x8(/* reset=*/ U8X8_PIN_NONE);
 
#define RE 8
#define DE 7
#define sensorPin1 A1
#define sensorPin2 A2
#define sensorPin3 A3
 
//const byte code[]= {0x01, 0x03, 0x00, 0x1e, 0x00, 0x03, 0x65, 0xCD};
const byte nitro[] = {0x01,0x03, 0x00, 0x1e, 0x00, 0x01, 0xe4, 0x0c};
const byte phos[] = {0x01,0x03, 0x00, 0x1f, 0x00, 0x01, 0xb5, 0xcc};
const byte pota[] = {0x01,0x03, 0x00, 0x20, 0x00, 0x01, 0x85, 0xc0};
 
byte values[11];
SoftwareSerial mod(10,11);
 
void setup() {
  Serial.begin(9600);
  mod.begin(9600);
  pinMode(RE, OUTPUT);
  pinMode(DE, OUTPUT);
  
  u8x8.begin();
  u8x8.setPowerSave(0);
}
 
void loop() {
  byte val1,val2,val3;
  val1 = nitrogen();
  delay(250);
  val2 = phosphorous();
  delay(250);
  val3 = potassium();
  delay(250);
  int value1 = analogRead(sensorPin1);
  int value2 = analogRead(sensorPin2);
  int value3 = analogRead(sensorPin3);
  
  Serial.print("Nitrogen: ");
  Serial.print(val1);
  Serial.println(" mg/kg");
  Serial.print("Phosphorous: ");
  Serial.print(val2);
  Serial.println(" mg/kg");
  Serial.print("Potassium: ");
  Serial.print(val3);
  Serial.println(" mg/kg");
   Serial.print("Analog output 1: ");
  Serial.println(value1);
   Serial.print("Analog output 2: ");
  Serial.println(value2);
   Serial.print("Analog output 3: ");
  Serial.println(value3);
  delay(3000);
 
  u8x8.setFont(u8x8_font_chroma48medium8_r);
  char val1String[4]; // Assuming the maximum length of the string representation of val1 is 4
sprintf(val1String, "%d", val1);

 u8x8.clearDisplay();
u8x8.setInverseFont(1);
u8x8.drawString(0, 0, "012345678901234567890123456789");
u8x8.setInverseFont(0);
u8x8.drawString(1, 1, "N:");
u8x8.drawString(3, 1, val1String); // Display val1




  char val2String[4]; // Assuming the maximum length of the string representation of val1 is 4
sprintf(val2String, "%d", val2);

u8x8.setInverseFont(1);
u8x8.setInverseFont(0);
u8x8.drawString(1, 2, "P:");
u8x8.drawString(3, 2, val2String); // Display val2




  char val3String[4]; // Assuming the maximum length of the string representation of val1 is 4
sprintf(val3String, "%d", val3);

u8x8.setInverseFont(1);
u8x8.setInverseFont(0);
u8x8.drawString(1, 3, "K:");
u8x8.drawString(3, 3, val3String); // Display val3
 

  char val4String[4]; // Assuming the maximum length of the string representation of val1 is 4
sprintf(val4String, "%d", value1);

u8x8.setInverseFont(1);
u8x8.setInverseFont(0);
u8x8.drawString(1, 4, "water1: ");
u8x8.drawString(8, 4, val4String); // Display val3
  

  char val5String[4]; // Assuming the maximum length of the string representation of val1 is 4
sprintf(val5String, "%d", value2);

u8x8.setInverseFont(1);
u8x8.setInverseFont(0);
u8x8.drawString(1, 5, "water2: ");
u8x8.drawString(8, 5, val5String); // Display val3
 

  char val6String[4]; // Assuming the maximum length of the string representation of val1 is 4
sprintf(val6String, "%d", value3);

u8x8.setInverseFont(1);
u8x8.setInverseFont(0);
u8x8.drawString(1, 6, "water3: ");
u8x8.drawString(8, 6, val6String); // Display val3
  
delay(3000);



}
 
byte nitrogen(){
  digitalWrite(DE,HIGH);
  digitalWrite(RE,HIGH);
  delay(10);
  if(mod.write(nitro,sizeof(nitro))==8){
    digitalWrite(DE,LOW);
    digitalWrite(RE,LOW);
    for(byte i=0;i<7;i++){
    //Serial.print(mod.read(),HEX);
    values[i] = mod.read();
    Serial.print(values[i],HEX);
    }
    Serial.println();
  }
  return values[4];
}
 
byte phosphorous(){
  digitalWrite(DE,HIGH);
  digitalWrite(RE,HIGH);
  delay(10);
  if(mod.write(phos,sizeof(phos))==8){
    digitalWrite(DE,LOW);
    digitalWrite(RE,LOW);
    for(byte i=0;i<7;i++){
    //Serial.print(mod.read(),HEX);
    values[i] = mod.read();
    Serial.print(values[i],HEX);
    }
    Serial.println();
  }
  return values[4];
}
 
byte potassium(){
  digitalWrite(DE,HIGH);
  digitalWrite(RE,HIGH);
  delay(10);
  if(mod.write(pota,sizeof(pota))==8){
    digitalWrite(DE,LOW);
    digitalWrite(RE,LOW);
    for(byte i=0;i<7;i++){
    //Serial.print(mod.read(),HEX);
    values[i] = mod.read();
    Serial.print(values[i],HEX);
    }
    Serial.println();
  }
  return values[4];
}

It may help to see a schematic of your hardware and a clear photograph that shows the device as built.

Thought about that, but they're all bytes. So max 255d.

You're right, I missed that.

Honestly, I'm not too sure as the code was copied from the internet. But it is returning the values from the NPK sensor which is what I needed.

will try this later. thanks!

Though I'm not sure about that, it is what I have right now and I'm just making do with what I have. Thanks for the insight though.

I suspect that the "NPK sensor" uses a random number generator, then massages the values to resemble actual data.

Maybe it uses the conductivity to seed the random number generator.

For a much more ambitious scam, take a look at Teralytic. You rent the sensor for something like $1-$2K/year, and THEY own all the data it transmits, whatever that might be.

I consulted some soil chemists about this operation, and they just shook their heads.

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