完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
我正在尝试采用下面的草图与我的 WeMos D1 mini 和众所周知的旋转编码器 KY-040 一起使用,但由于某种原因它不起作用,但相同的草图在 Arduino Uno 上运行良好(与不同的引脚分配)。
有人能告诉我草图有什么问题吗? 谢谢你! 代码:全选const int PinA = 14; // Used for generating interrupts using CLK signal const int PinB = 12; // Used for reading DT signal const int PinSW = 13; // Used for the push button switch int lCnt = 0; // Keep track of last rotary value volatile int vPos = 0; // Updated by the ISR (Interrupt Service Routine) void isr_event () { static unsigned long lIsrTmr = 0; // Last Interrupt time unsigned long IsrTmr = millis(); // Interrupt time // If interrupts come faster than 5ms, assume it's a bounce and ignore if (IsrTmr - lIsrTmr > 5) { if (digitalRead(PinB) == LOW) { vPos++ ; // Could be +5 or +10 } else { vPos-- ; // Could be -5 or -10 } // Restrict value from 0 to +100 vPos = min(10, max(0, vPos)); // Keep track of when we were here last (no more than every 5ms) lIsrTmr = IsrTmr; } } void setup() { Serial.begin(9600); // Rotary pulses are INPUTs pinMode(PinA, INPUT); pinMode(PinB, INPUT); pinMode(PinSW, INPUT_PULLUP); // Switch is floating so use the in-built PULLUP so we don't need a resistor attachInterrupt(digitalPinToInterrupt(PinA), isr_event, LOW); // Attach the routine to service the interrupts Serial.println(F("Starting...")); // Ready to go! } void loop() { // Is someone pressing the rotary switch? if ((!digitalRead(PinSW))) { vPos = 0; while (!digitalRead(PinSW)) delay(10); } // If the current rotary switch position has changed then update everything if (vPos != lCnt) { // Write out to serial monitor the value and direction Serial.println(vPos); // Keep track of this new value lCnt = vPos ; } } |
|
相关推荐
|
|
只有小组成员才能发言,加入小组>>
358 浏览 0 评论
1202 浏览 1 评论
591浏览 6评论
485浏览 5评论
有没有办法在不使用混杂模式的情况下实现Wifi驱动程序接收缓冲区访问中断呢?
468浏览 5评论
467浏览 4评论
445浏览 4评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-31 01:09 , Processed in 0.814278 second(s), Total 73, Slave 57 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号