|
本帖最後由 antlu 於 2020-2-6 09:41 PM 編輯
最近做了兩個事件紀錄電路,一個是紀錄10組觸發時間隨時可以讀取發生時間,動作是 使用 DS1302 RTC 隨時監控時間,一但 中斷觸發就紀錄當時的時間,這樣可以紀錄小貓侵入的時間,搭配錄影機就方便調影片來追蹤!! 另一個則搭配 SD卡片 把發生的時間紀錄在SD卡上,先前做了個電壓紀錄器,於是把他整合進來,也就是可以紀錄每隔 5秒 10秒 30秒 300秒 1800秒的三組外部輸入電壓數值,也可以紀錄外部觸發 中斷0 中斷1 時候的時間與電壓,這方便紀錄何時繼電器動作時候的三組電壓... 第二個因為搭配SD卡存資料,程式太大所以沒辦法做到讀取 SD卡記憶體的內容,SD卡的資料必須搭配電腦和讀卡機以 EXCEL 檔案讀取...提供大家參考..
第一台的外觀和線路 使用 ARDUINO PRO-MINI
按鍵一事件時間讀取增加,第二按鍵 事件時間讀取減少..
線路圖
PCB
外觀
第二台的外觀和線路 使用 ARDUINO NANO
外觀
線路
輸入部分
第一台程式 (使用 DS1302 RTC LCM使用74595+LCM2402 ARDUINO PRO-MINI)
- // USE 74595 LCM ARDUINO PROMINI Interrupt
- // key in low active interrupt BECKHOFF MODULE HIGH ACTIVE
- //used BECKOFF PLC INPUT MODULE LOW OUT 2020.01.23
- #include <DS1302.h>
- volatile boolean int0Flag = 0;
- volatile boolean int1Flag = 0;
- volatile byte evn0_count = 0;
- volatile byte evn1_count = 0;
- //Define 74HC595 Connections with arduio
- const int Dsply_Latch=13; //LCM----74595 pin 12 black
- const int Dsply_Data=12; //LCM----74595 pin 14 green
- const int Dsply_Clock=11; //LCM----74595 pin 11 blue
- const int RTC_CLK=6;//
- const int RTC_DAT=7;//
- const int RTC_RST=8;//
- const int EVN_1=2;//interrupt 0
- const int EVN_2=3;//interrupt 1
- const int ReadKey1 =4;//^ key
- const int ReadKey2 =5;//v key
- volatile byte EVN_1flag =0;
- volatile byte EVN_2flag =0;
- String date_bf0[]={"1","2","3","4","5","6","7","8","9","10"};//it is String not char ..
- String time_bf0[]={"a","b","c","d","e","f","g","h","i","j"};
- String date_bf1[]={"1","2","3","4","5","6","7","8","9","10"};//it is String not char ..
- String time_bf1[]={"a","b","c","d","e","f","g","h","i","j"};
- int bf0;
- int bf1;
- byte i =0;
- byte j =0;
- byte dspDatacount;
- boolean dspupdate_flag = true;
- // const int Led=13;//Led on off
- void LCDinit(void);
- void LCDSendByte(char d,char RS);
- void LCDPrint(char Line,char string[40]);//--
- DS1302 rtc(RTC_RST,RTC_DAT,RTC_CLK);
- char EVN_Array[3];
- char D[24];
- char A[20];
- void setup() {
- Serial.begin(9600);
- delay(500);
- pinMode(EVN_1,INPUT ); // INPUT_PULLUP
- pinMode(EVN_2, INPUT); //
- pinMode(ReadKey1, INPUT_PULLUP); //
- pinMode(ReadKey2, INPUT_PULLUP); //
- attachInterrupt(0,EVN_1_ISR,RISING); // set an interrupt on PinA
- attachInterrupt(1,EVN_2_ISR,RISING); // set an interrupt on PinB
- pinMode(Dsply_Data, OUTPUT);
- pinMode(Dsply_Clock, OUTPUT);
- pinMode(Dsply_Latch, OUTPUT);
- DS1302 rtc(RTC_RST,RTC_DAT,RTC_CLK);
- rtc.halt(false);
-
- //取消寫入保護,設定日期時要這行
- rtc.writeProtect(false);
- // 以下是設定時間的方法,在電池用完之前,只要設定一次就行了
- // rtc.setTime(23, 7, 0); // 設定時間 時,分,秒 (24hr format)
- // rtc.setDate(22, 1, 2020); // 設定日期 日,月,年
- LCDinit();
- LCDclean();
- LCDPrint(0," Event Record V1");
- LCDPrint(1," 2020.01.17");
- }
- void loop() {
- while(1)
- {
- // Serial.println("end of program");
- //---------start program ---------------
- if(int0Flag == true)
- {
-
- evn0_count ++;
- if (evn0_count >10){evn0_count =0;}
- Serial.print("evn0_count =");
- Serial.println(evn0_count);
- date_bf0[evn0_count] = rtc.getDateStr();
- time_bf0[evn0_count] = rtc.getTimeStr();
- dspupdate_flag = true;
- LCDPrintString(0,1," ");
- readData2Dsply(evn0_count);
- String evn_count_str = String(evn0_count);
- evn_count_str.toCharArray(EVN_Array,3);
- // LCDPrintString(0,1," int0 count ");
-
- LCDPrintString(0,1,EVN_Array);
- LCDWriteChar(0,23,'@');
- delay(100);
- int0Flag = false;
- }
- if(int1Flag == true)
- {
- evn1_count ++;
- int1Flag = false;
- if (evn1_count >10){evn1_count =0;}
- Serial.print("evn1_count =");
- Serial.println(evn1_count);
- date_bf1[evn1_count] = rtc.getDateStr();
- time_bf1[evn1_count] = rtc.getTimeStr();
- dspupdate_flag = true;
- LCDPrintString(1,1," ");
- readData2Dsply(evn1_count+10);
- String evn_count_str = String(evn1_count);
- evn_count_str.toCharArray(EVN_Array,3);
-
-
- LCDPrintString(1,1,EVN_Array);
- LCDWriteChar(1,23,'@');
- delay(100);
- int1Flag = false;
- }
- //read key in sw
- readSW();// const int ReadKey1 =4;//^ key const int ReadKey2 =5;//v key
- if( dspupdate_flag == true)
- {
- dspData();//display readKey
- // 日期
- Serial.print(rtc.getDateStr());
- Serial.print(" -- ");
- // 時間
- Serial.println(rtc.getTimeStr());
- Serial.println();
- }
- }
- }
- //=============================================================
- // Send Data or Command to LCD
- //=============================================================
- void LCDSendByte(char d,char RS)
- {
- char dH,dL,temp;
- //Keep Data on upper nybble
- dH = d & 0xF0; //Get MSB
- dL = d & 0x0F;
- dL = d << 4; //Get LSB
- //Send MSB with E=clock
- temp=0;
- temp=dH | RS | 0x02; //MSB With RS+E bit
- ShiftData(temp);
- //Send MSB with E=0
- temp=0;
- temp=dH | RS; //MSB With RS bit
- ShiftData(temp);
- //Send LSB with E=clock
- temp=0;
- temp=dL | RS | 0x02; //MSB With RS+E bit
- ShiftData(temp);
- //Send LSB with E=0
- temp=0;
- temp=dL | RS; //MSB With RS bit
- ShiftData(temp);
- }
- //=============================================
- void ShiftData(char temp)
- {
- int i;
- for(i=0;i<8;i++)
- {
- if((temp & 0x80)==0x80) //Send 1-bit to shift register
- {digitalWrite(Dsply_Data,HIGH);}
- else
- {digitalWrite(Dsply_Data,LOW);}
- digitalWrite(Dsply_Clock,LOW);
- digitalWrite(Dsply_Clock,HIGH);
- temp=temp<<1;
- }
- //Latch the data
- digitalWrite(Dsply_Latch,LOW);
- delay(1);
- digitalWrite(Dsply_Latch,HIGH);
- }
- //=================================================================
- // LCD Display Initialization Function
- //=================================================================
- void LCDinit()
- {
- int count;
- char t[]={0x43,0x03,0x03,0x02,0x28,0x01,0x0C,0x06,0x02,0x02};
- for (count = 0; count <= 9; count++)
- {
- LCDSendByte(t[count],0); //Command RS=0
- }
- }
- //=================================================================
- // Display Line on LCD at desired location Function
- //=================================================================
- void LCDPrint(char Line,char string[40])//--------------------------------------
- {
- int len,count;
- if(Line==0) {
- LCDSendByte(0x80,0); //Command RS=0 Move Cursor to Home
- }
- else {
- LCDSendByte(0xC0,0); //Command RS=0 Move Cursor to Second Line LCDSendByte(0xC0,0);
- }
- len = strlen(string);
- for (count=0;count<len;count++)
- {
- LCDSendByte(string[count],1); //Data RS=1
- }
- }
- //=================================================================
- void LCDclean(void)
- {
- LCDSendByte(0x01,0);
- }
- void LCDCurserMove(int y,int x)
- {
- // LCDSendByte((a*4+16),0);//a=1 shift right
- if (y == 0)
- {
- LCDSendByte(0x80 + x,0);
- }
- else
- {
- LCDSendByte(0xC0 + x,0);
- }
- LCDSendByte(0x0b,0);
- }
- void LCDPrintString(int x,int y, char string[])
- {
- int count, len;
- // summ = x;
- if (x == 0)
- {
- LCDSendByte(0x80+y ,0);
- }
- else
- {
- LCDSendByte(0xC0+y,0);
- }
- len = strlen(string);
- for (count=0;count<len;count++)
- {
- LCDSendByte(string[count],1); //Data RS=1
- }
- }
- void LCDWriteChar(int x,int y,char Data)
- {
- if (x == 0)
- {
- LCDSendByte(0x80 + y,0);
- }
- else
- {
- LCDSendByte(0xC0 + y,0);
- }
- LCDSendByte(Data,1);
- }
- void LCDhome(void)
- {
- LCDSendByte(0x02,0);
- }
- void LCDDisp(void)
- {
- LCDSendByte(0x0f,0);
- }
- void EVN_1_ISR()//interruput 0
- {
- int0Flag = true;
- }
- void EVN_2_ISR()//interrupt 1
- {
- int1Flag = true;
- }
- void readSW(void)
- {
- //const int ReadKey1 =4;//^ key
- //const int ReadKey2 =5;//v key
- if((!digitalRead(ReadKey1))&&(digitalRead(ReadKey2))) //key 1 pressed++
- {
- delay(200);
- if((!digitalRead(ReadKey1))&&(digitalRead(ReadKey2)))
- // if(!digitalRead(ReadKey1))
- {
- if(dspDatacount >19){dspDatacount=0;}
- dspDatacount++;//display record
-
- Serial.print("dspDatacount = ");
- Serial.println(dspDatacount);
- dspupdate_flag = true;
- readData2Dsply(dspDatacount);
- LCDWriteChar(0,23,'R');
- LCDWriteChar(1,23,'R');
- }
- }
- if((digitalRead(ReadKey1))&&(!digitalRead(ReadKey2))) //key 2 pressed--
- {
- delay(100);
- if((digitalRead(ReadKey1))&&(!digitalRead(ReadKey2)))
- // if(!digitalRead(ReadKey2))
- {
- if(dspDatacount !=0){dspDatacount--;}
- // Serial.print("dspDatacount = ");
- // Serial.println(dspDatacount);
- dspupdate_flag = true;
- readData2Dsply(dspDatacount);
-
- }
- }
- if((!digitalRead(ReadKey1))&&(!digitalRead(ReadKey2))) //key 1 & 2 pressed
- {
- delay(500);
- if((!digitalRead(ReadKey1))&&(!digitalRead(ReadKey2)))
- {
- for(byte i=0;i<10;i++)
- {
- time_bf0[i]="";
- time_bf1[i]="";
- }
- evn0_count = 0;
- evn1_count = 0;
- dspDatacount = 0;
- // Serial.print("dspDatacount = ");
- // Serial.println(dspDatacount);
- dspupdate_flag = true;
-
- }
- }
-
- }
- void dspData(void)//display readKey
- {
- // Serial.println("LCD display *** ");
- // Serial.print("evn0 display = ");
- // Serial.println(evn0_count);
- // Serial.print("evn1 display = ");
- // Serial.println(evn1_count);
- dspupdate_flag = false;
- }
- void readData2Dsply(byte rcount)
- {
- LCDWriteChar(0,0,'A');
- LCDWriteChar(1,0,'B');
- if(rcount<10)
- {
- // Serial.print("Read event count = ");
- // Serial.println(rcount);
- // Serial.println("Read event date&time = ");
- // Serial.print(date_bf0[rcount]);
- // Serial.print(" ");
- // Serial.println(time_bf0[rcount]);
- String rcount_str = String(rcount);
- rcount_str.toCharArray(EVN_Array,3);
- LCDPrintString(0,1,EVN_Array);
- date_bf0[rcount].toCharArray(D,11);
- LCDPrintString(0,3,D);
- time_bf0[rcount].toCharArray(D,10);
- LCDPrintString(0,14,D);
- }
- if(rcount ==10)
- {
- LCDWriteChar(0,2,' ');
- LCDWriteChar(0,22,' ');
- }
- if((rcount >10) && (rcount<=19))
- {
- // Serial.print("Read event count = ");
- // Serial.println(rcount);
- rcount=rcount-10;
- // Serial.println("Read event date&time = ");
- // Serial.print(date_bf1[rcount]);
- // Serial.print(" ** ");
- // Serial.println(time_bf1[rcount]);
- String rcount_str = String(rcount);
- rcount_str.toCharArray(EVN_Array,3);
- LCDPrintString(1,1,EVN_Array);
- date_bf1[rcount].toCharArray(D,11);
- LCDPrintString(1,3,D);
- time_bf1[rcount].toCharArray(D,10);
- LCDPrintString(1,14,D);
- }
- }
複製代碼
第二台的程式
說明: 使用 I2C 介面的 DS1307 RTC 和 LCM2004 和 ARDUINO NANO
按鍵一開始/停止紀錄SD ,第二按鍵 選擇時間間隔 5秒...10秒..
- /* FILE NAME datalog20200127eventV2
- * 20200201 增加了選擇內部計時的功能 兩個按鍵 按鍵一 開始與停止 按鍵二 內部時間間隔選擇
- 5秒 10秒 30秒 60秒 300秒 1800秒 選擇之後 按下開始 就開始計時與紀錄.
- 起始動作 顯示時間而以,螢幕顯示 STOP ,此時並沒有啟動開檔動作(SD CARD) 內部自定 60秒,
- 一但按下START/STOP 按鍵時開始開檔啟動紀錄,這時候就不在建立新檔案,若再按一次 START/STOP 鍵
- 只會做停止計時動作與檔案 SD.FLASH動作,在 任何狀況下可以選擇時間間隔.
- */
-
- #include <SPI.h>
- #include <SD.h>
- #include <Wire.h>
- #include "RTClib.h"
- #include <LiquidCrystal_I2C.h> //20200127
- LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
- #define ECHO_TO_SERIAL 1 // echo data to serial port
- #define WAIT_TO_START 0 // Wait for serial input in setup()
- // The analog pins that connect to the sensors
- #define whitePortPin 0 // analog 0 40V white port adjustable 5 10 40V range
- #define redPortPin 1 // analog 1 5V red port fix 5V range
- #define yellowPortPin 2 // analog 2 20V yellow port fix 20V range 2017.01.01
- #define BANDGAPREF 14 // special indicator that we want to measure the bandgap
- #define aref_voltage 5.0 // we tie 3.3V to ARef and measure it with a multimeter!2017.01.01 change from 3.3 to 5.0V
- #define bandgap_voltage 1.1 // this is not super guaranteed but its not -too- off
- RTC_DS1307 RTC; // define the Real Time Clock object
- const int chipSelect = 10;
- // the logging file
- File logfile;
- void error(char *str)
- {
- Serial.print(F("error: "));
- Serial.println(str);
- lcd.setCursor(1,0);
- lcd.print("err ");
- lcd.print(str);
- while(1);
- }
- //------------20200129-----------------
- byte count = 0;
- volatile boolean int0Flag = 0;
- volatile boolean int1Flag = 0;
- volatile byte evn0_count = 0;
- volatile byte evn1_count = 0;
- const byte EVN_1=2;//interrupt 0
- const byte EVN_2=3;//interrupt 1
- const byte ReadKey1 =4;// START/STOP key
- const byte ReadKey2 =5;// time interval select defalt 60s ,5,10,30,60,300,1800.
- boolean Start_Stop_flag = false;// true is Start,false is Stop
- const int Time_period[6] ={60,5,10,30,300,1800};
- byte Time_periord_count = 0;
- long startTime = 0;//time delay counter 1000ms
- int down_count_sec =0;
- int time_record_count=0;
- boolean onesec = false;
- boolean delayFlag =false;
- boolean dsp_flag = false;
- //------------end--------------
- void setup(void)
- {
- Serial.begin(9600);
- Serial.println();
- pinMode(EVN_1,INPUT ); // INPUT_PULLUP
- pinMode(EVN_2, INPUT); //
- pinMode(ReadKey1, INPUT_PULLUP); // 開始/停止(存檔SD)
- pinMode(ReadKey2, INPUT_PULLUP); // 選擇間隔秒數
- attachInterrupt(0,EVN_1_ISR,RISING); // set an interrupt on PinA
- attachInterrupt(1,EVN_2_ISR,RISING); // set an interrupt on PinB
- lcd.begin (20,4); // initialize the lcd
- lcd.backlight();//To Power ON the back light
- //--------20200201--------------------
- //initial 一開始先進入停止模式一但啟動再做SD卡存取
- while( !Start_Stop_flag)
- {
- if(Start_Stop_flag == false)
- {
- lcd.setCursor(1,2);
- lcd.print("STOP ");
- }
- if((!digitalRead(ReadKey1))&&(digitalRead(ReadKey2))) //key 1 pressed++
- {
- delay(200);
- if((!digitalRead(ReadKey1))&&(digitalRead(ReadKey2)))
- {
- Start_Stop_flag = (!Start_Stop_flag);
- if(Start_Stop_flag == true)
- {
- lcd.setCursor(1,2);
- lcd.print("START");
- down_count_sec = Time_period[Time_periord_count];//start count
- }
- if(Start_Stop_flag == false)
- {
- //logfile.flush();
- }
- }
- }
- if((digitalRead(ReadKey1))&&(!digitalRead(ReadKey2))) //key 2 pressed--
- {
- delay(100);
- if((digitalRead(ReadKey1))&&(!digitalRead(ReadKey2)))
- {
- Time_periord_count++;
- if(Time_periord_count >5){Time_periord_count =0;}
- lcd.setCursor(2,3);
- lcd.print("Time period: ");
- lcd.setCursor(14,3);
- lcd.print(Time_period[Time_periord_count]);
- Serial.print("Time period: ");
- Serial.println(Time_period[Time_periord_count]);
- }
- }
- }
- //-----------20200201 END------------
- #if WAIT_TO_START
- Serial.println("Type any character to start");
- while (!Serial.available());
- #endif //WAIT_TO_START
- // initialize the SD card
- Serial.print(F("Initializing SD card..."));
- lcd.setCursor(1,0);
- lcd.print("init SD ..");
- // make sure that the default chip select pin is set to
- // output, even if you don't use it:
- pinMode(chipSelect, OUTPUT);
-
- // see if the card is present and can be initialized:
- if (!SD.begin(chipSelect)) {
- error("NO Card or NG");
- }
- Serial.println(F("card initialized."));
-
- // create a new file
- char filename[] = "Antlog00.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) {
- error("couldnt create file");
- }
-
- Serial.print(F("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("millis,stamp,datetime,whitePort,redPort,yellowPort,countNo.");
- #if ECHO_TO_SERIAL
- Serial.println("millis,stamp,datetime,whitePort,redPort,yellowPort,countNo.");
- #endif //ECHO_TO_SERIAL
-
- // If you want to set the aref to something other than 5v
- // analogReference(EXTERNAL);
- analogReference(DEFAULT);
- if (! RTC.isrunning()) {
- RTC.adjust(DateTime(F(__DATE__), F(__TIME__)));
- }
- }
- void loop(void)
- {
- while(1){
- DateTime now;
- readSW();//read START/STOP key and TIME_period select
- if(int0Flag == true)
- {
- lcd.clear();
- if (int0Flag ==true){ evn0_count ++; }
- Serial.print(F("evn0_count ="));
- Serial.println(evn0_count);
- now = RTC.now();
- lcd.setCursor(0, 2);
- lcd.print("A");
- lcd.print(now.day());
- lcd.print('/');
- lcd.print(now.month());
- lcd.print('/');
- lcd.print(now.year());
- lcd.setCursor(10, 2);//position,line
- lcd.print(" ");
- lcd.print(now.hour());
- lcd.print(':');
- lcd.print(now.minute());
- lcd.print(':');
- lcd.print(now.second());
- lcd.print(" ");
- lcd.setCursor(1,3);
- lcd.print("A ");
- lcd.print(evn0_count);
- lcd.setCursor(10,3);
- lcd.print("B ");
- lcd.print(evn1_count);
- uint32_t m = millis();
- logfile.print(m); // milliseconds since start
- logfile.print(", ");
- #if ECHO_TO_SERIAL
- Serial.print(m); // milliseconds since start
- Serial.print(", ");
- #endif
- now = RTC.now();
- logfile.print(now.unixtime()); // seconds since 1/1/1970
- logfile.print(", ");
- logfile.print('"');
- logfile.print(now.year(), DEC);
- logfile.print("/");
- logfile.print(now.month(), DEC);
- logfile.print("/");
- logfile.print(now.day(), DEC);
- logfile.print(" ");
- logfile.print(now.hour(), DEC);
- logfile.print(":");
- logfile.print(now.minute(), DEC);
- logfile.print(":");
- logfile.print(now.second(), DEC);
- logfile.print('"');
- #if ECHO_TO_SERIAL
- Serial.print(now.unixtime()); // seconds since 1/1/1970
- Serial.print(", ");
- Serial.print('"');
- Serial.print(now.year(), DEC);
- Serial.print("/");
- Serial.print(now.month(), DEC);
- Serial.print("/");
- Serial.print(now.day(), DEC);
- Serial.print(" ");
- Serial.print(now.hour(), DEC);
- Serial.print(":");
- Serial.print(now.minute(), DEC);
- Serial.print(":");
- Serial.print(now.second(), DEC);
- Serial.print('"');
- #endif //ECHO_TO_SERIAL
- analogRead(whitePortPin);
- delay(10);
- int whitePortReading = analogRead(whitePortPin);
- analogRead(redPortPin);
- delay(10);
- int redPortReading = analogRead(redPortPin);
- analogRead(yellowPortPin); //2017
- delay(10); //2017
- int yellowPortReading = analogRead(yellowPortPin); //2017
- float whiteVoltage = whitePortReading * aref_voltage * 8 / 1024;//2017
- float redVoltage = redPortReading * aref_voltage / 1024;//2017
- float yellowVoltage = yellowPortReading * aref_voltage * 4 / 1024;//2017
- logfile.print(", ");
- logfile.print(whiteVoltage);//2017
- logfile.print(", ");
- logfile.print(redVoltage);//2017
- logfile.print(", ");
- logfile.print(yellowVoltage);//2017
-
- #if ECHO_TO_SERIAL
- Serial.print(", ");
- Serial.print(whiteVoltage);//2017
- Serial.print(", ");
- Serial.print(redVoltage);//2017
- Serial.print(", ");
- Serial.print(yellowVoltage);//2017
- #endif //ECHO_TO_SERIAL
- // Log the estimated 'VCC' voltage by measuring the internal 1.1v ref
- analogRead(BANDGAPREF);
- delay(10);
- int refReading = analogRead(BANDGAPREF);
- float supplyvoltage = (bandgap_voltage * 1024) / refReading;
-
- logfile.print(", ");
- if(int0Flag ==true)
- {
- logfile.print(evn0_count);
- logfile.print(",A ");
- int0Flag = false;
- }
- #if ECHO_TO_SERIAL
- Serial.print(", ");
- Serial.print(evn0_count);
- #endif // ECHO_TO_SERIAL
- logfile.println();
- #if ECHO_TO_SERIAL
- Serial.println();
- #endif // ECHO_TO_SERIAL
- count++;
- }
- //--------------interrupt 1----------------------
-
- // int0 or int1 when interrupt triggered then evn0_count & evn1_count ++ 5count save file
- if(int1Flag == true)
- {
- lcd.clear();
- if (int1Flag ==true){ evn1_count ++; }
- Serial.print(F("evn1_count ="));
- Serial.println(evn1_count);
- now = RTC.now();
- lcd.setCursor(0, 2);
- lcd.print("B");
- lcd.print(now.day());
- lcd.print('/');
- lcd.print(now.month());
- lcd.print('/');
- lcd.print(now.year());
- lcd.setCursor(10, 2);//position,line
- lcd.print(" ");
- lcd.print(now.hour());
- lcd.print(':');
- lcd.print(now.minute());
- lcd.print(':');
- lcd.print(now.second());
- lcd.print(" ");
-
- lcd.setCursor(1,3);
- lcd.print("A ");
- lcd.print(evn0_count);
- lcd.setCursor(10,3);
- lcd.print("B ");
- lcd.print(evn1_count);
- // log milliseconds since starting
- uint32_t m = millis();
- logfile.print(m); // milliseconds since start
- logfile.print(", ");
- #if ECHO_TO_SERIAL
- Serial.print(m); // milliseconds since start
- Serial.print(", ");
- #endif
- // fetch the time
- now = RTC.now();
- logfile.print(now.unixtime()); // seconds since 1/1/1970
- logfile.print(", ");
- logfile.print('"');
- logfile.print(now.year(), DEC);
- logfile.print("/");
- logfile.print(now.month(), DEC);
- logfile.print("/");
- logfile.print(now.day(), DEC);
- logfile.print(" ");
- logfile.print(now.hour(), DEC);
- logfile.print(":");
- logfile.print(now.minute(), DEC);
- logfile.print(":");
- logfile.print(now.second(), DEC);
- logfile.print('"');
- #if ECHO_TO_SERIAL
- Serial.print(now.unixtime()); // seconds since 1/1/1970
- Serial.print(", ");
- Serial.print('"');
- Serial.print(now.year(), DEC);
- Serial.print("/");
- Serial.print(now.month(), DEC);
- Serial.print("/");
- Serial.print(now.day(), DEC);
- Serial.print(" ");
- Serial.print(now.hour(), DEC);
- Serial.print(":");
- Serial.print(now.minute(), DEC);
- Serial.print(":");
- Serial.print(now.second(), DEC);
- Serial.print('"');
- #endif //ECHO_TO_SERIAL
- analogRead(whitePortPin);
- delay(10);
- int whitePortReading = analogRead(whitePortPin);
- analogRead(redPortPin);
- delay(10);
- int redPortReading = analogRead(redPortPin);
- analogRead(yellowPortPin); //2017
- delay(10); //2017
- int yellowPortReading = analogRead(yellowPortPin); //2017
-
- // converting that reading to voltage, for 3.3v arduino use 3.3, for 5.0, use 5.0
- // float voltage = redPortReading * aref_voltage / 1024;
- // float temperatureC = (voltage - 0.5) * 100 ;
- // float temperatureF = (temperatureC * 9 / 5) + 32;
- float whiteVoltage = whitePortReading * aref_voltage * 8 / 1024;//2017
- float redVoltage = redPortReading * aref_voltage / 1024;//2017
- float yellowVoltage = yellowPortReading * aref_voltage * 4 / 1024;//2017
- logfile.print(", ");
- logfile.print(whiteVoltage);//2017
- logfile.print(", ");
- logfile.print(redVoltage);//2017
- logfile.print(", ");
- logfile.print(yellowVoltage);//2017
-
- #if ECHO_TO_SERIAL
- Serial.print(", ");
- Serial.print(whiteVoltage);//2017
- Serial.print(", ");
- Serial.print(redVoltage);//2017
- Serial.print(", ");
- Serial.print(yellowVoltage);//2017
- #endif //ECHO_TO_SERIAL
- // Log the estimated 'VCC' voltage by measuring the internal 1.1v ref
- analogRead(BANDGAPREF);
- delay(10);
- int refReading = analogRead(BANDGAPREF);
- float supplyvoltage = (bandgap_voltage * 1024) / refReading;
- logfile.print(", ");
- if(int1Flag ==true)
- {
- logfile.print(evn1_count);
- logfile.print(",B ");
- int1Flag = false;
- }
- #if ECHO_TO_SERIAL
- Serial.print(", ");
- Serial.print(evn0_count);
-
- #endif // ECHO_TO_SERIAL
- logfile.println();
- #if ECHO_TO_SERIAL
- Serial.println();
- #endif // ECHO_TO_SERIAL
- count++;
- }
- //--------------end interrupt1----------------------------
- //----20200201--time delay -1s -----------
- if(Start_Stop_flag == true)
- {
- if(millis()-startTime>1000)//1s 1 time
- {
- down_count_sec--;
- onesec = true;
- startTime = millis();
- }
- // Serial.println("START status");
- if(down_count_sec == 0)
- {
- uint32_t m = millis();
- logfile.print(m); // milliseconds since start
- logfile.print(", ");
- now = RTC.now();
- logfile.print(now.unixtime()); // seconds since 1/1/1970
- logfile.print(", ");
- logfile.print('"');
- logfile.print(now.year(), DEC);
- logfile.print("/");
- logfile.print(now.month(), DEC);
- logfile.print("/");
- logfile.print(now.day(), DEC);
- logfile.print(" ");
- logfile.print(now.hour(), DEC);
- logfile.print(":");
- logfile.print(now.minute(), DEC);
- logfile.print(":");
- logfile.print(now.second(), DEC);
- logfile.print('"');
- #if ECHO_TO_SERIAL
- Serial.print(now.unixtime()); // seconds since 1/1/1970
- Serial.print(", ");
- Serial.print('"');
- Serial.print(now.year(), DEC);
- Serial.print("/");
- Serial.print(now.month(), DEC);
- Serial.print("/");
- Serial.print(now.day(), DEC);
- Serial.print(" ");
- Serial.print(now.hour(), DEC);
- Serial.print(":");
- Serial.print(now.minute(), DEC);
- Serial.print(":");
- Serial.print(now.second(), DEC);
- Serial.print('"');
- #endif //ECHO_TO_SERIAL
- analogRead(whitePortPin);
- delay(10);
- int whitePortReading = analogRead(whitePortPin);
- // Serial.print (F("whitePortPin "));
- // Serial.println(whitePortPin);
- // Serial.print (F("whitePortReading "));
-
- // Serial.println(whitePortReading);
- analogRead(redPortPin);
- delay(10);
- int redPortReading = analogRead(redPortPin);
- analogRead(yellowPortPin); //2017
- delay(10); //2017
- int yellowPortReading = analogRead(yellowPortPin); //2017
- float whiteVoltage = whitePortReading * aref_voltage * 8 / 1024;//2017
- float redVoltage = redPortReading * aref_voltage / 1024;//2017
- float yellowVoltage = yellowPortReading * aref_voltage * 4 / 1024;//2017
- logfile.print(", ");
- logfile.print(whiteVoltage);//2017
- logfile.print(", ");
- logfile.print(redVoltage);//2017
- logfile.print(", ");
- logfile.print(yellowVoltage);//2017
- logfile.print(", ");
- logfile.print(time_record_count);
- logfile.print(",T ");
- logfile.println();
-
- logfile.flush(); //????? 是否需要?
- #if ECHO_TO_SERIAL
- Serial.print(", ");
- Serial.print(whiteVoltage);//2017
- Serial.print(", ");
- Serial.print(redVoltage);//2017
- Serial.print(", ");
- Serial.print(yellowVoltage);//2017
- #endif //ECHO_TO_SERIAL
- down_count_sec= Time_period[Time_periord_count];//reset timer counter
- Serial.println("down count complete status");
- time_record_count++;
- lcd.setCursor(16,1);
- lcd.print(time_record_count);
- }
- }
- //-----20200201 end ------------
-
- if(count ==5)//per 5 time save 1 time to SD card
- {
- logfile.flush();
- count = 0;
- }
- if(onesec ==true)//1S update once
- {
- onesec = false;
- now = RTC.now();
- lcd.setCursor(0, 1);//position,line
- lcd.print("TIME");
- lcd.print(" ");
- lcd.print(now.hour());
- lcd.print(':');
- lcd.print(now.minute());
- lcd.print(':');
- lcd.print(now.second());
- lcd.print(" ");
- lcd.setCursor(0, 0);
- lcd.print("DATE");
- lcd.print(" ");
- lcd.print(now.day());
- lcd.print('/');
- lcd.print(now.month());
- lcd.print('/');
- lcd.print(now.year());
- }
- }
- }
- void EVN_1_ISR()//interruput 0
- {
- int0Flag = true;
- }
- void EVN_2_ISR()//interrupt 1
- {
- int1Flag = true;
- }
- //--------20200201--------------------
- void readSW(void)
- {
- if((!digitalRead(ReadKey1))&&(digitalRead(ReadKey2))) //start/stop key
- {
- delay(200);
- if((!digitalRead(ReadKey1))&&(digitalRead(ReadKey2)))
- {
-
- Start_Stop_flag = (!Start_Stop_flag);
- dsp_flag= true;
- }
- }
- if((digitalRead(ReadKey1))&&(!digitalRead(ReadKey2))) //key 2 pressed--
- {
- delay(100);
- if((digitalRead(ReadKey1))&&(!digitalRead(ReadKey2)))
- {
- Time_periord_count++;
-
- if(Time_periord_count >5){Time_periord_count =0;}
- dsp_flag= true;
- down_count_sec= Time_period[Time_periord_count];//reset timer counter
- Serial.print(F("Time period: "));
- Serial.println(Time_period[Time_periord_count]);
- }
- }
- if(dsp_flag== true)
- {
- lcd.clear();
- lcd.setCursor(0,2);
- if(Start_Stop_flag == false)
- {
- lcd.setCursor(1,2);
- lcd.print("STOP");
- }
- else
- {
- lcd.print("START");
- }
- lcd.setCursor(1,3);
- lcd.print(" Time period: ");
- lcd.setCursor(14,3);
- lcd.print(Time_period[Time_periord_count]);
- dsp_flag = false;
- }
- }
- //-----------20200201 END------------
複製代碼 |
-
評分
-
12
查看全部評分
-
|