Hi I have a potameter and a pressure sensor hooked up to an Arduino Uno with a SD card datalogger
The problem is that values for the second sensor are different on the LCD display than both the datalogger and serial output.
Whatsmore, when i play around with the position of the sensor value on the LCD it seems to effect the values.
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 RTC;
#include <LiquidCrystal.h>
// how many milliseconds between grabbing data and logging it. 1000 ms is once a second
#define LOG_INTERVAL 1000 // mills between entries (reduce to take more/faster data)
#define ECHO_TO_SERIAL 1 // echo data to serial port
// select 10 for adafruit shield
const int chipSelect = 10;
// create file to log to
File logfile;
LiquidCrystal lcd(9,8,5,4,3,2);
int potPin1 = A1;
int potPin2 = A2;
void setup() {
Serial.begin(9600);
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);
pinMode(13, OUTPUT);
Wire.begin(); // Start the I2C
RTC.begin(); // Init RTC
RTC.adjust(DateTime(DATE, TIME));
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
// create a new file
char filename[] = "LOGGER00.CSV";
for (uint8_t i = 0; i < 100; i++) {
filename[6] = i/10 + '0';
filename[7] = i%10 + '0';
if (! SD.exists(filename)) {
// only open a new file if it doesn't exist
logfile = SD.open(filename, FILE_WRITE);
break; // leave the loop!
}
}
if (! logfile) {
Serial.println("could not create file");
}
Serial.print("Logging to: ");
Serial.println(filename);
// connect to RTC
Wire.begin();
if (!RTC.begin()) {
logfile.println("RTC failed");
#if ECHO_TO_SERIAL
Serial.println("RTC failed");
#endif //ECHO_TO_SERIAL
}
logfile.println("s1");
#if ECHO_TO_SERIAL
// Serial.println("s1");
//Serial.println("s2");
#endif //ECHO_TO_SERIAL
pinMode(potPin1, INPUT);
pinMode(potPin2, INPUT);
lcd.begin(16, 2);
lcd.clear();
}
void loop() {
digitalWrite(13, HIGH); // turn the LED on
delay(250); // wait for a 1/4second
digitalWrite(13, LOW); // turn the LED off
delay(250); // wait for a 1/4second
DateTime now;
delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL));
String dataString = "";
// fetch the time
now = RTC.now();
// log time
logfile.print(now.unixtime()); // seconds since 1/1/1970
logfile.print(",");
if (now.month() < 10) {
logfile.print(0, DEC);
}
logfile.print(now.month(), DEC);
logfile.print("/");
if (now.day() < 10) {
logfile.print(0, DEC);
}
logfile.print(now.day(), DEC);
logfile.print("/");
logfile.print(now.year(), DEC);
logfile.print(" ");
logfile.print(",");
if (now.hour() < 10) {
logfile.print(0, DEC);
logfile.print(",");
}
logfile.print(now.hour(), DEC);
logfile.print(":");
if (now.minute() < 10) {
logfile.print(0, DEC);
}
logfile.print(now.minute(), DEC);
logfile.print(":");
if (now.second() < 10) {
logfile.print(0, DEC);
}
logfile.print(now.second(), DEC);
#if ECHO_TO_SERIAL
Serial.print(now.unixtime()); // seconds since 1/1/1970
Serial.print(",");
if (now.month() < 10) {
Serial.print(0, DEC);
}
Serial.print(now.month(), DEC);
Serial.print("/");
if (now.day() < 10) {
Serial.print(0, DEC);
}
Serial.print(now.day(), DEC);
Serial.print("/");
Serial.print(now.year(), DEC);
Serial.print(" ");
if (now.hour() < 10) {
Serial.print(0, DEC);
}
Serial.print(now.hour(), DEC);
Serial.print(":");
if (now.minute() < 10) {
Serial.print(0, DEC);
}
Serial.print(now.minute(), DEC);
Serial.print(":");
if (now.second() < 10) {
Serial.print(0, DEC);
}
Serial.print(now.second(), DEC);
logfile.print(",");
Serial.print("\t S1: "); // Prints Sensor Val: to serial
Serial.print(",");
Serial.print(analogRead(potPin1)); // Prints value on Potpin1 to serial
delay(10);
Serial.print(",");
Serial.print("\t S2: "); // Prints Sensor Val: to serial
Serial.print(",");
Serial.print(analogRead(potPin2)); // Prints value on Potpin1 to serial
delay(10);
//Serial.println();// Prints on next line
#endif //ECHO_TO_SERIAL
logfile.print("Sensor 1: "); // Prints Sensor Val: to log
logfile.print(",");
logfile.print(analogRead(potPin1)); // Prints value on Potpin1 to log
delay(10);
logfile.print(",");
logfile.print("Sensor 2: "); // Prints Sensor Val: to log
logfile.print(",");
logfile.print(analogRead(potPin2)); // Prints value on Potpin1 to log
delay(10);
logfile.println();
#if ECHO_TO_SERIAL
Serial.println();
#endif // ECHO_TO_SERIAL
// flush to file
logfile.flush();
lcd.setCursor(0,0); // Sets the cursor to col 0 and row 0
lcd.print("Sensor 1: "); // Prints Sensor Val: to LCD
lcd.print(analogRead(potPin1)); // Prints value on Potpin1 to LCD
delay(10);
lcd.setCursor(0,1); // Sets the cursor to col 1 and row 0
lcd.print("Sensor 2: "); // Prints Sensor Val: to LCD
lcd.print(analogRead(potPin2)); // Prints value on Potpin1 to LCD
delay(10);
delay(1000);
}