Pages: 1 [2] 3   Go Down
Author Topic: reading information from sd card  (Read 2182 times)
0 Members and 1 Guest are viewing this topic.
Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 654
Posts: 50931
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
"array2" is the name of the array.
In the Serial.print() call after the for loop, you are, as I showed before, referencing the array element at index 3.

Quote
this is all of my code,
Bullpoop. There ls no setup() function and no loop() function.

Code:
int counter;
Local variables, unlike global variables, are not initialized.

Code:
        if (counter >0){
At this point, counter could be 0, or it could be -28456 or 31874 or any other garbage left in that memory location.

Code:
          Serial.print(array);
          Serial.print("]");
          value = atoi(array);
The variable array is not NULL terminated. You should NOT be passing it to functions that expect a NULL terminated array of chars.

What is in this file you are trying to read? Why do you want to store one value in three different positions in the array?
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 74
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Bullpoop. There ls no setup() function and no loop() function.
I forgot to put the begining of the setup, all the program runs on the setup that's why there is no loop() function
Code:
#include <SD.h>

// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 10;

void setup()
{
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  Serial.print("Initializing 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");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
 
  // 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("path.txt");
const int len = 4;
char array[len+1], c;
int counter;
int value, i, array2[3];;
counter=0;
  // if the file is available, write to it:
  if (dataFile) {
    while (dataFile.available()) {
      for (i=0; i<3; i++){     
        c = dataFile.read();
        if(c==','){
          if (counter >0){
          //Serial.print("Buffer: [");
          //Serial.print(array);
          //Serial.print("]");
            value = atoi(array);
            array2[i]=value;
            Serial.println(array2[i]);
          //Serial.print("value: ");
          //Serial.println(value);
          }
          counter=0;
          int minimum= min (array2 [0], min (array2 [1], array2 [2]));
          Serial.print("minimum: ");
          Serial.println(minimum);         
          }       
        else{
          if (counter<len){
            array[counter++] = c;
            array[counter] = 0;
            }
        }
      }
    }
 dataFile.close();
 }
// if the file isn't open, pop up an error:
 
}

void loop()
{
}
this is the full code, here I am trying to put 3 values on array2 and I want to find the smallest value on the array.

Quote
What is in this file you are trying to read? Why do you want to store one value in three different positions in the array?
I have a file with several numbers and I need to take them 3 at a time and I need to find the smallest value inside of the array.
This program works when it comes to read all the values from the array.
Logged

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

Quote
I have a file with several numbers and I need to take them 3 at a time and I need to find the smallest value inside of the array.

The way that you read the file has problems. You'd see them easier of you put each { on a new line and use Tools + Auto Format.
Code:
  if (dataFile)
  {
    while (dataFile.available())
    {
      for (i=0; i<3; i++)
      {     
        c = dataFile.read();
If there is a file, while not at the end of the file, loop three times, to read 3 characters from the file.

That is hardly going to get you 3 integer values read from the file. Ger rid of the for loop, and initialize and increment i just like you do counter, except at different places.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 74
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I tried to read the array on the loop but all I got was 0 on it. this is the code, is the idea right?
Code:
/*
  SD card file dump
 
 This example shows how to read a file from the SD card using the
 SD library and send it over the serial port.
 
 The circuit:
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4
 
 created  22 December 2010
 by Limor Fried
 modified 9 Apr 2012
 by Tom Igoe
 
 This example code is in the public domain.
 
 */

#include <SD.h>

// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 10;

void setup()
{
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  Serial.print("Initializing 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");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
 
  // 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("path.txt");
const int len = 4;
char array[len+1], c;
int counter, counter2;
int value, i, array2[3];;
counter=0;
counter2=0;
  // if the file is available, write to it:
  if (dataFile) {
    while (dataFile.available()) {
        c = dataFile.read();
        if(c==','){
          if (counter >0){
          //Serial.print("Buffer: [");
          //Serial.print(array);
          //Serial.print("]");
            value = atoi(array);
            array2[counter2]=value;
            Serial.println(array2[counter2]);
          //Serial.print("value: ");
          //Serial.println(value);
          counter2++;
          counter=0;
          //if(counter2==2){
          //int minimum= min (array2 [counter2-2], min (array2 [counter2-1], array2 [counter2]));
          //Serial.print("minimum: ");
          //Serial.println(minimum);
          //counter2=0;
          //}         
          }       
        else{
          if (counter<len){
            array[counter++] = c;
            array[counter] = 0;
            }
        }
      }
    }
 dataFile.close();
 }
// if the file isn't open, pop up an error:
 
}

void loop()
{
}
Logged

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

The code is not working the way you want. And, yet you have commented out all the debug stuff. I'm sure that there is a good reason for that.

You've been asked to put all the { on a new line, and use Tools + Auto Format. You have not done so. I'm sure that there is a good reason for that.

What does the file you are reading from look like?
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 74
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I forgot to take to comments out I was trying to look at different stuff.
here is the code with the auto format
Code:
/*
  SD card file dump
 
 This example shows how to read a file from the SD card using the
 SD library and send it over the serial port.
 
 The circuit:
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4
 
 created  22 December 2010
 by Limor Fried
 modified 9 Apr 2012
 by Tom Igoe
 
 This example code is in the public domain.
 
 */

#include <SD.h>

// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 10;

void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  Serial.print("Initializing 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");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");

  // 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("path.txt");
  const int len = 4;
  char array[len+1], c;
  int counter, counter2;
  int value, i, array2[3];
  ;
  counter=0;
  counter2=0;
  // if the file is available, write to it:
  if (dataFile)
  {
    while (dataFile.available())
    {
      c = dataFile.read();
      if(c==',')
      {
        if (counter >0)
        {
          //Serial.print("Buffer: [");
          //Serial.print(array);
          //Serial.print("]");
          value = atoi(array);
          array2[counter2]=value;
          Serial.println(array2[counter2]);
          //Serial.print("value: ");
          //Serial.println(value);
          counter2++;
          counter=0;
          if(counter2==2){
          int minimum= min (array2 [counter2-2], min (array2 [counter2-1], array2 [counter2]));
          Serial.print("minimum: ");
          Serial.println(minimum);
          counter2=0;
          }         
        }       
        else
        {
          if (counter<len)
          {
            array[counter++] = c;
            array[counter] = 0;
          }
        }
      }
    }
    dataFile.close();
  }
  // if the file isn't open, pop up an error:

}

void loop()
{
}

all I get is zeros, this is what it looks like on the serial monitor
Quote
array2: 0
array2: 0
minimum: -4864
array2: 0
array2: 0
minimum: -4864
array2: 0
array2: 0
minimum: -4864
array2: 0
array2: 0
minimum: -4864
array2: 0
array2: 0
minimum: -4864
array2: 0
array2: 0
minimum: -4864
array2: 0
array2: 0
minimum: -4864
array2: 0
array2: 0
minimum: -4864
Logged

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

Code:
          //Serial.print("Buffer: [");
          //Serial.print(array);
          //Serial.print("]");
This just might tell you something.

Code:
          array2[counter2]=value;
          Serial.println(array2[counter2]);
          //Serial.print("value: ");
          //Serial.println(value);
          counter2++;
          counter=0;
          if(counter2==2){
          int minimum= min (array2 [counter2-2], min (array2 [counter2-1], array2 [counter2]));
You store a value in [ 0 ], then increment counter2 to 1.
You store a value in [ 1 ], then increment counter2 to 2.
Then, you find the minimum of the values stored in [ 0 ], [ 1 ], and [ 2 ], even though you have not stored anything in [ 2 ].

As far as I can tell, your file contains something like:
Joe, Tom, Fred<cr>
Bob, Dog, Cat<cr>

I'm going to continue operating on this assumption until you PROVE otherwise.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 74
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

what is happening is, when I use the original code I the program works fine all the numbers are stored in value
Code:
/*
  SD card file dump
 
 This example shows how to read a file from the SD card using the
 SD library and send it over the serial port.
 
 The circuit:
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4
 
 created  22 December 2010
 by Limor Fried
 modified 9 Apr 2012
 by Tom Igoe
 
 This example code is in the public domain.
 
 */

#include <SD.h>

// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 10;

void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  Serial.print("Initializing 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");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");

  // 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("path.txt");
  const int len = 4;
  char array[len+1], c;
  int counter, counter2;
  int value, i, array2[3];
  ;
  counter=0;
  counter2=0;
  // if the file is available, write to it:
  if (dataFile)
  {
    while (dataFile.available())
    {
      c = dataFile.read();
      if(c==',')
      {
        if (counter >0)
        {
          Serial.print("Buffer: [");
          Serial.print(array);
          Serial.print("]");
          value = atoi(array);
          //array2[counter2]=value;
          Serial.print("value: ");
          Serial.println(value);
          //Serial.print("array2: ");
          //Serial.println(array2[counter2]);
          //counter2++;
          counter=0;
          //if(counter2==3){
          //int minimum= min (array2 [counter2-3], min (array2 [counter2-2], array2 [counter2-1]));
          //Serial.print("minimum: ");
          //Serial.println(minimum);
          //counter2=0;
          }         
        }       
        else
        {
          if (counter<len)
          {
            array[counter++] = c;
            array[counter] = 0;
          }
        }
      }
    }
    dataFile.close();
  }
  // if the file isn't open, pop up an error:

//}

void loop()
{
}

with this code this is what I get on the serial monitor
Quote
Buffer: [156 ]value: 156
Buffer: [ 28 ]value: 28
Buffer: [ 156]value: 156
Buffer: [ 28 ]value: 28
Buffer: [ 99 ]value: 99
Buffer: [ 156]value: 156
Buffer: [ 28 ]value: 28
Buffer: [ 99 ]value: 99
Buffer: [ 156]value: 156
Buffer: [ 28 ]value: 28
Buffer: [ 98 ]value: 98
Buffer: [ 156]value: 156
but if I try to store them on the array this is what I get
Quote
Buffer: [,]value: 0
array2: 0
Buffer: [,]value: 0
array2: 0
Buffer: [,]value: 0
array2: 0
Buffer: [,]value: 0
array2: 0
Buffer: [,]value: 0
array2: 0
Buffer: [,]value: 0
array2: 0
Buffer: [,]value: 0
array2: 0
Buffer: [,]value: 0
array2: 0
is there any reason why this happens?
Logged

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

Would it kill you to uncomment ALL the Serial.print() statements?

Would it kill you to share the damned file?
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 74
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No, it wouldn't kill but I commented out those parts so you could see how the original code looked like and I assumed that you would be able to understand that the parts that I commented out were the ones that I added and were giving me problems. I am sorry if my assumption was wrong, here is the whole code with no comments.
Code:
/*
  SD card file dump
 
 This example shows how to read a file from the SD card using the
 SD library and send it over the serial port.
 
 The circuit:
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4
 
 created  22 December 2010
 by Limor Fried
 modified 9 Apr 2012
 by Tom Igoe
 
 This example code is in the public domain.
 
 */

#include <SD.h>

// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 10;

void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  Serial.print("Initializing 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");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");

  // 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("path.txt");
  const int len = 4;
  char array[len+1], c;
  int counter, counter2;
  int value, i, array2[3];
  ;
  counter=0;
  counter2=0;
  // if the file is available, write to it:
  if (dataFile)
  {
    while (dataFile.available())
    {
      c = dataFile.read();
      if(c==',')
      {
        if (counter >0)
        {
          Serial.print("Buffer: [");
          Serial.print(array);
          Serial.print("]");
          value = atoi(array);
          array2[counter2]=value;
          Serial.print("value: ");
          Serial.println(value);
          Serial.print("array2: ");
          Serial.println(array2[counter2]);
          counter2++;
          counter=0;
          if(counter2==3){
          int minimum= min (array2 [counter2-3], min (array2 [counter2-2], array2 [counter2-1]));
          Serial.print("minimum: ");
          Serial.println(minimum);
          counter2=0;
          }         
        }       
        else
        {
          if (counter<len)
          {
            array[counter++] = c;
            array[counter] = 0;
          }
        }
      }
    }
    dataFile.close();
  }
  // if the file isn't open, pop up an error:

}

void loop()
{
}

Now, would it kill you to help me to figure it out why the program stops to read the file after I try to pass these values to the array?
Logged

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

In order to help you, we need to see three things:
1) Your code
2) Your serial output
3) Your file

