这几天我一直在忙这件事,而且我每次都空着。我试图创建一个简单的临时记录设备。
我有一个使用串行监视器创建和写入文件的工作代码,这很好用,但是当我将它与任何其他设备接触时(我使用开关来导航 LCD 显示器,它在写入部分失败)
该代码将使用我选择的名称创建一个文件,但不会写入该文件这是我的问题
这是我的代码。
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
//A4 -- SDA
//A5 -- SCL
// 2 -- DS ///// Not needed really I dont think
// It is assumed that the LCD module is connected to
// the following pins using a levelshifter to get the
// correct voltage to the module.
// SCK - Pin 0
// MOSI - Pin 1
// DC - Pin 2
// RST - Pin 3
// CS - Pin 5
//
/*
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 10
*/
#include <LCD5110_Graph.h>
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
LCD5110 myGLCD(0,1,2,3,5);
RTC_DS1307 RTC;
extern uint8_t SmallFont[];
extern uint8_t TinyFont[];
int up=6,down=8,enter=4,right=7,left=9; ////worked out new ones
File root;
char fileName[12];
void setup()
{
Wire.begin();
RTC.begin();
myGLCD.InitLCD();
myGLCD.clrScr();
myGLCD.setFont(SmallFont);
while (! RTC.isrunning())
{
myGLCD.print("RTC NOT running!",CENTER,18);
myGLCD.update();
Wire.begin();
RTC.begin();
}
myGLCD.print("RTC NOW running!",CENTER,18);
myGLCD.update();
delay(1000);
myGLCD.clrScr();
myGLCD.print("Start",CENTER,18);
myGLCD.update();
delay(1000);
pinMode(10, OUTPUT);
if (!SD.begin(10))
{
myGLCD.print("FAILED!",CENTER,18);
myGLCD.update();
delay(1000);
}
else
{
myGLCD.print("DONE",CENTER,18);
myGLCD.update();
delay(1000);
}
pinMode(up,INPUT);
pinMode(down,INPUT);
pinMode(enter,INPUT);
pinMode(left,INPUT);
pinMode(right,INPUT);
pinMode (A0,INPUT);
}
void loop()
{
myGLCD.print("Names now",CENTER,18);
myGLCD.update();
delay(1000);
menu();
float Temp
root = SD.open(fileName,FILE_WRITE);
DateTime now = RTC.now();
if(root) {
for(int p=0;p<5;p++)
{
myGLCD.print("DONE 2",CENTER,18);
myGLCD.update();
delay(1000);
/////////// Code to get the current temp
Temp = log(((34611200/analogRead(A0)) - 33800));
Temp = 1 / (0.000815883 + (0.000222435 * Temp) + (0.000000104 * Temp * Temp * Temp));
Temp = Temp - 273.15; // Convert Kelvin to Celcius
myGLCD.clrScr();
myGLCD.print("Temp",CENTER,18);
myGLCD.update();
pinMode(10,OUTPUT);
root.print(Temp);
root.print(" ---- ");
root.print(now.hour());
root.print(":");
root.print(now.minute());
root.print(" ");
root.print(now.day());
root.print("/");
root.print(now.month());
root.print("/");
root.println(now.year());
delay(2000);
}
root.close();
}
else {
myGLCD.print("Failed",CENTER,18);
myGLCD.update();
delay(1000);
}
root.close();
}
void menu()
{
//////////// menu to select what you want to do (create a file)
/// then goes to picker to select name
}
void picker()
{
//// code to put a selection for a file name on the LCD USER selects this file name after that
////////// .txt is added to the name
}
任何帮助将不胜感激。非常感谢 :)