Go Down

Topic: HC-Sr04 data logging (Read 88 times) previous topic - next topic

sudacastud

Hello.

I'm using an Arduino Yun board and my plan is using an ultrasonic sensor to read water levels and datalog that data into a Micro SD Card. I also want to make an LCD display to show the distance while the data is being data logged. The issue I have is that the LCD display doesn't show any message or reading.

Anyway, there's the code. I'm using a Yun board, an hc-sr04 sensor and a Robot LCD Keypad shield screen.

Code: [Select]
/*Como usar el módulo Arduino LCD KeyPad Shieldcon el sensor de ultrasonidos HC-SR04Ideado por J. L. Ahedohttp://www.web-robotica.com/Este código es de dominio público,utilizalo y modificalo según tus necesidades*/
#include <FileIO.h>
#include <LiquidCrystal.h> // Iniciamos la librería LiquidCrystal
// Inicializamos la libnrería con los números de los pines
// El módulo Arduino LCD KeyPad Shield
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

int trigPin = 3;   // Elegimos el pin 3 para Trig
int echoPin = 11;  // Elegimos el pin 11 para Echo
long duration, cm; // Variables que utilizaremos


void setup() {
 
lcd.begin(16, 2);// set up the LCD's number of columns and rows:
lcd.print(" ");// Print a message to the LCD.
Bridge.begin();
Serial.begin(9600);
FileSystem.begin();

pinMode(trigPin, OUTPUT);  //Definimos inputs y outputs
pinMode(echoPin, INPUT);

while(!SerialUSB);
SerialUSB.println("Filesystem datalogger\n");
}

void loop() {
 
// El emisor se activa al recibir un impulso HIGH de 10 milisegundos.
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
 
String dataString;
dataString += getTimeStamp();
dataString += " = ";

// La duración es el tiempo, en microsegundos que tarda la señal

duration = pulseIn(echoPin, HIGH);

// Convertimos el tiempo en distancia

cm = float (duration/2) *0.0343;

// Imprimimos el resultado en el módulo Arduino LCD KeyPad Shield


delay(500);



dataString += String(cm);

File dataFile = FileSystem.open("/mnt/sd/datalog.txt", FILE_APPEND);

if (dataFile) {
  dataFile.println(dataString);
  dataFile.close();
  Serial.println(dataString);
  lcd.print("Distancia");

  lcd.setCursor(0, 1);
  lcd.print(cm);

  lcd.setCursor(7, 1);
  lcd.print("cm");
  lcd .clear ();
}
else{
  SerialUSB.println("Fuck you");
  lcd.setCursor(0, 2);
  lcd.print("NO CARD!");
  lcd.setCursor(0, 1);
  lcd.print("FUCK YOU!");
  lcd .clear ();
}
delay(15000);
}

String getTimeStamp() {
  String result;
  Process time;
  time.begin("date");
  time.addParameter("+%D-%T");
  time.run();
  while (time.available() > 0) {
    char c = time.read();
    if (c != '\n'){
      result += c;
      }
    }
  return result;
}


I hope i could provide enough info, and also hope to answer as soon as possible. This thing is for my university title and I must expose progress in this thing soon.

Cheers

AWOL

Before you reached the stage of the code you just posted, you wrote a simple LCD test sketch.
How did that go?
What have you done differently?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

Go Up