Thank you PaulS for the reply. I have made some of the adjustments.
Except for this part because I still need to work out the packet marker solution.
while (Serial.available()) {
delay(100);
char c = Serial.read();
readString += c;
}
The code now runs the loop over and over but, it ignores my "if" statements in the "setup" area of the code.
// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later
// SD card attached to SPI bus as follows:
// MOSI - pin 11:
// MISO - pin 12:
// CLK - pin 13:
// CS - pin 8:
int startattempt = 0; //counter for start attempts:
int talking = 0; // communication status bit:
String readString; // storage for incoming characters:
char intalk = 0; // incoming serial hex byte marker:
long rxbyte = 0; //used for storing incoming :
int txbyte = 0; //outgoing serial hex byte:
int bytecount = 0; //may use for counting string inputs:
int outputpin = 1; //TX pin control to for HIGH/LOW state:
int failurepin = 9; // led pin to show card failure:
long charAt = 0; // possibly used to find characters in string:
unsigned long previousMillis = 0; // will store last time:
unsigned long currentMillis = 0; // stores current time:
unsigned long interval = 150; // interval at which to:
int i = 0;
char inChar = 0;
String stringOne = 0; // string for storing incoming bytes
#include <LiquidCrystal.h>
#include <SD.h>
// initialize the library with the numbers of the interface pins:
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
#include <SD.h>
const int chipSelect = 8;
void setup() {
Serial.begin(9600); // opens serial port
// put your setup code here, to run once:
// Pin 1 has an LED connected on most Arduino boards:
lcd.begin(16, 2); // set up the LCD's number of columns and rows:
lcd.setCursor(2,1);
lcd.print("Ini. SD card");// Print a message to the LCD.
Serial.print("Ini SD card");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
lcd.scrollDisplayLeft();
lcd.print("Card failed, or not present");
// don't do anything more:
return;
}
pinMode(13, OUTPUT);
Serial.println("serial test 0021"); // so I can keep track of what is loaded
if (talking == 0 && startattempt < 100){
pinMode(outputpin, OUTPUT); // initialize the digital pin 1 as an output to control communication intialization:
digitalWrite(outputpin, HIGH);
delay(300);
if (talking == 0){
while (Serial.available()) {
delay(100);
char c = Serial.read();
readString += c;
if (readString.length() >0 && talking == 0) {
Serial.println(readString);
lcd.clear(); // clear the screen
if (readString == "11" && talking == 0) {
readString = ""; // clear readstring to reuse it
talking = 1;
digitalWrite(13, HIGH);
Serial.println("completed ini");
lcd.print("completed ini");
Serial.print(129,HEX);
}
else {
digitalWrite(13, LOW);
Serial.println(" ini. failed" );
lcd.print(" ini. failed");
Serial.println (readString);
lcd.scrollDisplayLeft();
lcd.print(readString);
readString = ""; // clear readstring to reuse it
startattempt = startattempt + 1;
}
}
}
}
}
}
void loop() {
if (talking == 1){
currentMillis = millis();
previousMillis = currentMillis; // set the time counter:
talking = 2; // lock into the next loop of communication
}
while(talking == 2 && currentMillis - previousMillis < interval ){
Serial.println(" big dog"); // at way to see progress:
currentMillis = millis(); // refresh the timer:
}
while (talking = 2) {
talking = 3;
interval = 140; // adjusted interval for delay in the loop;
currentMillis = millis();
previousMillis = currentMillis; // set the time counter:
Serial.println("data request");
lcd.print("data request");
Serial.print(128, HEX);
talking = 4;
bytecount = 0;
delay(10);
while (bytecount < 50 && currentMillis - previousMillis < interval ) {
char c = Serial.read();
readString += c;
bytecount = bytecount + 1;
currentMillis = millis(); // refresh the timer:
}
if (readString.length() > 0 && talking == 4) {
Serial.println ("bytecounter full");
Serial.println(readString);
Serial.println(bytecount);
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.csv", FILE_WRITE);
Serial.println("file open");
// if the file is available, write to it:
if (dataFile) {
dataFile.println(readString);
dataFile.close();
Serial.println("file closed");
readString = ""; // clear readstring to reuse it
talking = 3; // go back to data request:
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
lcd.print("error opening datalog.txt");
}
}
}
}