{"id":2814,"date":"2018-09-16T18:16:53","date_gmt":"2018-09-16T10:16:53","guid":{"rendered":"https:\/\/drho.club\/?p=2362"},"modified":"2018-09-16T18:16:53","modified_gmt":"2018-09-16T10:16:53","slug":"nodemcu-micropython-rgbled-effects","status":"publish","type":"post","link":"https:\/\/104.es\/index.php\/2018\/09\/16\/nodemcu-micropython-rgbled-effects\/","title":{"rendered":"\u3010NodeMCU MicroPython\u3011\u4e09\u8272LED\u7684\u547c\u5438\u71c8\u6548\u679c"},"content":{"rendered":"<p>\u9023\u63a5\u7dda\u7684\u65b9\u5f0f\u548c\u4e0a\u4e00\u500b\u7df4\u7fd2\u76f8\u540c\uff0c\u4f46\u662f\u6211\u5011\u628a\u7a0b\u5f0f\u4fee\u6539\u4e00\u4e0b\uff0c\u5c31\u53ef\u4ee5\u5f97\u5230\u8a31\u591a\u4e0d\u540c\u7684\u986f\u793a\u6548\u679c\u3002\u4f7f\u7528SIN\u51fd\u6578\u662f\u5176\u4e2d\u4e00\u500b\u65b9\u5f0f\u3002\u900f\u904eSIN\u51fd\u6578\u4f86\u6c7a\u5b9a\u986f\u793a\u7684\u4eae\u5ea6\uff08\u5176\u5be6\u5c31\u662f\u8f38\u51fa\u7684\u503c\uff0c0\u662f\u6700\u5c0f\uff0c1023\u662f\u6700\u5927\uff09\uff0c SIN(0)~SIN(360)\u4e0d\u65b7\u5730\u8f38\u51fa\uff0c\u5c31\u53ef\u4ee5\u88fd\u4f5c\u51fa\u5982\u4e0b\u6240\u793a\u7684\u6548\u679c\uff1a<\/p>\n<p><iframe loading=\"lazy\" title=\"NodeMCU\u4f7f\u7528MicroPython\u88fd\u4f5c\u547c\u5438\u71c8\u6548\u679c\" width=\"640\" height=\"360\" src=\"https:\/\/www.youtube.com\/embed\/AsoRUUbTyww?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p>\u8981\u4f7f\u7528\u9019\u500b\u6548\u679c\u4e4b\u524d\uff0c\u8981\u5148\u78ba\u5b9a\u4f7f\u7528\u7684PIN\u8173\u8a2d\u5b9a\u4e0d\u518d\u662f\u539f\u4f86\u7684\u6578\u4f4d\u8f38\u51fa\u975e0\u53731\uff0c\u800c\u662f\u4f7f\u7528PWM\uff08Pulse Width Modulation\uff09\u6280\u8853\uff0c\u628a\u8f38\u51fa\u7684\u5167\u5bb9\u5f9e\u539f\u6709\u7684\u6578\u4f4d\u8a0a\u865f\u6539\u70ba\u4ee5\u6ce2\u5f62\u5bec\u5ea6\u6bd4\u7387\u7684\u65b9\u5f0f\u4f86\u6a21\u64ec\u985e\u6bd4\u8f38\u51fa\uff0c\u6839\u64da\u8aaa\u660e\u6587\u4ef6\uff0cNodeMCU\u53ef\u4ee5\u652f\u63f4PWM\u8f38\u51fa\u7684\u63a5\u8173\u70ba\u00a00, 2, 4, 5, 12, 13, 14 \u4ee5\u53ca15\uff0c\u800cD1, D2, D3\u5206\u5225\u5c0d\u61c9\u52305, 4, 0\uff0c\u90fd\u662f\u53ef\u4ee5\u652f\u63f4PWM\u7684\u63a5\u8173\uff0c\u56e0\u6b64\u6211\u5011\u5c31\u53ef\u4ee5\u5728\u4e0d\u7528\u6539\u8b8a\u9023\u63a5\u7dda\u7684\u60c5\u6cc1\u4e0b\uff0c\u900f\u904e\u4ee5\u4e0b\u7684\u7a0b\u5f0f\u4f86\u505a\u5230\u547c\u5438\u71c8\u7684\u6548\u679c\uff1a<\/p>\n<p>[code language=&#8221;python&#8221;]<br \/>\nfrom machine import Pin, PWM<br \/>\nimport time, math<\/p>\n<p>ledR = PWM(Pin(5), freq=1000)<br \/>\nledG = PWM(Pin(4), freq=1000)<br \/>\nledB = PWM(Pin(0), freq=1000)<br \/>\nled = [ledR, ledG, ledB]<\/p>\n<p>while True:<br \/>\n    for d in range(360):<br \/>\n        for l in led:<br \/>\n            l.duty(int(512*math.sin(math.pi*d\/180)+512))<br \/>\n        time.sleep_ms(2)<\/p>\n<p>[\/code]<\/p>\n<p>\u5728\u9019\u500b\u7a0b\u5f0f\u4e2d\uff0c\u6211\u5011\u539f\u672c\u53ea\u4f7f\u7528Pin\u4f86\u8a2d\u5b9a\u63a5\u8173\uff0c\u73fe\u5728\u518d\u52a0\u4e0aPWM\uff0c\u6307\u5b9a\u9019\u4e9b\u63a5\u8173\u90fd\u9700\u8981\u4ee5PWM\u7684\u65b9\u5f0f\u8f38\u51fa\u3002PWM\u8f38\u51fa\u8a2d\u5b9a\u9700\u8981\u6307\u5b9a2\u500b\u90e8\u4efd\uff0c\u5176\u4e2d\u4e00\u500b\u90e8\u4efd\u662f\u983b\u7387\uff0c\u4ee5freq\u4f86\u8a2d\u5b9a\uff0c\u5728\u9019\u88e1\u6211\u5011\u8a2d\u5b9a\u70ba1000\uff0c\u4e5f\u5c31\u662f1KHz\uff0c\u4e00\u958b\u59cb\u5148\u8a2d\u5b9a\u983b\u7387\uff0c\u63a5\u4e0b\u4f86\u5c31\u53ef\u4ee5\u4f7f\u7528duty\u9019\u500b\u51fd\u6578\u4f86\u8a2d\u5b9a\u8f38\u51fa\u7684\u503c\uff0c0\u662f\u5b8c\u5168\u6c92\u6709\u8f38\u51fa\uff0c\u800c1023\u5247\u6700\u5927\u7684\u8f38\u51fa\u3002<\/p>\n<p>\u5728\u7b2c10\u884c\uff0c\u6211\u5011\u8a2d\u5b9a\u8b93d\u9019\u500b\u8b8a\u6578\u53ef\u4ee5\u5f9e0\u8b8a\u5316\u5230359\u5ea6\uff0c\u7136\u5f8c\u5728\u7b2c12\u884c\u7684\u5730\u65b9\uff0c\u900f\u904emath.sin\u9019\u500b\u51fd\u6578\u4f86\u8a08\u7b97\u4e09\u89d2\u51fd\u6578\u7684SIN\u51fd\u6578\u503c\u3002\u4e0d\u904e\uff0c\u7531\u65bcPython\u4f7f\u7528\u7684\u662f\u5f33\u5ea6\uff0c\u56e0\u6b64\u8981\u4f7f\u7528math.pi*d\/180\u4f86\u505a\u8f49\u8208\u3002\u6b64\u5916\uff0c\u8f38\u51fa\u503c\u53ea\u80fd\u662f\u6b63\u6578\uff0c\u4f46SIN\u51fd\u6578\u7684\u503c\u662f-1\u5230+1\uff0c\u56e0\u6b64\u6211\u5011\u53ea\u4f7f\u7528512\u53bb\u4e58SIN\u51fd\u6578\u7684\u503c\uff0c\u51fa\u4f86\u7684\u7d50\u679c\u518d\u52a0\u4e0a512\uff0c\u628a\u5b83\u5011\u7684\u503c\u5f9e-511~511\u8abf\u6574\u52300\u52301022\uff0c\u6700\u5f8c\uff0c\u8f38\u51fa\u7684\u503c\u9700\u8981\u662f\u6574\u6578\uff0c\u56e0\u6b64\u518d\u628a\u6574\u500b\u8a08\u7b97\u51fa\u4f86\u7684\u503c\u4ee5int\u51fd\u6578\u4f86\u8b8a\u6210\u6574\u6578\u578b\u614b\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n<p>\u6700\u5f8c\uff0c\u539f\u672c\u7684time.sleep\u6539\u70batime.sleep_ms\uff0c\u4ee5\u6beb\u79d2\u70ba\u55ae\u4f4d\uff0c\u4ee5\u907f\u514d\u6578\u503c\u7684\u8b8a\u63db\u901f\u5ea6\u904e\u6162\u3002Python\u7684math\u5957\u4ef6\u4e2d\u9084\u6709\u8a31\u591a\u7684\u51fd\u6578\u53ef\u4ee5\u4f7f\u7528\uff0c\u6709\u8208\u8da3\u7684\u540c\u5b78\u5011\u4e5f\u53ef\u4ee5\u8a66\u8a66\u770b\u5594\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9023\u63a5\u7dda\u7684\u65b9\u5f0f\u548c\u4e0a\u4e00\u500b\u7df4\u7fd2\u76f8\u540c\uff0c\u4f46\u662f\u6211\u5011\u628a\u7a0b\u5f0f\u4fee\u6539\u4e00\u4e0b\uff0c\u5c31\u53ef\u4ee5\u5f97\u5230\u8a31\u591a\u4e0d\u540c\u7684\u986f\u793a\u6548\u679c\u3002\u4f7f\u7528SIN\u51fd\u6578\u662f\u5176\u4e2d\u4e00\u500b\u65b9\u5f0f\u3002\u900f\u904eSIN\u51fd\u6578\u4f86\u6c7a\u5b9a\u986f\u793a\u7684\u4eae\u5ea6\uff08\u5176\u5be6\u5c31\u662f\u8f38\u51fa\u7684\u503c\uff0c0\u662f\u6700\u5c0f\uff0c1023\u662f\u6700\u5927\uff09\uff0c SIN(0)~SIN(360)\u4e0d\u65b7\u5730\u8f38\u51fa\uff0c\u5c31\u53ef\u4ee5\u88fd\u4f5c\u51fa\u5982\u4e0b\u6240\u793a\u7684\u6548\u679c\uff1a<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[13],"tags":[44,159,198,199],"class_list":["post-2814","post","type-post","status-publish","format-standard","hentry","category-nodemcu","tag-micropython","tag-pwm","tag-led","tag-199"],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/2814","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/comments?post=2814"}],"version-history":[{"count":0,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/2814\/revisions"}],"wp:attachment":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/media?parent=2814"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/categories?post=2814"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/tags?post=2814"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}