For some reason, you don't want to share the contents of your file, so, all I can do is wish you good luck.

I could compile and link the code and upload it to one of my Arduinos, but without the file, I can't possibly make it do anything.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 74
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok, this is the whole code. it is all run on the setup as I told you before. As for the file it is just a bunch of numbers separated by commas.
Quote
156 , 28 , 156 , 28 , 99 , 156 , 28 , 99 , 156 , 28 , 98 , 156 , 28 , 98 , 156 , 28 , 99 , 157 , 28 , 99 , 157 , 28 , 99 , 156 , 28 , 99 , 156 , 28 , 98 , 156 , 28 , 0 , 157 , 28 , 99 , 156 , 28 , 98 , 156 , 28 , 98 , 156 , 28 , 156 , 28 , 98 , 156 , 28 , 98 , 156 , 28 , 98 , 156 , 28 , 98 , 156 , 28 , 98 , 156 , 28 , 98 , 157 , 28 , 98 , 156 , 28 , 98 , 156 , 28 , 98 , 155 , 28 , 98 , 156 , 28 , 98 , 156 , 28 , 98 , 156 , 28 , 98 , 156 , 28 , 98 , 156 , 28 , 98 , 156 , 28 , 98 , 156 , 28 , 98 , 156 , 28 , 98 , 156 , 28 , 98 , 156 , 28 , 98 , 156 , 28 , 98 , 27 , 77 , 16 , 30 , 34 , 7 , 70 , -3 , 6 , 75 , -3 , 7 , 76 , -3 , 7 , 75 , -3 , 6 , 75 , -3 , 75 , 36 , -3 , 9 , 76 , -2 , 77 , 35 , -2 , 77 , 35 , -2 , 77 , 34 , -2 , 77 , 33 , -2 , 8 , 34 , -2 , 2 , 33 , -2 , 79 , 33 , 0 , 3 , 33 , -1 , 81 , 33 , -1 , 3 , 33 , -1 , 5 , 33 , -1 , 79 , 33 , -1 , 79 , 33 , -1 , 79 , 34 , -1 , 79 , 38 , -1 , 79 , 23 , -1 , 79 , 60 , 84 , 209 , 74 , 80 , 86 , 72 , -1 , 84 , 63 , -1 , 104 , 60 , -1 , 87 , 61 , -1 , 104 , 61 , -1 , 106 , 57 , -1 , 79 , 0 , -1 , 70 , 9 , -1 , 58 , 0 , 110 , 62 , 28 , 109 , 50 , 27 , 109 , 51 , 0 , 106 , 47 , 26 , 48 , 45 , 0 , 45 , 46 , 22 , 44 , 79 , 23 , 41 , 50 , 98 , 40 , 0 , 95 , 35 , 52 , 93 , 34 , 34 , 7 , 32 , 32 , 4 , 29 , 29 , 5 , 27 , 236 , 5 , 27 , 43 , 85 , 28 , 44 , 4 , 30 , 51 , 3 , 27 , 47 , 2 , 27 , 47 , 6 , 238 , 30 , 3 , 98 , 77 , 5 , 54 , 78 , 24 , 54 , 78 , 23 , 104 , 80 , 23 , 107 , 79 , 21 , 41 , 74 , 19 , 85 , 80 , 18 , 66 , 72 , 64 , 99 , 73 , 52 , 99 , 73 , 50 , 72 , 70 , 221 , 68 , 65 , 44 , 65 , 62 , 40 , 85 , 60 , 42 , 37 , 54 , 37 , 27 , 51 , 29 , 17 , 49 , 29 , 14 , 46 , 33 , 12 , 64 , 147 , 11 , 9 , 145 , 16 , 12 , 145 , 26 , 31 , 144 , 30 , 60 , 92 , 0 , 21 , 13 , 0 , 22 , 9 , 51 , 23 , 9 , 182 , 22 , 8 , 181 , 23 , 8 , 51 , 32 , 7 , 24 , 32 , 7 , 23 , 33 , 8 , 23 , 46 , 11 , 24 , 47 , 6 , 60 , 51 , 7 , 0 , 50 , 8 , 56 , 49 , 0 , 57 , 49 , 57 , 49 , 54 , 10 , 51 , 54 , 11 , 48 , 80 , 8 , 51 , 116 , 13 , 57 , 114 , 10 , 0 , 116 , 52 , 0 , 118 , 49 , 0 , 116 , 37 , 0 , 114 , 23 , 0 , 241 , 24 , 48 , 243 , 24 , 46 , 160 , 26 , 50 , 0 , 45 , 0 , 147 , 29 , 54 , 81 , 28 , 51 , 48 , 29 , 51 , 0 , 27 , 46 , 39 , 27 , 47 , 40 , 24 , 57 , 0 , 14 , 13 , 25 , 11 , 10 , 25 , 12 , 6 , 10 , 12 , 5 , 7 , 13 , 14 , 22 , 33 , 41 , 10 , 19 , 41 , 12 , 19 , 41 , 13 , 19 , 12 , 22 , 19 , 12 , 8 , 19 , 41 , 27 , 18 , 41 , 37 , 17 , 42 , 37 , 18 , 41 , 39 , 18 , 42 , 28 , 18 , 41 , 24 , 33 , 43 , 43 , 105 , 21 , 20 , 107 , 108 , 20 , 104 , 112 , 114 , 110 , 236 , 122 , 130 , 0 , 1 , 109 , 110 , 2 , 106 , 227 , 159 , 109 , 110 , 32 , 108 , 109 , 118 , 105 , 224 , 32 , 100 , 221 , 113 , 33 , 100 , 110 , 118 , 91 , 108 , 117 , 95 , 108 , 115 , 86 , 107 , 108 , 0 , 131 , 0 , 0 , 46 , 90 , 67 , 45 , 90 , 66 , 45 , 90 , 63 , 45 , 89 , 68 , 44 , 88 , 57 , 46 , 87 , 51 , 31 , 78 , 50 , 27 , 76 , 45 , 26 , 73 , 39 , 25 , 72 , 40 , 4 , 0 , 36 , 26 , 67 , 0 , 4 , 67 , 36 , 81 , 33 , 7 , 30 , 31 , 58 , 2 , 29 , 58 , 0 , 29 , 4 , 2 , 29 , 35 , 3 , 30 , 35 , 2 , 33 , 29 , 0 , 33 , 30 , 36 , 64 , 30 , 92 , 54 , 25 , 30 , 49 , 0 , 15 , 56 , 13 , 11 , 108 , 13 , 9 , 56 , 11 , 10 , 54 , 36 , 8 , 53 , 35 , 6 , 52 , 0 , 9 , 50 , 0 , 14 , 50 , 14 , 33 , 51 , 12 , 38 , 52 , 11 , 38 , 105 , 10 , 40 , 103 , 11 , 42 , 104 , 11 , 47 , 10 , 40 , 49 , 10 , 42 , 56 , 9 , 52 , 59 , 10 , 52 , 38 , 42 , 53 , 42 , 43 , 62 , 34 , 48 , 53 , 31 , 40 , 58 , 66 , 46 , 58 , 34 , 35 , 55 , 39 , 35 , 0 , 33 , 31 , 48 , 31 , 28 , 48 , 30 , 24 , 48 , 10 , 21 , 13 , 8 , 24 , 45 , 7 , 23 , 44 , 7 , 23 , 44 , 5 , 23 , 45 , 3 , 26 , 113 , 4 , 19 , 112 , 5 , 23 , 23 , 47 , 58 , 11 , 39 , 58 , 7 , 37 , 33 , 10 , 37 , 18 , 9 , 38 , 15 , 9 , 0 , 16 , 8 , 36 , 18 , 9 , 36 , 18 , 7 , 6 , 34 , 7 , 5 , 33 , 5 , 4 , 33 , 54 , 16 , 88 , 19 , 13 , 111 , 53 , 13 , 55 , 52 , 36 , 55 , 85 , 33 , 54 , 84 , 32 , 53 , 83 , 43 , 53 , 236 , 38 , 53 , 232 , 34 , 56 , 83 , 32 , 0 , 0 , 6 , 48 , 0 , 5 , 84 , 9 , 7 , 85 , 6 , 5 , 44 , 5 , 3 , 83 , 6 , 3 , 82 , 42 , 3 , 0 , 39 , 86 , 43 , 200 , 2 , 43 , 199 , 37 , 43 , 199 , 36 , 42 , 82 , 38 , 42 , 79 , 37 , 40 , 192 , 28 , 48 , 188 , 13 , 46 , 15 , 11 , 42

