Arduino 使用光中断的正交编码器
我试图用光中断和7474D触发器编程编码器。我相信我的接线是正确的。它只是将photo中断连接到arduino,并将引脚输出穿过7474。还有一根导线连接到引脚4以进行数字读取 下面是我的代码。由于某种原因,我无法通过照片中断来读取车轮的转动Arduino 使用光中断的正交编码器,arduino,Arduino,我试图用光中断和7474D触发器编程编码器。我相信我的接线是正确的。它只是将photo中断连接到arduino,并将引脚输出穿过7474。还有一根导线连接到引脚4以进行数字读取 下面是我的代码。由于某种原因,我无法通过照片中断来读取车轮的转动 const int clock = 0; //pin 2 is interrupt 0 const int dirPin = 4; //the number of the LED pin //const int ledPin = 13;
const int clock = 0; //pin 2 is interrupt 0
const int dirPin = 4; //the number of the LED pin
//const int ledPin = 13;
int count = 0;
int dir = 0;
//int clockA = 0;
void setup(){
pinMode(dirPin, INPUT);
Serial.begin(9600);
attachInterrupt(clock, program, RISING);
}
void loop()
{
delay(50);
}
void program()
{
dir = digitalRead(dirPin);
if (dir == HIGH)
{
count ++;
}
else
{
count --;
}
Serial.println(count*30);
}
不要将Serial.println放入中断处理程序中。在处理程序中,其他中断被禁用。尽可能少地工作,然后返回。处理程序应该只更新计数,并可以标记一个标志来通知主循环进行处理
int count = 0;
int newcounts = 0;
void loop() {
delay(100);
if (newcounts) {
Serial.println(count*30);
newcounts = 0
}
}
void program() {
// ... update count
newcounts = 1;
}
请不要只是发布arduino游乐场正交编码器网站。我不知道为什么这些程序不能工作。我在7474的引脚2和引脚4有电线,我在D和clkYou不说硬件。更新问题以指示Arduino板类型。attachInterrupt在到期时是不同的。