{"id":1223,"date":"2025-12-18T08:00:00","date_gmt":"2025-12-18T00:00:00","guid":{"rendered":"https:\/\/www.640629.xyz\/?p=1223"},"modified":"2025-12-17T10:08:02","modified_gmt":"2025-12-17T02:08:02","slug":"%e5%bc%95%e7%94%a8%e5%a4%96%e9%83%a8%e5%87%bd%e5%bc%8f%e5%ba%ab%ef%bc%9aonebutton%e3%80%81grove-4-digit-display","status":"publish","type":"post","link":"https:\/\/www.640629.xyz\/?p=1223","title":{"rendered":"\u5f15\u7528\u5916\u90e8\u51fd\u5f0f\u5eab\uff1aOneButton\u3001Grove 4-Digit Display"},"content":{"rendered":"<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" src=\"https:\/\/img.640629.xyz\/i\/2025\/01\/02\/6775f80d8967c.png\" alt=\"\"\/><\/figure>\n<\/div>\n\n\n<p>Arduino\u7684<strong>OneButton Library<\/strong>\u662f\u4e00\u500b\u975e\u5e38\u5be6\u7528\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u4f86\u8f15\u9b06\u8655\u7406\u6309\u9215\u7684\u591a\u7a2e\u64cd\u4f5c\uff0c\u4f8b\u5982\uff1a\u55ae\u64ca\u3001\u96d9\u64ca\u3001\u9577\u6309\u7b49\u3002\u5c0d\u65bc\u9700\u8981\u8655\u7406\u591a\u7a2e\u6309\u9215\u8f38\u5165\u6a21\u5f0f\u7684\u61c9\u7528\u5834\u666f\u975e\u5e38\u65b9\u4fbf\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">#include &lt;OneButton.h&gt;         \/\/ \u5f15\u7528OneButton\u51fd\u5f0f\u5eab\n#define pb 34                  \/\/ \u5b9a\u7fa9\u6309\u9215\u5f15\u8173\nOneButton button(pb, true);    \/\/ \u5efa\u7acbOneButton\u7269\u4ef6\uff0c\u540d\u7a31\u70babutton\uff0c\u6309\u9215\u70ba\u4f4e\u614b\u52d5\u4f5c\n\nvoid setup() {\n  Serial.begin(9600);                           \/\/ \u555f\u7528\u4e32\u5217\u57e0\u76e3\u770b\u8996\u7a97\n  button.attachClick(singleClick);              \/\/ \u55ae\u64ca\n  button.attachDoubleClick(doubleClick);        \/\/ \u96d9\u64ca\n  button.attachLongPressStart(longPressStart);  \/\/ \u9577\u6309\u958b\u59cb\n  button.attachDuringLongPress(longPress);      \/\/ \u9577\u6309\u904e\u7a0b\u4e2d\n  button.attachLongPressStop(longPressStop);    \/\/ \u9577\u6309\u7d50\u675f\n}\n\nvoid loop() {\n  button.tick(); \/\/ \u5b9a\u671f\u5075\u6e2c\u6309\u9215\u72c0\u614b\n}\n\n\/\/ \u55ae\u64ca\u8655\u7406\nvoid singleClick() {\n  Serial.println(\"Button Click.\");\n}\n\n\/\/ \u96d9\u64ca\u8655\u7406\nvoid doubleClick() {\n  Serial.println(\"Button Double Click.\");\n}\n\n\/\/ \u9577\u6309\u958b\u59cb\nvoid longPressStart() {\n  Serial.println(\"Button Long Press Start.\");\n}\n\n\/\/ \u9577\u6309\u904e\u7a0b\u4e2d\nvoid longPress() {\n  Serial.println(\"Button Long Press ......\");\n}\n\n\/\/ \u9577\u6309\u7d50\u675f\nvoid longPressStop() {\n  Serial.println(\"Button Long Press Stop.\");\n}<\/code><\/pre>\n\n\n\n<p>Grove 4-Digit Display \u662f\u4e00\u500b\u51fd\u5f0f\u5eab\uff0c\u53ef\u4ee5\u9a45\u52d5TM1637\u6676\u7247\u6240\u5efa\u69cb\u76844\u4f4d\u5143\u4e03\u6bb5\u986f\u793a\u5668\u6a21\u7d44\uff0c\u80fd\u5feb\u901f\u7c21\u55ae\u7684\u986f\u793a\u6578\u5b57(0-9)\u548c\u67d0\u4e9b\u5b57\u6bcd(A, b, C, d\u7b49)\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">\/\/-----------------------------------------------------------------------------\n#include \"TM1637.h\"            \/\/ \u5f15\u7528TM1637\u51fd\u5f0f\u5eab\uff0c\u4e26\u5b9a\u7fa9TM1637\u63a5\u8173\n#define CLK 22                 \/\/ CLK\u70ba\u6642\u8108\u63a5\u8173\n#define DIO 23                 \/\/ DIO\u70ba\u8cc7\u6599\u63a5\u8173\nTM1637 tm1637(CLK, DIO);       \/\/ \u5275\u7acb\u4e00\u500bTM1637\u7269\u4ef6\uff0c\u540d\u7a31\u70batm1637\n\/\/-----------------------------------------------------------------------------\nint num[4] = {0, 0, 0, 0};     \/\/ 4\u4f4d\u6578\u5b57\u9663\u5217 {\u4edf\u4f4d,\u767e\u4f4d,\u5341\u4f4d,\u500b\u4f4d}\nint count = 1234;\n\/\/-----------------------------------------------------------------------------\n\nvoid setup() {\n  \/\/----------------------------------------------------------\n  Serial.begin(9600);  \/\/ \u555f\u7528\u4e32\u5217\u57e0\u76e3\u770b\u8996\u7a97\n  \/\/----------------------------------------------------------\n  tm1637.init();               \/\/ \u521d\u59cb\u5316TM1637\n  tm1637.set(BRIGHT_TYPICAL);  \/\/ \u8a2d\u5b9a\u4eae\u5ea6\uff0c\u7bc4\u570d\u662f0\u52307\uff0c\u9810\u8a2d\u503c\u70ba2\n  \/\/----------------------------------------------------------\n}\n\nvoid loop() {\n  \/\/----------------------------------------------------------\n  \/\/ \u8a2d\u5b9a\u8207\u8f49\u63db\u4e03\u6bb5\u986f\u793a\u5668\u6bcf\u500b\u4f4d\u5143\u8981\u986f\u793a\u7684\u6578\u5b57\n  num[0] = count \/ 1000 % 10;   \/\/ \u5343\u4f4d\n  num[1] = count \/ 100 % 10;    \/\/ \u767e\u4f4d\n  num[2] = count \/ 10 % 10;     \/\/ \u5341\u4f4d\n  num[3] = count % 10;          \/\/ \u500b\u4f4d\n  tm1637.display(0, num[0]);    \/\/ \u986f\u793a\u7b2c\u4e00\u4f4d\n  tm1637.display(1, num[1]);    \/\/ \u986f\u793a\u7b2c\u4e8c\u4f4d\n  tm1637.display(2, num[2]);    \/\/ \u986f\u793a\u7b2c\u4e09\u4f4d\n  tm1637.display(3, num[3]);    \/\/ \u986f\u793a\u7b2c\u56db\u4f4d\n  \/\/----------------------------------------------------------\n}<\/code><\/pre>\n\n\n\n<p>\u7d50\u5408\u4e0a\u8ff02\u500bLibrary\u7684\u53c3\u8003\u7a0b\u5f0f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">\/\/-----------------------------------------------------------------------------\n#include &lt;OneButton.h&gt;         \/\/ \u5f15\u7528OneButton\u51fd\u5f0f\u5eab\n#define pb 34                  \/\/ \u5b9a\u7fa9\u6309\u9215\u5f15\u8173\nOneButton button(pb, true);    \/\/ \u5efa\u7acbOneButton\u7269\u4ef6\uff0c\u540d\u7a31\u70babutton\uff0c\u6309\u9215\u70ba\u4f4e\u614b\u52d5\u4f5c\n\/\/-----------------------------------------------------------------------------\n#include \"TM1637.h\"            \/\/ \u5f15\u7528TM1637\u51fd\u5f0f\u5eab\uff0c\u4e26\u5b9a\u7fa9TM1637\u63a5\u8173\n#define CLK 22                 \/\/ CLK\u70ba\u6642\u8108\u63a5\u8173\n#define DIO 23                 \/\/ DIO\u70ba\u8cc7\u6599\u63a5\u8173\nTM1637 tm1637(CLK, DIO);       \/\/ \u5275\u7acb\u4e00\u500bTM1637\u7269\u4ef6\uff0c\u540d\u7a31\u70batm1637\n\/\/-----------------------------------------------------------------------------\nint num[4] = {0, 0, 0, 0};     \/\/ 4\u4f4d\u6578\u5b57\u9663\u5217 {\u4edf\u4f4d,\u767e\u4f4d,\u5341\u4f4d,\u500b\u4f4d}\nint count = 5678;\n\/\/-----------------------------------------------------------------------------\n\nvoid setup() {\n  \/\/----------------------------------------------------------\n  Serial.begin(9600);  \/\/ \u555f\u7528\u4e32\u5217\u57e0\u76e3\u770b\u8996\u7a97\n  \/\/----------------------------------------------------------\n  button.attachClick(singleClick);              \/\/ \u55ae\u64ca\n  button.attachDoubleClick(doubleClick);        \/\/ \u96d9\u64ca\n  button.attachLongPressStart(longPressStart);  \/\/ \u9577\u6309\u958b\u59cb\n  button.attachDuringLongPress(longPress);      \/\/ \u9577\u6309\u904e\u7a0b\u4e2d\n  button.attachLongPressStop(longPressStop);    \/\/ \u9577\u6309\u7d50\u675f\n  \/\/----------------------------------------------------------\n  tm1637.init();               \/\/ \u521d\u59cb\u5316TM1637\n  tm1637.set(BRIGHT_TYPICAL);  \/\/ \u8a2d\u5b9a\u4eae\u5ea6\uff0c\u7bc4\u570d\u662f0\u52307\uff0c\u9810\u8a2d\u503c\u70ba2\n  \/\/----------------------------------------------------------\n}\n\nvoid loop() {\n  \/\/----------------------------------------------------------\n  button.tick(); \/\/ \u5b9a\u671f\u5075\u6e2c\u6309\u9215\u72c0\u614b\n  \/\/----------------------------------------------------------\n  \/\/ \u8a2d\u5b9a\u8207\u8f49\u63db\u4e03\u6bb5\u986f\u793a\u5668\u6bcf\u500b\u4f4d\u5143\u8981\u986f\u793a\u7684\u6578\u5b57\n  num[0] = count \/ 1000 % 10;   \/\/ \u5343\u4f4d\n  num[1] = count \/ 100 % 10;    \/\/ \u767e\u4f4d\n  num[2] = count \/ 10 % 10;     \/\/ \u5341\u4f4d\n  num[3] = count % 10;          \/\/ \u500b\u4f4d\n  tm1637.display(0, num[0]);    \/\/ \u986f\u793a\u7b2c\u4e00\u4f4d\n  tm1637.display(1, num[1]);    \/\/ \u986f\u793a\u7b2c\u4e8c\u4f4d\n  tm1637.display(2, num[2]);    \/\/ \u986f\u793a\u7b2c\u4e09\u4f4d\n  tm1637.display(3, num[3]);    \/\/ \u986f\u793a\u7b2c\u56db\u4f4d\n  \/\/----------------------------------------------------------\n}\n\n\/\/ \u55ae\u64ca\u8655\u7406\nvoid singleClick() {\n  Serial.println(\"Button Click.\");\n}\n\n\/\/ \u96d9\u64ca\u8655\u7406\nvoid doubleClick() {\n  Serial.println(\"Button Double Click.\");\n}\n\n\/\/ \u9577\u6309\u958b\u59cb\nvoid longPressStart() {\n  Serial.println(\"Button Long Press Start.\");\n}\n\n\/\/ \u9577\u6309\u904e\u7a0b\u4e2d\nvoid longPress() {\n  Serial.println(\"Button Long Press ......\");\n}\n\n\/\/ \u9577\u6309\u7d50\u675f\nvoid longPressStop() {\n  Serial.println(\"Button Long Press Stop.\");\n}<\/code><\/pre>\n\n\n\n<p>\u4f5c\u696d\u7df4\u7fd2\uff1a\u8acb\u6539\u7528OneButton Library\u3001Grove 4-Digit Display Library\u5b8c\u6210\u4e0a\u9031\u7684\u4f5c\u696d\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u958b\u6a5f\u5f8c\uff0c4\u4f4d\u5143\u4e03\u6bb5\u986f\u793a\u5668\u986f\u793a\u6b32\u5012\u6578\u8a08\u6642\u7684\u521d\u59cb\u6642\u9593(\u4f8b\uff1a1200\u4ee3\u886812:00\uff0c\u6642\u959312:00\u219211:59\u219211:58\u2192\u2026\u2026\u219211:01\u219211:00\u219210:59\u2192\u2026\u2026\u219200:00)\u3002<\/li>\n\n\n\n<li>\u6bcf\u6309\u4e00\u4e0b\u6309\u9215\u958b\u95dc\uff0c\u53ef\u958b\u59cb\u6216\u505c\u6b62\u5012\u6578\u8a08\u6578(\u6bcf\u79d2\u6e1b1\uff0c\u76f4\u52300000\u505c\u6b62)\u3002<\/li>\n\n\n\n<li>\u9577\u6309\u6309\u9215\u958b\u95dc\uff0c\u8a08\u6578\u5668\u91cd\u7f6e\u70ba\u521d\u59cb\u6642\u9593\u3002<\/li>\n\n\n\n<li>\u4efb\u4f55\u904e\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u900f\u904e\u76e3\u770b\u8996\u7a97\u6539\u8b8a\u5012\u6578\u8a08\u6642\u7684\u6578\u503c\u3002(\u8f38\u5165\u7bc4\u570d0000~5959)<\/li>\n\n\n\n<li>\u4e03\u6bb5\u986f\u793a\u5668\u7684\u72c0\u614b\u5fc5\u9808\u540c\u6b65\u986f\u793a\u5728\u76e3\u770b\u8996\u7a97\u4e0a\u3002<\/li>\n\n\n\n<li>\u8acb\u5b8c\u6210\u5be6\u9ad4\u63a5\u7dda\u3002<\/li>\n\n\n\n<li>\u8acb\u5728Wokwi\u7db2\u7ad9\u6a21\u64ec\u5f8c\uff0c\u4e26\u5b8c\u6210\u5be6\u9ad4\u63a5\u7dda\u3002<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Arduino\u7684OneButton Library\u662f\u4e00\u500b\u975e\u5e38\u5be6\u7528\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u4f86\u8f15\u9b06\u8655\u7406\u6309\u9215\u7684\u591a\u7a2e\u64cd\u4f5c\uff0c\u4f8b\u5982\uff1a<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-1223","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/www.640629.xyz\/index.php?rest_route=\/wp\/v2\/posts\/1223","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.640629.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.640629.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.640629.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.640629.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1223"}],"version-history":[{"count":7,"href":"https:\/\/www.640629.xyz\/index.php?rest_route=\/wp\/v2\/posts\/1223\/revisions"}],"predecessor-version":[{"id":2319,"href":"https:\/\/www.640629.xyz\/index.php?rest_route=\/wp\/v2\/posts\/1223\/revisions\/2319"}],"wp:attachment":[{"href":"https:\/\/www.640629.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.640629.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.640629.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}