พี่ครับ ผมอุดหนุนพี่ตลอด ตอนนี้ผมมีปัญหา ผมงมมาหลายวันแล้วอะพี่ คือรันโค๊ด Exam ผ่านนะครับใช้งานได้ปกติ
ทีนี้ผมสงสัยว่าผมวางโค๊ดผิดตำแหน่งหรือเปล่า เลยทำให้ใช้ได้ไม่ได้ คอมไพล์ผ่าน แต่ไม่มีการเก็บข้อมูลไปที่ txt อะครับ ด้านล่างเป็นโค๊ดผมที่ยังไม่ได้ใส่โค๊ด data logger เข้าไป เพราะผมทำจนมั่วไปแล้ว ผมอยากรู้ว่าผมควรนำโค๊ด data logger มาใส่ช่วงไหนบ้างอะครับช่วยชี้ตำแหน่งให้ผมที ผมหัวปั่นมากเลย หาทางออกไม่ได้
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
byte resetButtonA = 11;
byte resetButtonB = 12;
byte statusLed = 13;
byte sensorInterrupt = 0;
byte sensorPin = 2;
float calibrationFactor = 4.5;
volatile byte pulseCount;
float flowRate;
unsigned int flowMilliLitres;
unsigned long totalMilliLitresA;
unsigned long totalMilliLitresB;
unsigned long oldTime;
void setup()
{
lcd.begin();
Serial.begin(9600);
pinMode(statusLed, OUTPUT);
digitalWrite(statusLed, HIGH);
pinMode(resetButtonA, INPUT);
digitalWrite(resetButtonA, HIGH);
pinMode(resetButtonB, INPUT);
digitalWrite(resetButtonB, HIGH);
pinMode(sensorPin, INPUT);
digitalWrite(sensorPin, HIGH);
pulseCount = 0;
flowRate = 0.0;
flowMilliLitres = 0;
totalMilliLitresA = 0;
totalMilliLitresB = 0;
oldTime = 0;
attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
}
void loop()
{
if(digitalRead(resetButtonA) == LOW)
{
totalMilliLitresA = 0;
lcd.setCursor(0, 1);
lcd.print("0L ");
}
if(digitalRead(resetButtonB) == LOW)
{
totalMilliLitresB = 0;
lcd.setCursor(8, 1);
lcd.print("0L ");
}
if( (digitalRead(resetButtonA) == LOW) || (digitalRead(resetButtonB) == LOW) )
{
digitalWrite(statusLed, LOW);
} else {
digitalWrite(statusLed, HIGH);
}
if((millis() - oldTime) > 1000) // Only process counters once per second
{
detachInterrupt(sensorInterrupt);
flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
oldTime = millis();
flowMilliLitres = (flowRate / 60) * 1000;
totalMilliLitresA += flowMilliLitres;
totalMilliLitresB += flowMilliLitres;
unsigned int frac;
Serial.print("L/min ");
Serial.print(int(flowRate));
Serial.print(".");
frac = (flowRate - int(flowRate)) * 10;
Serial.print(frac, DEC) ;
Serial.print(" ");
Serial.print(flowMilliLitres);
Serial.print(" ");
Serial.print(totalMilliLitresA);
Serial.print(" ");
Serial.println(totalMilliLitresB);
lcd.setCursor(0, 0);
lcd.print(" ");
lcd.setCursor(0, 0);
lcd.print("Flow: ");
if(int(flowRate) < 10)
{
lcd.print(" ");
}
lcd.print((int)flowRate);
lcd.print('.');
lcd.print(frac, DEC) ;
lcd.print(" L");
lcd.print("/min");
lcd.setCursor(0, 1);
lcd.print(int(totalMilliLitresA / 1000));
lcd.print("L");
lcd.setCursor(8, 1);
lcd.print(int(totalMilliLitresB / 1000));
lcd.print("L");
pulseCount = 0;
attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
}
}
void pulseCounter()
{
pulseCount++;
}
อันนี้เป็นโค๊ดที่ผมจะเอามาดัดแปลงนะครับ เป็นโค๊ดของ arduinoall นี่แหละครับ
#include <DS1307RTC.h>
#include <Time.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
/*
ต่อขา 5 หรือ SCL ของบอร์ดไปที่ขา 21 ของ Mega
ต่อขา 4 หรือ SDA ของบอร์ดไปที่ขา 20 ของ Mega
*/
File myFile;
const int chipSelect = 10;
String time ;
tmElements_t tm;
void setup() {
Serial.begin(9600);
while (!Serial) ; // wait for serial
delay(200);
Serial.println("ArduinoAll DataLogger Shield Test");
pinMode(SS, OUTPUT);
// if (!SD.begin(chipSelect)) {
if (!SD.begin(10, 11, 12, 13)) {
Serial.println("SD Card initialization failed!");
return;
}
Serial.println("SD Card OK.");
ReadText();
}
void loop() {
time = Now()+" SenSerValue";
Serial.println(time);
WriteText(time);
delay(3000);
}
void ReadText(){
// re-open the file for reading:
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
}
else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void WriteText(String txt){
myFile = SD.open("test.txt", FILE_WRITE);
if (myFile) {
myFile.println(txt);
myFile.close();
}
else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
String Now(){
String time = "";
if (RTC.read(tm)) {
// time = String(tm.Hour+":"+tm.Minute+":"+tm.Secnd+" DAY : "+tm.Day+"/"+tm.Month+"/"+tmYearToCalendar(tm.Year));
time+=tm.Hour;
time+=":";
time+=tm.Minute;
time+=":";
time+=tm.Second;
time+=" DAY : ";
time+=tm.Day;
time+="/";
time+=tm.Month;
time+="/";
time+=tmYearToCalendar(tm.Year);
}
else {
time = "NO";
if (RTC.chipPresent()) {
Serial.println("The DS1307 is stopped. Please run the SetTime");
Serial.println("example to initialize the time and begin running.");
Serial.println();
}
else {
Serial.println("DS1307 read error! Please check the circuitry.");
Serial.println();
}
}
return time;
}
ขอความช่วยเหลือหน่อยนะครับพี่ ผมใช้บอร์ด arduino mega2560
จากโคดผมเห็นมีการใช้ขาซ้ำกับ sd card if (!SD.begin(10, 11, 12, 13))
เปลี่ยนตำแหน่งขานี้เป็นขาอื่นครับ
byte resetButtonA = 11;
byte resetButtonB = 12;
byte statusLed = 13;
ลอง debug ทีละส่วน ทดสอบเฉพาะ data logger ตามหน้าบทความครับ http://www.arduinoall.com/b/16
พี่ครับตอนนี้ได้แล้ว ขอบคุณมากเลยครับ แต่ผมงงอยู่อีกอย่าง คือเวลาผมรันโปรแกรม หรือ รีเซ็ตบอร์ด มันจะรันข้อมูลที่เก็บล็อค มาตั้งแต่เริ่ม ยันล่าสุดเลยอะครับ โปรแกรมถึงจะทำงานต่อได้ มีวิธีที่ไม่ให้รันดาต้าปะครับ แบบ เปิดระบบมา ก้อใช้งานได้เลย ไม่ต้องมาโชว์รันดาต้าก่อนอะครับ
ตรงไหนหรอครับ หรือไม่ก็บอกคำเสิท กูเกิ้ลก้อได้ครับ ได้โปรดชี้แนะผมด้วย