Striped lines instead of cymbols on startup program

An ex colleague of mine left a program which reads data from a luminox oxygen sensor and writes it to a sd card and a lcd. Now I want the oxygen sensor to control a solenoid valve via a relays after a push of a button. got that to work.. but when I power up the system the display gives blinking striped lines. which turn into the correct oxygen level I want display only after I pushed the button. what is going wrong here?

Ps. Im new to Arduino :blush:.

Code:



#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
#include <SPI.h>  //for sd card
#include <SD.h>   //for sd card
#define  DEBUG true
#include "RTClib.h"  //Real time clock library
#include <Wire.h>
#define  vPin A1 //for relays valve
#define  startButton A0 //Startbutton for process


const int rs = 7, en = 6, d4 = 5, d5 = 10, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

SoftwareSerial myserial(8, 9);

long delayPeriod;
String Luminoxstring = "";
String t;                                   //temperature
String o;                                   //oxygen
boolean Luminox_stringcomplete = false;
int startLoop;

//SD Card Section

File myFile;
char logname[12];
const int chipSelect = 4;

//Time
RTC_DS3231 rtc;

String print_time(DateTime timestamp) {
  char message[120];

  int Year = timestamp.year();
  int Month = timestamp.month();
  int Day = timestamp.day();
  int Hour = timestamp.hour();
  int Minute = timestamp.minute();
  int Second= timestamp.second();

  sprintf(message, "%d-%d-%d %02d:%02d:%02d", Month,Day,Year,Hour,Minute,Second);
  
  return message;
}

void setup() {

  Serial.begin(9600);
  myserial.begin(9600);
  Luminoxstring.reserve(41);
  lcd.begin(16, 2);
  
  pinMode (vPin,OUTPUT);                  
  pinMode (startButton,INPUT_PULLUP);
  
  //test whether sd card is present
  Serial.print("Initializing SD card...");

  if (!SD.begin(4)) 
  {Serial.println("initialization failed!");
  while (1);}
  Serial.println("initialization done.");

  for (int i=1;i<=9999;i++)
  {snprintf(logname,sizeof(logname),"data%04d.txt",i);
  if (!SD.exists(logname))
  {break;}
  }
  
  Serial.println("initialization done. Logging to Filename: ");   
  Serial.println(logname);

  rtc.begin();
  if (!rtc.begin()){
    Serial.println("Clock is not running");
  }

  DateTime now = rtc.now();
  myFile = SD.open(logname, FILE_WRITE);
  myFile.println(print_time(now));
  myFile.close();
}

void loop() {
    lcd.display();
    
      while (myserial.available()){                //when a char is available in software serial buffer
        char inchar = (char)myserial.read();       //grab that char
        Luminoxstring += inchar;                   //add the received char to LuminoxString
      if (inchar == '\r'){                         //if the incoming character is a <term>, reset
        Luminox_stringcomplete = true;             //then a complete string of data has been received from Luminox-O2 sensor  
        }
    }

    if (Luminox_stringcomplete) {                  //has a complete string from the Luminox sensor has been received?
      Luminoxstring.remove(41);                    //remove any serial string overruns between reads
      Serial.print(Luminoxstring);                 //use the Arduino serial port to send that data to CoolTerm
      }
      
    //Luminoxstring write to SD card  (Data from sensor)  
       
    File myFile = SD.open(logname,FILE_WRITE);
    if (myFile){
      myFile.println(Luminoxstring);
      myFile.close();
    }
      t="";
      t+=(Luminoxstring.charAt(11));
      t+=(Luminoxstring.charAt(12));
      t+=(Luminoxstring.charAt(13));
      t+=(Luminoxstring.charAt(14));
      t+=(Luminoxstring.charAt(15));
      
      o="";
      o+=(Luminoxstring.charAt(27));
      o+=(Luminoxstring.charAt(28));
      o+=(Luminoxstring.charAt(29));
      o+=(Luminoxstring.charAt(30));
      o+=(Luminoxstring.charAt(31));

      Serial.println (o);
          
      Luminoxstring = "";                        //then clear the Luminoxstring:
      Luminox_stringcomplete = false;            //await the next data string from Luminox-O2 sensor
      delay(delayPeriod);                      
    
      lcd.setCursor(0, 0);                       // Cursor on place 0, row 0 
      lcd.print("Temp.  :");                     // Temp. : write to LCD
      lcd.setCursor(9, 0);                       // Cursor on place 9, row 0 
      lcd.print(t);                              // write "t" to LCD (temperature)
      lcd.setCursor(15, 0);                      // Cursor on place 15, row 0
      lcd.print("C");                            // C write to LCD
      lcd.setCursor(0, 1);                       // Cursor on place 0, row 1 
      lcd.print("O2-Geh.:");                     // O2-Geh.: write to LCD
      lcd.setCursor(9, 1);                       // Cursor on place 9, row 1 s
      lcd.print(o);                              // write "o" to LCD (oxygen percentage)
      lcd.setCursor(15, 1);                      // Cursor on place 15, row 1
      lcd.print("%");                            // write % to LCD
      
            if (digitalRead(startButton)==LOW){  //Reads if the start button is pressed
        startLoop = 1;                           //Set startloop
      }

      if (startLoop == 1){                       //start the if section after button is pushed
        if (o>"18.00"){                          
          digitalWrite (vPin, HIGH);             //Solenoid valve opens if oxygen is above 18%
          delay(2000);
        } else { 
          digitalWrite (vPin, LOW);              //Solenoid valve closes if oxygen is under 18%
          delay(2000);
          startLoop = 0;                         //Reset startloop
        }
      }
    }
  

