Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <LiquidCrystal.h>
- #include <SD.h>
- LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
- int buttons;
- File myFile;
- //name of the file
- char nombrefichero[12];
- int i = 1;
- int exist = LOW ;
- void setup() {
- Serial.begin(9600);
- //check the sd card adapter
- Serial.print("Iniciando SD ...");
- if (!SD.begin(53)) {
- Serial.println("No se pudo inicializar");
- return;
- }
- Serial.println("inicializacion exitosa");
- }
- void loop() {
- // use the buttons of the lcd keypand shield in analog 0
- buttons = analogRead(0);
- //button right
- if(buttons < 50){
- count ++;
- nuevo();
- }
- //button select
- if(buttons < 790){
- capturar();
- }
- }
- void capturar(){
- myFile = SD.open(nombrefichero, FILE_WRITE);//abrimos el archivo
- if (myFile) {
- Serial.print("Escribiendo SD: ");
- int sensor1 = analogRead(0);
- int sensor2 = analogRead(1);
- int sensor3 = analogRead(2);
- myFile.print(millis());
- myFile.print(",");
- myFile.print(sensor1);
- myFile.print(",");
- myFile.print(sensor2);
- myFile.print(",");
- myFile.println(sensor3);
- myFile.close(); //cerramos el archivo
- Serial.print("Tiempo(ms)=");
- Serial.print(millis());
- Serial.print(",sensor1=");
- Serial.print(sensor1);
- Serial.print(",sensor2=");
- Serial.print(sensor2);
- Serial.print(",sensor3=");
- Serial.println(sensor3);
- } else {
- // if the file didn't open, print an error:
- Serial.println("Error al abrir el archivo");
- }
- delay(1000);
- }
- void nuevo(){
- while (exist == LOW)
- {
- sprintf(nombrefichero, "M%d.txt",i); //generate autoincrement number of
- file
- if(SD.exists(nombrefichero))
- {
- i = i+1;
- myFile = SD.open(nombrefichero, FILE_WRITE);
- if (myFile) {
- Serial.println("Archivo nuevo, Escribiendo encabezado(fila 1)");
- myFile.println("Tiempo(ms),Sensor1,Sensor2,Sensor3");
- myFile.close();
- } else {
- exist = HIGH;
- Serial.println("Error creando el archivo");
- }
- }
- }
- }
- void nuevo(){
- while (exist == LOW)
- {
- sprintf(nombrefichero, "M%d.txt",i);
- if(SD.exists(nombrefichero))
- {
- i = i+1;
- myFile = SD.open(nombrefichero, FILE_WRITE);
- if (myFile) {
- Serial.println("Archivo nuevo, Escribiendo encabezado(fila 1)");
- myFile.println("Tiempo(ms),Sensor1,Sensor2,Sensor3");
- myFile.close();
- } else {
- exist = HIGH;
- Serial.println("Error creando el archivo");
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment