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 .
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
}
}
}