https://ibb.co/qBvvdY9

Hello, do yourself a favour and please read How to get the best out of this forum and modify your post accordingly (including code tags and necessary documentation of your ask).

I was searching that, thanks

is that really the oxygen sensor library ?

what Arduino are you using?


snprintf(logname, sizeof(logname), "data%04d.txt", i);

logname is defined as an array of 12 chars ➜ char logname[12]; so 11 characters available for the name and then the trailing null char.

you already have 8 characters taken by the static part of the file name data.txt so it leaves only 3 spaces available in which you cram 4 characters

➜ that will overflow the buffer and thus your filename won't be what you expect

Ugh, yuk, yikes.
Ok, Strings can work. But they're...not optimal and in practice more of a liability than an asset.
How about taking your Serial parsing exercise a little further and actually parse float or int variables from your Serial input and outputting those onto the LCD.

is that really the oxygen sensor library ?

Nope sorry, Its the real time clock.

what Arduino are you using?

Arduino Uno

What do you suggest changing?

I would do char logname[13]; for a start and see what happens

what do you see in the serial monitor?

PS: your image for others so that they don't need to go to an alternate site

seems the string being asked to be displayed is not initialized properly

Changed it to 13.
im getting 2 documents.
DATA0001.TX
&
DATA001 (Text document)
With:
8-17-2021 14:22:57

O 0203.6 T +19.3 P 1010 % 020.16 e 0000

But it does not change anything regarding the LCD screen. If i upload the code or power the system, the LCD gives this weird bug. untill i press the start button. after that the temps and oxygen levels display as normal.

This is what the monitor shows:
Initializing SD card...initialization done.
initialization done. Logging to Filename:
data0002.txt

O 0203.0 T +19.6 P 1010 % 020.09 e 0000
20.09

Try putting lcd.clear() after lcd,begin() in setup. You have lcd.display() at the start of loop before sending anything to the display, and I am not sure what that accomplishes.

the problem is here:

 if (Luminox_stringcomplete) {                  //has a complete string from the Luminox sensor has been received?
    Luminoxstring.remove(41);                    //remove any serial string overruns between reads
    Serial.print(Luminoxstring);                 //use the Arduino serial port to send that data to CoolTerm
  }

  //Luminoxstring write to SD card  (Data from sensor)

  File myFile = SD.open(logname, FILE_WRITE);
  if (myFile) {
    myFile.println(Luminoxstring);
    myFile.close();
  }
  t = "";
  t += (Luminoxstring.charAt(11));
  t += (Luminoxstring.charAt(12));
  t += (Luminoxstring.charAt(13));
  t += (Luminoxstring.charAt(14));
  t += (Luminoxstring.charAt(15));

  o = "";
  o += (Luminoxstring.charAt(27));
  o += (Luminoxstring.charAt(28));
  o += (Luminoxstring.charAt(29));
  o += (Luminoxstring.charAt(30));
  o += (Luminoxstring.charAt(31));

your loop builds up the Luminoxstring from what's in the incoming Serial buffer but there is no guarantee the string will be complete (might take a couple loops).

Then you test for completeness with

  if (Luminox_stringcomplete) {                  //has a complete string from the Luminox sensor has been received?
    Luminoxstring.remove(41);                    //remove any serial string overruns between reads
    Serial.print(Luminoxstring);                 //use the Arduino serial port to send that data to CoolTerm
  }

but you close the if bracket and then you go build t and o regardless of the status of Luminoxstring by concatenating stuff in memory that might be totally irrelevant

  t = "";
  t += (Luminoxstring.charAt(11));
  t += (Luminoxstring.charAt(12));
  t += (Luminoxstring.charAt(13));
  t += (Luminoxstring.charAt(14));
  t += (Luminoxstring.charAt(15));

  o = "";
  o += (Luminoxstring.charAt(27));
  o += (Luminoxstring.charAt(28));
  o += (Luminoxstring.charAt(29));
  o += (Luminoxstring.charAt(30));
  o += (Luminoxstring.charAt(31));

So the first time (and until something is received) when you print the info onto the LCD


  lcd.setCursor(0, 0);                       // Cursor on place 0, row 0
  lcd.print("Temp.  :");                     // Temp. : write to LCD
  lcd.setCursor(9, 0);                       // Cursor on place 9, row 0
  lcd.print(t);                              // write "t" to LCD (temperature)
  lcd.setCursor(15, 0);                      // Cursor on place 15, row 0
  lcd.print("C");                            // C write to LCD
  lcd.setCursor(0, 1);                       // Cursor on place 0, row 1
  lcd.print("O2-Geh.:");                     // O2-Geh.: write to LCD
  lcd.setCursor(9, 1);                       // Cursor on place 9, row 1 s
  lcd.print(o);                              // write "o" to LCD (oxygen percentage)
  lcd.setCursor(15, 1);                      // Cursor on place 15, row 1
  lcd.print("%");                            // write % to LCD

you get garbage

the whole part that depends on having Luminox_stringcomplete true should be embedded in the if() statement


I would recommend to drop the String business and I would suggest to study Serial Input Basics to handle this

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