Pages: [1]   Go Down
Author Topic: writing and reading integers  (Read 1156 times)
0 Members and 1 Guest are viewing this topic.
Sweden, Bollebygd
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
I've created a small datalogger that read 16bit integers from a CAN network and I want to store them as 16bit itegers using the sd.write(byte) see code below. I don't know what I'm doing wrong, but the file will not initialize. I'm greatful for any help I can get. In the code below I've tried to initialize the file in four sequences in order to save RAM.

Also, in order to save RAM memory I would like to read one integer at a time by using the SD.seek(position) and read the integer value by SD.PEEK(). Will that work?

Thanks' for your help.

Code:
//_______________________________Initialize SD card and files_________________________
void initSdCard(void){
  uint16_t zeroValue = 0;
  if (SD.exists(logFileName)) {
    lcd.print(logFileName); lcd.print(" exists.");
    delay(2000);
  }
  else {
    lcd.print(logFileName);
    lcd.print(" doesn't exist.");
    // open a new file and immediately close it:
    delay(1000);
    lcd.print("Creating ");
    lcd.print(logFileName);
    lcd.print("...");
    polarFile = SD.open(logFileName, FILE_WRITE);
    polarFile.close();
    delay(500);            //_____________Wait to close file_____________
    lcd.print("file created");
    delay(1500);
    lcd.clear();
    //____________________________Write the the matrix. 72 columns (numberOfWindDirectionColumns) by 40 rows (numberOfWindSpeedRows)_____________________       
    //____________________________ fileFramSize = 10, used to avoid have the complete matrix in flash (limited memory)
        for(uint8_t frameNo=1; frameNo < (numberOfWindSpeedRows / fileFrameSize); frameNo++){
          uint16_t cellNo = 0;
          Serial.print("first loop started");    //*******************
          delay(500);
          File polarFile = SD.open(logFileName, FILE_WRITE);
          if (polarFile) {
            Serial.print(logFileName);    //******************
            Serial.println("opened");    //********************'
            for(uint8_t row=1;row < (fileFrameSize+1); row++){
              Serial.println("second loop");
              for(uint8_t column=1; column < (numberOfWindDirectionColumns); column++){
//              polarFile.write(word(row,column),2);      //row is the high byte and column is the
                polarFile.write(row);    // One 16 bit data consists of row (windDir) high byte and
                polarFile.write(column); // column (windSpeed) low byte
                cellNo++;
                delay(10);
              }
            }
          }
        polarFile.close();
        Serial.println("File closed");
        }
    lcd.print(logFileName);
    lcd.print(" initiated OK");
  }
}


Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 644
Posts: 50524
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
                polarFile.write(row);    // One 16 bit data consists of row (windDir) high byte and
                polarFile.write(column); // column (windSpeed) low byte
row and column are your loop indexes. They are not the data from your CAN network data. They are not integers. They are bytes.

If the file won't initialize, that has nothing to do with the (il)logic of the code that would write to the file.

Some serial output would be good to see.

Code:
            Serial.print(logFileName);    //******************
            Serial.println("opened");    //********************'
Do these comments convey any useful information? Not to me, they don't. Comments like this are useless and interfere with reading the code. Lose them.
Logged

Sweden, Bollebygd
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Paul and thanks' for you response. It looks as you've missunderstood my question. Row an colums are bytes, ok. My intentio is to initialize the file by (for various reasons) writing two bytes (uint8_t) using the file.write() with a byte parameter. Together they will form an integer that in a later stage be read as an integer. Make sense?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 644
Posts: 50524
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Row an colums are bytes, ok.
But, they are loop indexes. They are not wind speed or wind direction values.

Quote
My intentio is to initialize the file by (for various reasons) writing two bytes (uint8_t) using the file.write() with a byte parameter.
So, what is the problem?

What you write INTO the file after you've opened it has no bearing on your ability to create the file in the first place.
Logged

Sweden, Bollebygd
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well, when reading the file it's emty. The file is there, but no content. That's my problem now. smiley-sad

Any ideas why?

Thanks' for you comments. _/) P-L
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 644
Posts: 50524
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Any ideas why?
Without seeing your serial output? No.

I can't tell whether your outer for loop iterates at all. Without knowing that, it's impossible to say what is going on.

It does seem strange to open and close the file each time through that loop, though.

I have a hard time following code that does not have the { and } lined up. I find it much easier to understand code like:
Code:
if(polarFile)
{
   for(int i=0; i<10; i++)
   {
      for(int j=0; j<20; j++)
      {
         polarFile.write(i);
         polarFile.write(j);
      }
   }
   polarFile.close();
}

It's real obvious to me, for instance, that the file is only closed if it is opened. It's real clear where the for loops start and end. Your code, on the other hand, is not clear at all.
Logged

Sweden, Bollebygd
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, got your point. I'll come back here after a short busines trip.
Logged

Pages: [1]   Go Up
Jump to: