Problems reading Data from a SD-Card to PWM (char to int?)

Hello everybody,

I´m a arduino beginner and not very familiar with programming in general. In advance I say thank you to everybody who can give me a little hint or advice concerning the following subject:

The idea is to log data on a SD card and a few days later, to read the data and dim a LED on a digital pin with PWM. This is just an example for a more complex thing.

I logged data on a SD card, using a simple potentiometer from an analog pin (0-1023). The „core“ of this patch is shown under REC. This works fine! I made another file (excerpt: PLAY) to read this data from the SD card, but I have some trouble doing this. (Just because of testing reasons I made 2 different files for the moment, REC and PLAY). As far as I understand, the data on the SD card is ASCII; when I „println“ the data on the serial monitor, I get values like 10 13 48 13 10… I´m able to interpret these values (p.e. 48 = 0…) I tried several methods to address a digital Pin(0-255) to dim the LED using data from the SD card, but without any success so far. At least using the PLAY patch, I got as many 0´s in my serial monitor as the serial monitor lists numbers while logging. I deduce, that there is some kind of conversion from a string of chars to int(?), but I only got zero´s… :-((( The compiler works fine, I d´ont get error messages.

The final REC/PLAY patch should run stable for many days, therefore I´m not sure if using strings would be the best way to do this, since I´ve read, that dynamic memory allocation could cause some problems in practice.

I´m using:

http://www.sparkfun.com/products/12761 Arduino uno/R3 Software Arduino 1.0.6 SD.h library v1.3


REC:

File dataFile = SD.open(„data.txt", FILE_WRITE);  // creates/opens the dataFile
                                                    
if (dataFile && valRED == HIGH) {
   dataFile.println(sensorValue);      //recording per line
   dataFile.close();
   delay(2);

  }

PLAY:

int SDValues = dataFile.read();
int sdVal = 0;
int sdValIndex = 0;

String sdValtoSTR;
   
dataFile = SD.open(„data.txt", FILE_READ);    // opens data.txt
  
  if (dataFile) {                          
      digitalWrite(6, HIGH); turn on control LED

    while (dataFile.available()) {
      // Serial.write(dataFile.read());  // if not commented out, the serial monitor shows values from 0-1023
      // Serial.println(dataFile.read());    // if not commented out, the serial monitor shows ASCII char (48, 49, ...)

        sdVal = dataFile.read();

      if ( sdVal >= 48 ) {
        sdValtoSTR.setCharAt(sdValIndex, String(sdVal).charAt(0));   // actually nothing is shown in the serial monitor out of these lines
        sdValIndex++;
      }

      if ( sdVal == 10 ) {
        sdValIndex = 0;                                    // I´m not sure, but these two lines produce constantly 0 in the serial monitor

      } 

      Serial.println(sdValtoSTR.toInt());
      //analogWrite(AtmungLED, SDValues);     
    } 

    dataFile.close(); // close the file:
    digitalWrite(6, LOW); //turn off control LED

Thank you for any comment or hint. Best regards! :) :confused:

      if ( sdVal >= 48 ) {
        sdValtoSTR.setCharAt(sdValIndex, String(sdVal).charAt(0));   // actually nothing is shown in the serial monitor out of these lines
        sdValIndex++;
      }

It's really rather pathetic that you are wasting resources on a (multiple, actually) String instances, and then doing all that monkey motion stuff to treat the String as a string. Shitcan the String and use a string (a NULL terminated array of chars).

Thank you for the comment. Unfortunately I could´t solve the problem yet. I would really appreciate it, if somebody has a link or something like that, showing some methods related to the subject I described... as I said, I´m a real real newbie... :-(

Best regards.

Replace that nonsense dealing with Strings with:

char string[32];
      if ( sdVal >= 48 )
     { // Down here where it belongs
         string[sdValIndex++] = sdVal;
         string[sdValIndex] = '\0';

Thank you so much, it´s getting better!!! :-) Finally I get some usable data for the digital Pin, although the values are tripled... 0,0,0,0,0,0,0,2,23,234,234,234,0,0,0,0....

Finally I get some usable data for the digital Pin, although the values are tripled

My crystal ball says to fix line number 33875434785947295151785.