Advertisement
uas_arduino

Arduino - SPI Write/Read

Apr 17th, 2015
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 KB | None | 0 0
  1. #include <SD.h>
  2.  
  3. void setup(){
  4.   Serial.begin(9600);
  5.   pinMode(10, OUTPUT);
  6.   SD.begin(4);
  7.  
  8.   SD.remove("myFile.bin");
  9.  
  10.   File myFile = SD.open("myFile.bin", FILE_WRITE);
  11.   if(! myFile){
  12.     Serial.println("Failed to open file for writing");
  13.     return;
  14.   }
  15.  
  16.   Serial.println("Starting Sampling");
  17.   for(short a = 0; a < 256; a++){
  18.     short samp = analogRead(0);
  19.     myFile.write((byte)(samp & 0xff));
  20.     myFile.write((byte)((samp >> 8) & 0xff));
  21.     delay(10);
  22.   }
  23.   Serial.println("End of Sampling");
  24.   myFile.close();
  25.  
  26.   myFile = SD.open("myFile.bin", FILE_READ);
  27.   if(! myFile){
  28.     Serial.println("Could not open file for reading");
  29.     return;
  30.   }
  31.  
  32.   for(short a = 0; a < 256; a++){
  33.     short samp = (myFile.read() & 0xff) | ((myFile.read() << 8) & 0xff00);
  34.     Serial.print("Read back ");
  35.     Serial.println(samp);
  36.   }
  37.  
  38.   myFile.close();
  39. }
  40.  
  41. void loop(){
  42.  
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement