Advertisement
andrewmovic

sdcard

Nov 12th, 2012
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.57 KB | None | 0 0
  1. #include <SD.h>
  2. #include <SPI.h>
  3. #include <Ethernet.h>
  4. int i = 0;
  5. int j = 0;                              // array pointer:
  6. char chtr;                                  // variable to hold value from file:
  7. char name[4];                                   // array for name data:
  8. char value[8];
  9.  
  10. byte     mac[]   = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
  11. byte     ip[]    = {10, 0, 0, 59};
  12. // set up variables using the SD utility library functions:
  13. Sd2Card card;
  14. int SD_CS = 4;  
  15.  
  16.  
  17. void setup() {
  18.    
  19.     Serial.begin(9600);
  20.    
  21.     // disable w5100 SPI while setting up SD
  22.    
  23.     digitalWrite(10,HIGH);  // Disabled
  24.    
  25.     Serial.print("Starting SD...");
  26.     pinMode(10,OUTPUT);
  27.     if(!SD.begin(4)) Serial.println("SD failed to start");
  28.     else Serial.println("ok");
  29.     // SD.begin() returns with its SPI disabled. Good SD library!
  30.     if (!card.init(SPI_HALF_SPEED, SD_CS)) {
  31.         Serial.println("Initialization Failed.");
  32.        
  33.     } else {
  34.         Serial.println("Wiring is correct and a card is present.");
  35.     }
  36.    
  37.    
  38.     delay (2000);
  39.    
  40.     Serial.print("Starting w5100...");
  41.     Serial.println();
  42.    
  43.     readFile();
  44.    
  45.     Ethernet.begin(mac, ip);
  46.     Serial.println();
  47.     Serial.print("Arduino set IP to ");
  48.     Serial.println();
  49.     Serial.println(Ethernet.localIP());
  50.    
  51.    
  52.     // rest of your setup
  53. }
  54.  
  55. void loop(){}
  56.  
  57.  
  58. void readFile() {
  59.    
  60.     File file;
  61.     file=SD.open("params.txt");
  62.     if (file) {
  63.         // array for value data:
  64.         while (file.available()) {              // read the settings file:
  65.             i = 0;
  66.             boolean paramEnd = false;          
  67.             while (!paramEnd) {             // while not at the end of the parameter keep reading:
  68.                 chtr = file.read();
  69.                 if (chtr != ':') {          // if the char from file is not a':' read it in:
  70.                     name[i] = chtr;
  71.                     i++;
  72.                 } else {
  73.                     paramEnd = true;                   // otherwise we have come to the end of our parmeter name:
  74.                 }
  75.             }
  76.             j = 0; 
  77.             boolean valueEnd = false;
  78.             value[0] = '\0';                               // reset our array pointer:
  79.             while (!valueEnd) {
  80.                 chtr = file.read();
  81.                 if (chtr != ',') {          // if the char from file is not a ',' read it in:
  82.                     value[j] = chtr;
  83.                     j++;
  84.                 } else {
  85.                     valueEnd = true;        // otherwise we have come to the end of our value:
  86.                 }
  87.             }
  88.             if (!strcmp(name, "ip0")) {
  89.                 ip[0] = atoi(value);                     // assign value to ip0 first ip octet:
  90.             }
  91.             if (!strcmp(name, "ip1")) {
  92.                 ip[1] = atoi(value);                     // assign value to ip1 first ip octet:
  93.                
  94.             }
  95.             if (!strcmp(name, "ip2")) {
  96.                 ip[2] = atoi(value);                     // assign value to ip2 first ip octet:
  97.                
  98.             }
  99.             if (!strcmp(name, "ip3")) {
  100.                 ip[3] = atoi(value);                     // assign value to ip4 first ip octet:
  101.                
  102.             }
  103.         }
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement