LusoRobótica - Robótica em Português

Sistemas específicos => Arduino / AVR => Tópico iniciado por: brunomartins em 23 de Maio de 2009, 23:59

Título: Gravar TXT
Enviado por: brunomartins em 23 de Maio de 2009, 23:59
Pessoal, alguém tem alguma dica de como gravar em um arquivo TXT usando arduino?

Achei esse codigo na internet, mas não estou conseguino compilar, e não estou conseguindo resolver o erro..

Código: [Seleccione]
/**
 * Analog In
 *
 * Reads a value from the serial port and sets the background color.
 * Running this example requires you have a BX-24 microcontroller
 * and peripheral hardware. More information can be found on the tutorial
 * pages of Tom Igoe: http://stage.itp.nyu.edu/~tigoe/pcomp/examples.shtml
 * Because this program uses the serial port, it will not work within a web browser.
 *
 * Temperature collecting and writing to a text file
 */
 
import processing.serial.*;

String buff = "";
int val = 0;
int NEWLINE = 10;
int i = 0 ;
PFont font;
Serial port;


PrintWriter output;

void setup()
{
  size(250, 200);
  background(255, 204, 0);
  font = loadFont("UniversLTStd-Light-48.vlw");
  textFont(font, 32);

  // Print a list in case COM1 doesn't work out
  println("Available serial ports:");
  println(Serial.list());

  //port = new Serial(this, "COM1", 19200);
  // Uses the first available port
  port = new Serial(this, Serial.list()[0], 9600);
  output = createWriter("temperaturas.txt"); // cria arquivo para armazenar as temperaturas
}

void draw()
{
  while (port.available() > 0) {
    serialEvent(port.read());
  }
 
 
}

void keyPressed() { // Press a key to save the data
  output.flush(); // Write the remaining data
  output.close(); // Finish the file
  exit(); // Stop the program
}

void serialEvent(int serial)
{
  // If the variable "serial" is not equal to the value for
  // a new line, add the value to the variable "buff". If the
  // value "serial" is equal to the value for a new line,
  //  save the value of the buffer into the variable "val".
 
   
    println(serial) ;
    val=serial ;
    // Escreve a temperatura e a hora
    //
    output.println(hour() + ":" + minute() + ":" + second() + "," + val);

    //line(i,200,i,200-(val*4)) ; // funciona bem para a linha
    fill(102,0,153) ;
    rect(i,200-(val*4),2,val*4) ; // desenha a barra temperatura
   
    fill(255, 204, 0) ;
    noStroke() ;        // nao desenha a borda do retangulo .
    rect(110,5,50,30) ; // apaga o texto anterior
    fill(102,0,153) ;
    text(val, 125, 30);
    fill(0, 102, 153);

    i+=6 ;             // se chegar ao limite da area de desenho , apaga tudo e recomeça
    if(i>=250) {
      i=0 ;
      background(255, 204, 0);
    }
   
   
}

Título: Re:Gravar TXT
Enviado por: Njay em 24 de Maio de 2009, 00:25
Esse código não é para Arduino, por isso não consegues compilar.

O Arduino em si não consegue gravar ficheiros no PC. A única maneira é haver um programa no PC que recebe dados do Arduino por porta série e os grava num ficheiro. Para isso podes usar o Processing (muito usado com o Arduino) ou qualquer outra linguagem de programação que possa aceder a uma porta série do PC.
Título: Re:Gravar TXT
Enviado por: guibot em 24 de Maio de 2009, 00:37
brunomartins esse código é de Processing (http://www.processing.org).

como o NJay disse com o Processing facilmente lês informação do arduino e gravas TXT's.
Título: Re:Gravar TXT
Enviado por: brunomartins em 25 de Maio de 2009, 13:11
como fazemos então para o Arduino salvar os dados em um cartão SD?
se ele não salva em TXT?

Tenho um sistema que tem que trabalhar independente do PC, ele não vai estar ligado no PC o tempo inteiro... não vou conseguir fazer ele gravar no sd?
Título: Re:Gravar TXT
Enviado por: TigPT em 25 de Maio de 2009, 13:22
Qualquer sistema que grave dados num cartão SD, pode gravar como txt pois um txt é simples ascii.

Tens aqui como gravar em pen usb:
http://www.arduino.cc/playground/Main/UsbMemory (http://www.arduino.cc/playground/Main/UsbMemory)
Título: Re:Gravar TXT
Enviado por: brunomartins em 25 de Maio de 2009, 13:38
TigPT,

Então, se eu colocar o Arduino com o SD ele vai salvar em TXT normalmente?

Não vou precisar do processing no PC?

Uma outra duvida, tem alguma biblioteca que retorna a data e hora atual do sistema, na hora que ele estiver salvando os dados?
Título: Re:Gravar TXT
Enviado por: TigPT em 25 de Maio de 2009, 13:41
Sim, pode gravar para o SD como txt.

Para data, pode utilizar esta biblioteca:
http://www.arduino.cc/playground/Code/DateTime (http://www.arduino.cc/playground/Code/DateTime)
ou um real time clock:
http://lusorobotica.com/index.php/topic,681.0.html (http://lusorobotica.com/index.php/topic,681.0.html)