my serial output I posted on previous posts but here it goes again

Quote
Buffer: [,]value: 0
array2: 0
Buffer: [,]value: 0
array2: 0
Buffer: [,]value: 0
array2: 0
minimum: 0
Buffer: [,]value: 0
array2: 0
Buffer: [,]value: 0
array2: 0
Buffer: [,]value: 0
array2: 0
minimum: 0
Buffer: [,]value: 0
array2: 0
Buffer: [,]value: 0
array2: 0
this is what my serial monitor looks like, if it is not this you want let me know.
Logged

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

You know, I've asked you to use Tools + Auto Format to properly indent your code.

I wrote a sketch that read data from the serial port, and wrote it to the SD card. I pasted part of your data in the send box, and sent it.

Then, I uploaded the sample to read the card, to confirm that the data on the card was correct.

Then, I pasted your code, and got the same results you did. I added some print statements to print the character read, and to print the data as it was added to the array in the else block.

The data is being read correctly, but the else block is never executed. Hmmm. Tools + Auto Format properly indented the code, and shows that the else statement goes with the if(counter > 0) statement, NOT the is(c == ',') statement as your indenting made it appear.

You need to move the else block out to the correct place.

I did that, and commented out the Serial.print() statements I added. The results, with your statements were:
Quote
Initializing SD card...card initialized.
Buffer: [156 ]value: 156
array2: 156
Buffer: [ 28 ]value: 28
array2: 28
Buffer: [ 156]value: 156
array2: 156
minimum: 28
Buffer: [ 28 ]value: 28
array2: 28
Buffer: [ 99 ]value: 99
array2: 99
Buffer: [ 156]value: 156
array2: 156
minimum: 28
Buffer: [ 28 ]value: 28
array2: 28
Buffer: [ 99 ]value: 99
array2: 99
Buffer: [ 156]value: 156
array2: 156
minimum: 28
Buffer: [ 28 ]value: 28
array2: 28
Buffer: [ 98 ]value: 98
array2: 98

