Components and supplies
1
Arduino Mega 2560
1
Breadboard (generic)
2
Flash Memory Card, MicroSD Card
2
Memory Socket, SD Card
1
Jumper wires (generic)
Project description
Code
Untitled file
arduino
1// script to deminstrate 2 x 16 LCD and 2 x SD card. 2 3#include <SPI.h> // required by SD 4#include <SD.h> // required for SD card 5 6#define SDSS1pin 53 // on the UNO the Sparkfun SDSS pin is 8 !!! 7#define SDSS2pin 13 // on the UNO the Sparkfun SDSS pin is 8 !!! 8 9File anyFile; // for logging day records 10File entry; 11File root; 12 13String fileName = "anyFile.txt"; 14 15unsigned long readPosition; 16unsigned long byteCount; 17 18byte byteBuffer[4096]; 19 20void setup() 21{ 22 pinMode(SDSS1pin, OUTPUT); // set SS pin to output (required they say) 23 pinMode(SDSS2pin, OUTPUT); // set SS pin to output (required they say) 24 digitalWrite(SDSS1pin, HIGH); 25 digitalWrite(SDSS2pin, HIGH); 26 27 Serial.begin(9600); // init seral for serialmonitor 28 Serial.println("Starting"); 29 30 31 if (!SD.begin(SDSS1pin)) // start the SD stuff with 53 as SS pin 32 { 33 Serial.println("SD.Begin() SD 1 Failed"); 34 delay(2000); 35 } 36 Serial.println("\ \ 37SD1 Begin OK."); 38 Serial.println("/r/n Files on SD2 are: "); 39 File root = SD.open("/"); 40 printDirectory(root); // list files on SD1 41 root.close(); 42 digitalWrite(SDSS1pin, HIGH); // disable SD1 43 44 if (!SD.begin(SDSS2pin)) // start the SD stuff with 53 as SS pin 45 { 46 Serial.println("SD.Begin() SD 2 Failed"); 47 delay(2000); 48 } 49 Serial.println("\ \ 50SD2 Begin OK."); 51 delay(1000); 52 53 Serial.println("/r/n Files on SD2 are: "); 54 root = SD.open("/"); 55 printDirectory(root); // list files on SD2 56 SD.remove(fileName); // remove the file on SD2 in case it exists 57 Serial.println("/r/n Files on SD2 now are: "); 58 root = SD.open("/"); 59 printDirectory(root); // list files on SD2 again 60 delay(2000); 61 62 Serial.println("\ \ 63\ \ 64Open ANYFILE.TXT on SD1 and copy to SD2"); 65 66 digitalWrite(SDSS1pin, HIGH); // disable SD1 67 digitalWrite(SDSS2pin, HIGH); // disable SD2 68 readPosition = 0; 69}; 70 71// ============================================ MAIN LOOP ==================================== 72void loop() 73{ 74 if (!SD.begin(SDSS1pin)) // start the SD stuff with 53 as SS pin 75 { 76 Serial.println("SD.Begin() SD 1 Failed"); 77 delay(2000); 78 } 79 80 anyFile = SD.open(fileName, FILE_READ); // Open for read 81 while (!anyFile) 82 { 83 Serial.println("Opening of the sourcefile Failed"); 84 delay(2000); 85 } 86 if (readPosition == anyFile.size()) 87 { 88 Serial.print("Copied bytes: "); 89 Serial.println(anyFile.size()); 90 while (1) delay(10); 91 } 92 93 anyFile.seek(readPosition); 94 byteCount = 0; 95 while (anyFile.available() && byteCount < 4096) 96 { 97 byteBuffer[byteCount] = anyFile.read(); 98 byteCount++; 99 readPosition++; 100 } 101 anyFile.close(); 102 digitalWrite(SDSS1pin, HIGH); // disable SD1 103 104 105 if (!SD.begin(SDSS2pin)) // start the SD stuff with 53 as SS pin 106 { 107 Serial.println("SD.Begin() SD 2 Failed"); 108 delay(2000); 109 } 110 111 anyFile = SD.open(fileName, FILE_WRITE); // Open for write 112 anyFile.write(byteBuffer, byteCount); 113 anyFile.close(); 114 digitalWrite(SDSS2pin, HIGH); // disable SD2 115}; 116 117 118 119// ================================ List files and sizes on SD card ========================== 120// ------------------------------------------------------------------------------------------- 121void printDirectory(File dir) // lists the files and filesize on the SD card (only root) 122{ 123 while (true) 124 { 125 File entry = dir.openNextFile(); 126 if (! entry) // no more files 127 { 128 break; 129 } 130 Serial.print(entry.name()); 131 Serial.print(" "); 132 Serial.println(entry.size(), DEC); 133 entry.close(); 134 } 135}; 136
Untitled file
arduino
1// script to deminstrate 2 x 16 LCD and 2 x SD card. 2 3#include <SPI.h> 4 // required by SD 5#include <SD.h> // 6 required for SD card 7 8#define SDSS1pin 53 // on the 9 UNO the Sparkfun SDSS pin is 8 !!! 10#define SDSS2pin 13 // 11 on the UNO the Sparkfun SDSS pin is 8 !!! 12 13File anyFile; // 14 for logging day records 15File entry; 16File root; 17 18String fileName = "anyFile.txt"; 19 20unsigned 21 long readPosition; 22unsigned long byteCount; 23 24byte byteBuffer[4096]; 25 26void 27 setup() 28{ 29 pinMode(SDSS1pin, OUTPUT); // set SS pin to output 30 (required they say) 31 pinMode(SDSS2pin, OUTPUT); // set SS pin 32 to output (required they say) 33 digitalWrite(SDSS1pin, HIGH); 34 digitalWrite(SDSS2pin, 35 HIGH); 36 37 Serial.begin(9600); // init seral for serialmonitor 38 39 Serial.println("Starting"); 40 41 42 if (!SD.begin(SDSS1pin)) // 43 start the SD stuff with 53 as SS pin 44 { 45 Serial.println("SD.Begin() SD 46 1 Failed"); 47 delay(2000); 48 } 49 Serial.println("\ \ 50SD1 Begin OK."); 51 52 Serial.println("/r/n Files on SD2 are: "); 53 File root = SD.open("/"); 54 55 printDirectory(root); // list files on SD1 56 root.close(); 57 58 digitalWrite(SDSS1pin, HIGH); // disable SD1 59 60 if (!SD.begin(SDSS2pin)) 61 // start the SD stuff with 53 as SS pin 62 { 63 Serial.println("SD.Begin() 64 SD 2 Failed"); 65 delay(2000); 66 } 67 Serial.println("\ \ 68SD2 Begin 69 OK."); 70 delay(1000); 71 72 Serial.println("/r/n Files on SD2 are: "); 73 74 root = SD.open("/"); 75 printDirectory(root); // 76 list files on SD2 77 SD.remove(fileName); // remove 78 the file on SD2 in case it exists 79 Serial.println("/r/n Files on SD2 now are: 80 "); 81 root = SD.open("/"); 82 printDirectory(root); // 83 list files on SD2 again 84 delay(2000); 85 86 Serial.println("\ \ 87\ \ 88Open 89 ANYFILE.TXT on SD1 and copy to SD2"); 90 91 digitalWrite(SDSS1pin, HIGH); // 92 disable SD1 93 digitalWrite(SDSS2pin, HIGH); // disable SD2 94 95 readPosition = 0; 96}; 97 98// ============================================ 99 MAIN LOOP ==================================== 100void loop() 101{ 102 if (!SD.begin(SDSS1pin)) 103 // start the SD stuff with 53 as SS pin 104 { 105 Serial.println("SD.Begin() 106 SD 1 Failed"); 107 delay(2000); 108 } 109 110 anyFile = SD.open(fileName, 111 FILE_READ); // Open for read 112 while (!anyFile) 113 { 114 Serial.println("Opening 115 of the sourcefile Failed"); 116 delay(2000); 117 } 118 if (readPosition == 119 anyFile.size()) 120 { 121 Serial.print("Copied bytes: "); 122 Serial.println(anyFile.size()); 123 124 while (1) delay(10); 125 } 126 127 anyFile.seek(readPosition); 128 byteCount 129 = 0; 130 while (anyFile.available() && byteCount < 4096) 131 { 132 byteBuffer[byteCount] 133 = anyFile.read(); 134 byteCount++; 135 readPosition++; 136 } 137 anyFile.close(); 138 139 digitalWrite(SDSS1pin, HIGH); // disable SD1 140 141 142 if (!SD.begin(SDSS2pin)) 143 // start the SD stuff with 53 as SS pin 144 { 145 Serial.println("SD.Begin() 146 SD 2 Failed"); 147 delay(2000); 148 } 149 150 anyFile = SD.open(fileName, 151 FILE_WRITE); // Open for write 152 anyFile.write(byteBuffer, byteCount); 153 154 anyFile.close(); 155 digitalWrite(SDSS2pin, HIGH); // disable 156 SD2 157}; 158 159 160 161// ================================ List files and sizes 162 on SD card ========================== 163// ------------------------------------------------------------------------------------------- 164void 165 printDirectory(File dir) // lists the files and filesize on the SD 166 card (only root) 167{ 168 while (true) 169 { 170 File entry = dir.openNextFile(); 171 172 if (! entry) // no more files 173 { 174 break; 175 176 } 177 Serial.print(entry.name()); 178 Serial.print(" "); 179 Serial.println(entry.size(), 180 DEC); 181 entry.close(); 182 } 183}; 184
Downloadable files
Mega with 2 x SD
Mega with 2 x SD

Comments
Only logged in users can leave comments