1602液晶屏显示问题
功能:利用SD模块和液晶屏模块,采集环境光照强度,存储在SD卡中,并显示在1602液晶屏上。现状:液晶屏能够显示Initializing Done,很清晰,但却不能显示每秒钟采集的光照强度,为什么呐?
/*
SD card datalogger
This example shows how to log data from three analog sensors
to an SD card using the SD library.
The circuit:
* analog sensors on analog ins 0, 1, and 2
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4
created24 Nov 2010
modified 9 Apr 2012
by Tom Igoe
This example code is in the public domain.
*/
#include <SPI.h>
#include <SD.h>
#include<LiquidCrystal.h>
LiquidCrystal lcd(5,6,7,8,9,10);
const int chipSelect = 4;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
lcd.begin(16,2);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
lcd.print("Initializing");
lcd.setCursor(0,1);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
lcd.print("Done");
delay(1000);
lcd.clear();
}
void loop() {
// make a string for assembling the data to log:
String dataString = "";
// read three sensors and append to the string:
int sensor = analogRead(A0);
delay(1000);
dataString += String(sensor);
lcd.setCursor(0,0);
lcd.print(sensor,DEC);
lcd.setCursor(0,1);
lcd.print("OK");
lcd.clear();
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("data1.xls", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.xls");
}
}
写文件要用时间啊。。。你电脑内存几个G,写个文件几毫秒,你不看看arduino内存多大。。。。 看看我写的这个,显示正常,存储正常,可以参考看看
#include <LiquidCrystal.h> //LCD显示库 1602LCD屏使用
#include <Wire.h> //i2c总线库 1307时钟使用 占用A4A5
#include <RTClib.h> //RTC时钟芯片库1307时钟使用
#include <DHT.h> //DHT11 DHT22 通用库
#include <SPI.h> //SPI总线库 SD卡使用
#include <SD.h> //SD卡库 SD卡使用
DHT dht;
RTC_DS1307 rtc;
int a=0;
const int chipSelect = 4; //SD卡使用到引脚4
LiquidCrystal lcd(10,9,8,7,6,5); //LCD使用到引脚10,9,8,7,6,5
void setup() {
Serial.begin(9600); //串口引脚波特率9600
Wire.begin(); //i2c总线启用
rtc.begin(); //RTC时钟启用
dht.setup(2); // data pin 2 //DHT使用到引脚2
pinMode(13, OUTPUT);
lcd.begin(16,2);
lcd.print("hello,World!");
Serial.print("Initializing SD card...");
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
return;
}
Serial.println("card initialized.");
}
void loop() {
delay(3000);
float humidity = dht.getHumidity();
float temperature = dht.getTemperature();
String dataString = "";
a= a+1;
DateTime now = rtc.now();
dataString +=String(a)+",";
dataString += String(now.year(), DEC)+"-"+String(now.month(), DEC)+"-"+String(now.day(), DEC)+" ";
dataString += String(now.hour(), DEC)+":"+String(now.minute(), DEC)+":"+String(now.second(), DEC);
dataString +=","+String(dht.getHumidity())+","+String(dht.getTemperature());
lcd.setCursor(0,1);
lcd.print(String(dht.getHumidity())+","+String(dht.getTemperature()));
digitalWrite(13, HIGH);
File dataFile = SD.open("datalog.csv", FILE_WRITE);
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
Serial.println(dataString);
digitalWrite(13, LOW);
}
else {
Serial.println("error opening datalog.txt");
digitalWrite(13, HIGH);
}
}
sxyx029 发表于 2016-6-28 10:38 static/image/common/back.gif
看看我写的这个,显示正常,存储正常,可以参考看看
#include //LCD显示库 1602LCD屏使用
谢谢指教,后来搞明白了。两个原因,1是IDE版本升级之后,1602液晶显示屏只能显示第一个字符,后来下载了一个低版本的IDE后,就能够正常显示了;2是没有给液晶屏显示字符设置停留时间,导致字符很快就消失了,后来增加了一句delay,就解决了。 zjz5717 发表于 2016-6-27 23:00 static/image/common/back.gif
写文件要用时间啊。。。你电脑内存几个G,写个文件几毫秒,你不看看arduino内存多大。。。。
给显示屏增加了一句delay,效果好多了
页:
[1]