The code:
Code:
#include <SD.h>

// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 4;

void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600);

  Serial.print("Initializing 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");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");

  // 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("path.txt");
  const int len = 4;
  char array[len+1], c;
  int counter, counter2;
  int value, i, array2[3];
  ;
  counter=0;
  counter2=0;
  // if the file is available, write to it:
  if (dataFile)
  {
    while (dataFile.available())
    {
      c = dataFile.read();
//      Serial.print("Read a ");
//      Serial.println(c);

      if(c==',')
      {
//        Serial.println("Found a comma...");
//        Serial.print("counter = ");
//        Serial.println(counter);
//        Serial.print("counter2 = ");
//        Serial.println(counter2);

        if (counter >0)
        {
          Serial.print("Buffer: [");
          Serial.print(array);
          Serial.print("]");
          value = atoi(array);
          array2[counter2]=value;
          Serial.print("value: ");
          Serial.println(value);
          Serial.print("array2: ");
          Serial.println(array2[counter2]);
          counter2++;
          counter=0;
          if(counter2==3){
            int minimum= min (array2 [counter2-3], min (array2 [counter2-2], array2 [counter2-1]));
            Serial.print("minimum: ");
            Serial.println(minimum);
            counter2=0;
          }         
        }
      }       
      else
      {
        if (counter<len)
        {
          array[counter++] = c;
          array[counter] = 0;
//          Serial.print("array: [");
//          Serial.print(array);
//          Serial.println("]");
        }
      }
    }
    dataFile.close();
  }
  // if the file isn't open, pop up an error:

}

