Go Down

Topic: CSV file read stops after 9 lines? (Read 2 times) previous topic - next topic

seguinc

Good morning all,

I am in the process of assembling a project using an Arduino Mega 2560.  It consists, in part, of reading in a CSV data file that will have a sequence of a relay addresse followed by each relay's on-time and off-time.  As you can surmise; the sketch causes relays connected to the Mega to turn on then off.  The CSV is formatted with integers in the following manner: relay number, time on (in millis), time off (in millis) terminated by a new line.  Here is a sequence I used to test all relays (for now 16, but this project will have 32 relays in the end):
1,250,250
2,250,250
3,250,250
4,250,250

...etc, you can see where this is going.  I currently have the sketch designed so this information outputs to a serial monitor (on my Linux box, I use a serial port terminal which permits the ability to read in the CSV file directly from my PC and send it to the USB port);in order to see what the sketch is doing with the info.

The issue I have encountered is that the whole thing stops after the 9th relay.  I've tried changing the variable types, played with the on-times and off-times making them longer and shorter, but nothing seems to change.  I can only presume the Mega is running out of memory, but the array I use (data[3]) is recycled at every iteration, so the memory addresses used by the Mega have to be overwritten.  I attempted using the Serial.flush() function right after sending the data array to the relay activation subroutine, but it does not seem to help.

Here is the entire sketch:
Code: [Select]
int data[3];
int counter;
int inbyte;
int relaypin;

void setup(){
  // initialize serial communication:
  Serial.begin(19200);
  counter = 0;
  // initialize the relay pins:
  for (relaypin = 22; relaypin < 38; relaypin++) {
    pinMode(relaypin, OUTPUT);
  }
}
void loop(){
  if (Serial.available()){
    inbyte = Serial.parseInt();// check for comma separated data
    data[counter] = inbyte;
    counter++;
    if(Serial.read() ==  '\n'){  // end of line test
      data[counter] = inbyte;
      counter = 0;
      //send data to actRelay func
      actRelay(data);
      Serial.print("Relay:");
      Serial.println(data[0]);
      Serial.print("Time ON:");
      Serial.println(data[1]);
      Serial.print("Time OFF:");
      Serial.println(data[2]);
    }
   
  }
}

void actRelay(int* array){
        switch (data[0]) {
        case 1:
          digitalWrite(22, HIGH);
          delay(data[1]);
          digitalWrite(22,LOW);
          delay(data[2]);
          break;
        case 2:
          digitalWrite(23, HIGH);
          delay(data[1]);
          digitalWrite(23,LOW);
          delay(data[2]);
          break;
        case 3:
          digitalWrite(24, HIGH);
          delay(data[1]);
          digitalWrite(24,LOW);
          delay(data[2]);
          break;
        case 4:
          digitalWrite(25, HIGH);
          delay(data[1]);
          digitalWrite(25,LOW);
          delay(data[2]);
          break;
        case 5:
          digitalWrite(26, HIGH);
          delay(data[1]);
          digitalWrite(26,LOW);
          delay(data[2]);
          break;
        case 6:
          digitalWrite(27, HIGH);
          delay(data[1]);
          digitalWrite(27,LOW);
          delay(data[2]);
          break;
        case 7:
          digitalWrite(28, HIGH);
          delay(data[1]);
          digitalWrite(28,LOW);
          delay(data[2]);
          break;
        case 8:
          digitalWrite(29, HIGH);
          delay(data[1]);
          digitalWrite(29,LOW);
          delay(data[2]);
          break;
        case 9:
          digitalWrite(30, HIGH);
          delay(data[1]);
          digitalWrite(30,LOW);
          delay(data[2]);
          break;
        case 10:
          digitalWrite(31, HIGH);
          delay(data[1]);
          digitalWrite(31,LOW);
          delay(data[2]);
          break;
        case 11:
          digitalWrite(32, HIGH);
          delay(data[1]);
          digitalWrite(32,LOW);
          delay(data[2]);
          break;
        case 12:
          digitalWrite(33, HIGH);
          delay(data[1]);
          digitalWrite(33,LOW);
          delay(data[2]);
          break;
        case 13:
          digitalWrite(34, HIGH);
          delay(data[1]);
          digitalWrite(34,LOW);
          delay(data[2]);
          break;
        case 14:
          digitalWrite(35, HIGH);
          delay(data[1]);
          digitalWrite(35,LOW);
          delay(data[2]);
          break;
        case 15:
          digitalWrite(36, HIGH);
          delay(data[1]);
          digitalWrite(36,LOW);
          delay(data[2]);
          break;
        case 16:
          digitalWrite(37, HIGH);
          delay(data[1]);
          digitalWrite(37,LOW);
          delay(data[2]);
          break;
        }
}


Thank you in advance for helping.

MarkT

You are not checking Serial.available() before every Serial.read() I fear - you must check on every single read.
[ I won't respond to messages, use the forum please ]

PaulS

Quote
I attempted using the Serial.flush() function right after sending the data array to the relay activation subroutine, but it does not seem to help.

What good is blocking until all serial data has been sent, when you aren't sending any serial data?

seguinc

MarkT; thanks for the reply.  Would I then have to re-write the section that tests for the new line in the following manner?

Code: [Select]

void loop(){
  if (Serial.available()){
    inbyte = Serial.parseInt();// check for comma separated data
    data[counter] = inbyte;
    counter++;
    if (Serial.available() && Serial.read() ==  '\n'){  // end of line test
      data[counter] = inbyte;
      counter = 0;
    }


The original code seems to detect the \n (EOL).  I may be going about parsing the data in the wrong way.  This code has been edited from a few ideas I've found on this forum and others.


PaulS

Code: [Select]
    inbyte = Serial.parseInt();// check for comma separated data
That's not what parseInt() does.

Go Up