danim

ArduinoJavaBeta1.1

Jul 26th, 2014
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.91 KB | None | 0 0
  1. #include <SD.h>
  2.  
  3. File myFile;
  4. int recebido = 0; //variável que receberá os dados da porta serial
  5. //int led = 2;
  6.  
  7. void setup(){
  8.   Serial.begin(9600);//frequência da porta serial
  9.    Serial.print("Initializing SD card...");
  10.  // pinMode(led, OUTPUT);
  11.    
  12.   if (!SD.begin(4)) {
  13.         Serial.println("initialization failed!");
  14.         return;
  15.   }
  16.   Serial.println("initialization done.");
  17.   myFile = SD.open("test.txt", FILE_WRITE);
  18.   // if the file opened okay, write to it:
  19.   if (myFile) {
  20.     myFile.println("26/07/2014 TESTE 1");
  21.     myFile.close();
  22.   }
  23. }
  24.  
  25. void loop(){
  26.   myFile = SD.open("test.txt", FILE_WRITE);
  27.   // if the file opened okay, write to it:
  28.   if (myFile) {
  29.     Serial.print("O resultado do sensor: ");
  30.     Serial.println(analogRead(A0));
  31.    // digitalWrite(led,HIGH);
  32.     myFile.println(analogRead(A0));
  33.     // close the file:
  34.     myFile.close();
  35.   } else {
  36.     // if the file didn't open, print an error:
  37.     Serial.println("error opening test.txt");
  38.   }
  39.    
  40. if(Serial.available() > 0) {
  41.     recebido = Serial.read();
  42.     if(recebido == 'a') {
  43.           SD.remove ("test.txt");
  44.           if (!SD.exists ("test.txt") ){
  45.               Serial.println ("Arquivo apagado com sucesso");
  46.           }else{
  47.               Serial.println ("Erro ao apagar arquivo!");
  48.           }
  49.     }
  50.     if(recebido == 'b'){
  51.       myFile = SD.open("test.txt");
  52.       if (myFile) {
  53.             Serial.println("test.txt:");            
  54.             // read from the file until there's nothing else in it:
  55.             while (myFile.available()) {
  56.                     Serial.write(myFile.read());
  57.                 }
  58.             // close the file:
  59.             myFile.close();
  60.           } else {
  61.               // if the file didn't open, print an error:
  62.               Serial.println("error opening test.txt");
  63.             }
  64.       }//FECHA IF PRINCIPAL
  65.   }
  66.  // digitalWrite(led,LOW);
  67.  delay(30000);
  68. }
Advertisement
Add Comment
Please, Sign In to add comment