void loop()
{
}

Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 74
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks that worked! but now I just have another question, I tried to get this program and use the information to drive a car as if I was reading these values from a sensor, although the values read right and the smallest value is found the car just keeps turning to the right. Any idea why this is happening? this is the whole code I used.
Code:
#include <AFMotor.h>
#include <SD.h>

AF_DCMotor motor1(1, MOTOR12_8KHZ);
AF_DCMotor motor2(2, MOTOR12_8KHZ);
AF_DCMotor motor3(3, MOTOR12_1KHZ);
AF_DCMotor motor4(4, MOTOR12_1KHZ);
int i, j;
char *names[]={
  "Right ", "Front ", "Left "};
const int chipSelect = 10;
void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  Serial.print("Initializing 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");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
  motor1.setSpeed(150);         // Set the speed for the motors (255 is the maximum)     
  motor2.setSpeed(150);
  motor3.setSpeed(150);
  motor4.setSpeed(150);
}
void forward(){      // This function moves the wheels forward
  motor1.run(FORWARD);
  motor2.run(FORWARD);
  motor3.run(FORWARD);
  motor4.run(FORWARD);

}
void backward() {      // This function moves the wheels backward
  motor1.run(BACKWARD);
  motor2.run(BACKWARD);
  motor3.run(BACKWARD);
  motor4.run(BACKWARD);
}
void haltMotors()   // This function stops each motor (It is better to stop the motors before changing direction.)
{
  motor1.run(RELEASE);
  motor2.run(RELEASE);
  motor3.run(RELEASE);
  motor4.run(RELEASE);
}
void turnRight(){    // This function turns the robot right.   
  motor1.run(FORWARD);
  motor2.run(BACKWARD);
  motor3.run(BACKWARD);
  motor4.run(FORWARD);
}
void turnLeft(){
  motor1.run(BACKWARD);
  motor2.run(FORWARD);
  motor3.run(FORWARD);
  motor4.run(BACKWARD);
}

