伊莉討論區
標題:
想請問arduino合併程式
[打印本頁]
作者:
火焰大死亡
時間:
2020-2-19 07:31 PM
標題:
想請問arduino合併程式
#include <TM1637Display.h>
#define CLK 9
#define DIO 8
TM1637Display display(CLK,DIO);
boolean colon = true;
int ledPin = 2;
int counter = 0;
int pushbottonpin = 4;
int ledstate = 0;
int pushbottonstate = 0;
void setup() {
Serial.begin(9600);
pinMode (ledPin, OUTPUT);
pinMode (pushbottonpin,INPUT);
display.setBrightness(0xA);
}
void loop() {
pushbottonstate = digitalRead(pushbottonpin);
Serial.println(pushbottonstate);
if(pushbottonstate==HIGH)
{
while(true){
pushbottonstate = digitalRead(pushbottonpin);
if(pushbottonstate==LOW){
break;
} };
counter = counter+1;}
display.showNumberDec(counter,false);
delay(10);
//counter = counter+1;
//display.showNumberDec(5000,false);
//delay(100);
//display.showNumberDec(counter,false);
//delay(100);
}
//digitalWrite(ledPin,LOW);
//delay(100);
//digitalWrite(ledPin,HIGH);
--------------------------------------------------------------------------------
#include <DS1302.h>
#include <TM1637Display.h>
// DS1302 初始化設定
DS1302 rtc(2, 3, 4);
// 設定 TM1637 接腳
#define CLK 9
#define DIO 8
TM1637Display display(CLK, DIO);
boolean colon = true ;
String dw = "";
String hh = "";
String mm = "";
String ss = "";
float t = 0;
void setup()
{
// 設定時鐘執行模式,取消寫入保護
rtc.halt(false);
rtc.writeProtect(false);
// Setup Serial connection
Serial.begin(9600);
display.setBrightness(0xA);
// 第一次設定寫入 DS1302 RTC時鐘,之後可以加上註解
// rtc.setDOW(SUNDAY); // 設定每週星期幾?
rtc.setTime(13, 02, 10); // 設定24小時時間 20:16:30
// rtc.setDate(19, 3, 2017); // 設定日期(日, 月, 年)
}
void loop()
{
// 取得星期幾
Serial.print(rtc.getDOWStr());
Serial.print(" ");
// 取得日期
Serial.print(rtc.getDateStr());
Serial.print(" -- ");
// 取得時間
dw = rtc.getTimeStr();
Serial.println(dw);
hh = dw.substring(0,2); // 時數
mm = dw.substring(3,5); // 分鐘數
ss = dw.substring(6,8); // 秒數
// 顯示四位數中間的冒號
uint8_t segto;
int value = 1000;
// 顯示 時:分
int t = hh.toInt()*100 + mm.toInt();
// 顯示 分:秒
// int t = mm.toInt() *100 +ss.toInt();
segto = 0x80 | display.encodeDigit((t / 100)%10);
display.setSegments(&segto, 1, 1);
delay(500);
// 顯示時間
display.showNumberDec(t, true);
delay(500);
}
------------------------------------------------------
想請問一下各位大大,上面這兩個程式要怎麼合併,因為同時要啟動兩顆四位元7段,但是一顆功能用在,抓現在時間;另一顆功能用在按鈕壓的次數
作者:
gonewang123
時間:
2020-2-20 10:06 AM
Hello,
因為你沒有繪出線路,再加上有些code , 如:Serial.begin(9600); 跟你要的好像沒關係。。。
這兩段程式碼,第二段看起來比較像 7-seg LED 顯示時間,這個應該比較複雜。
所以,我會建議,將第一段(計數按鈕次數)的功能,塞到第二段來。
將必要的initial setting塞到 setup(),讀取 push button 的 event 以及counter 塞到 loop()
因為通常不會剛好有實際的東西在手邊,所以code的修正恐怕得靠你自己了。
作者:
gonewang123
時間:
2020-2-20 10:24 AM
抱歉。。。
從沒想過 7-seg LED 顯示也需要用到 SCI
"https"
"://"
"atceiling"
".blogspot"
".com"
"/2017/03/"
"arduino-rtc-tm1637.html"
抱歉,不能post URL,請參考上面連結。
如前一所述,理論上 push button 的每個 event 發生後,去累加counter,將它顯示到LED。
頂多就是 event 發生需要 debounce,可以硬體deboounce。
或是用軟體 filter。譬如:累加持續的十筆狀態改變,才承認。
作者:
gonewang123
時間:
2020-2-20 11:22 AM
Hello,
順便可以提及一下FIR 數位濾波器,參考下面連結:
"https"
"://"
"pojenlai"
".wordpress"
".com"
"/2016/04/10/數位濾波器的學習筆記/"
平均每個delay 的時間(取樣頻率的倒數)的取樣值 x b (係數) 的累加值
b 都是 "1" 的話,其實就是算數平均。
FYI
作者:
johnwanz
時間:
2020-2-22 02:07 PM
針對顯示的部分
1. TM1637Display display(CLK, DIO); // 物件宣告
TM1637Display class: 提供display物件的建構以及操作的介面
(CLK, DIO)是該物件的通訊腳位 (建構式)
display, 是該物件的名稱
2. 當需要兩個顯示的時候, 就宣告兩次, 各自使用不同的名稱即可
TM1637Display display1(CLK1, DIO1);
TM1637Display display2(CLK2, DIO2);
CLK1/2, DIO1/2, 各自對應硬體連接的腳位去定義.
其實主要顯示的部分就搞定了, 後面記得對應修改使用的TM1637Display物件名稱.
另外, loop整合
最簡單, 容易的方式, 就是用兩個函式分別將兩個檔案的loop, 個別帶入.
再用同一個loop()引用,
loop()
{
func1();
func2();
}
複製代碼
但是, 由於兩個函式的執行迴圈間隔不同, 最好提出最小的delay到loop, 再重新依據需要, 用變數計算次數得到時間, 再去執行.
void loop()
{
static int cnt = 0;
cnt++;
if( 0 == (cnt%FUNC1_TIMES) ) //用餘數計算幾次delay後執行
func1();
if( 0 == (cnt%FUNC2_TIMES) ) //用餘數計算幾次delay後執行
func2();
delay(); // 最小時間
}
複製代碼
以上, 初步的想法, 供參考.
作者:
gonewang123
時間:
2020-2-22 09:54 PM
byte bit_buf = 0;
void setup()
{
// Timer1 initialization
Timer1.initialize(1000); // set a timer of length 1000 us
Timer1.attachInterrupt( timerIsr ); // attach the service routine here
}
void timerIsr()
{
bit_buf << 1;
bitWrite(bit_buf , 0, digitalRead(pushbottonpin) );
}
loop()
{
if(bit_buf == 255)
pushbottonstate==HIGH;
if(bit_buf == 0)
pushbottonstate==LOW;
do counter & display....
}
作者:
gonewang123
時間:
2020-2-22 10:04 PM
前述
取樣與算術平均,雖然其實不是平均。。。
反正也類似濾波的效果。。。
其他的。我覺得應該還好,原code就有。
歡迎光臨 伊莉討論區 (http://a401.file-static.com/)
Powered by Discuz!