int smallest_value (int array[3])
{
  return min (array [0], min (array [1], array [2]));
}
int pos (int array[3]){ // function to find position of the smallest value in the array
  int min_value; 
  int poS, n;
  poS=0;
  min_value=array[0];
  for (n=1; n<3; n++){
    if(array[n]<min_value){
      min_value=array[n];
      poS=n;
    }
  }
  return poS;
}

void loop(){
  File dataFile = SD.open("path.txt");
  const int len = 4;
  char array[len+1], c;
  int counter, counter2;
  int value, i, distance[3], minimum, Position;
  ;
  counter=0;
  counter2=0;
  // if the file is available, write to it:
  if (dataFile)
  {
    while (dataFile.available())
    {
      c = dataFile.read();
      //      Serial.print("Read a ");
      //      Serial.println(c);

      if(c==',')
      {
        //        Serial.println("Found a comma...");
        //        Serial.print("counter = ");
        //        Serial.println(counter);
        //        Serial.print("counter2 = ");
        //        Serial.println(counter2);

        if (counter >0)
        {
          //Serial.print("Buffer: [");
          //Serial.print(array);
          //Serial.print("]");
          value = atoi(array);
          distance[counter2]=value;
          //Serial.print("value: ");
          //Serial.println(value);
          Serial.print("distance: ");
          Serial.println(distance[counter2]);
          counter2++;
          counter=0;
         
          if(counter2==3){
            minimum=smallest_value(distance);
            Position=pos(distance);
            Serial.print ("Smallest value ");
            Serial.println (minimum);
            Serial.print ("position of smallest ");
            Serial.println (Position);
           
            //delay(1000);
            if (minimum==0){
              haltMotors();
            }
            if (minimum>20){
              forward();
            }
            else if (minimum<20 && Position==0){ //if the smallest value is smaller than 10 and the position is 0 (right)
              turnLeft();
            }
            else if (minimum<20 && Position==2){
              turnRight();
            }
            else if(minimum <20 && Position==1){
              haltMotors();
              if (distance[2]<distance[0])
                turnLeft();
              else
                turnRight();
            }
            counter2=0; 
          }         
        }
      }       
      else
      {
        if (counter<len)
        {
          array[counter++] = c;
          array[counter] = 0;
          //          Serial.print("array: [");
          //          Serial.print(array);
          //          Serial.println("]");
        }
      }
    }
    dataFile.close();
  }   


}


Logged

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

Quote
Any idea why this is happening?
Haven't we been down this road? Where is your serial output?

Code:
          if(counter2==3){
            minimum=smallest_value(distance);
            Position=pos(distance);
            Serial.print ("Smallest value ");
            Serial.println (minimum);
            Serial.print ("position of smallest ");
            Serial.println (Position);
Once you know the minimum value, why nor pass that to pos() so that it finds that value in the array?

Code:
            else if (minimum<20 && Position==0){ //if the smallest value is smaller than 10 and the position is 0 (right)
It's so nice to see comments match the code.

Code:
            if (minimum==0){
              haltMotors();
            }
            if (minimum>20){
              forward();
            }
            else if (minimum<20 && Position==0){ //if the smallest value is smaller than 10 and the position is 0 (right)
              turnLeft();
            }
            else if (minimum<20 && Position==2){
              turnRight();
            }
            else if(minimum <20 && Position==1){
              haltMotors();
              if (distance[2]<distance[0])
                turnLeft();
              else
                turnRight();
            }
It's also nice to see that you've learned to add Serial.print() statements to see what is going on.
Logged

Pages: 1 [2] 3   Go Up
Jump to: