360度PWM旋转编码器突破

      360度PWM旋转编码器突破

      USD
      Model #
      Quantity
      qty limit cart limit

      运输成本估算器

      Note: You must have items in your cart already before you can see the estimated costs to ship your cart. This shipping tool will estimate the total shipping cost for your entire basket.

      描述

      这种类型的开关是增量式旋转编码器。实际上,由于它的低成本和提供信号的能力,它是所有旋转编码器中使用最广泛的,这些信号可以轻松地解释为提供诸如速度之类的运动相关信息。

      通过旋转,旋转编码器可以在旋转期间对正向和反向的输出脉冲频率进行计数,这与旋转电位计计数器不同,没有计数限制。使用旋转编码器上的按钮,可以将其从零开始重置为初始状态。

      测试代码

      int redPin = 2;
      int yellowPin = 3;
      int greenPin = 4;
      int aPin = 6;
      int bPin = 7;
      int buttonPin = 5;
      int状态= 0;
      int longPeriod = 5000; //时间为绿色或红色
      int shortPeriod = 700; //更改时的时间段
      int targetCount = shortPeriod;
      int count = 0;
      无效setup()
      {
      pinMode(aPin,INPUT);
      pinMode(bPin,INPUT);
      pinMode(buttonPin,INPUT);
      pinMode(redPin,OUTPUT);
      pinMode(yellowPin,输出);
      pinMode(绿色引脚,输出);
      }
      无效循环()
      {
      数++;
      如果(digitalRead(buttonPin))
        {
      setLights(HIGH,HIGH,HIGH);
        }
      其他
        {
      int change = getEncoderTurn();
      int newPeriod = longPeriod +(更改* 1000);
      如果(newPeriod> = 1000 && newPeriod <= 10000)
          {
      longPeriod = newPeriod;
          }
      如果(count> targetCount)
          {
      setState();
      计数= 0;
          }
        }
      delay(1);
      }
      int getEncoderTurn()
      {

      //返回-1、0或+1
      static int oldA = LOW;
      static int oldB = LOW;
      int结果= 0;
      int newA = digitalRead(aPin);
      int newB = digitalRead(bPin);
      如果(newA!= oldA || newB!= oldB)
        {
      //发生了一些变化
      如果(oldA == LOW && newA == HIGH)
          {
      结果=-(oldB * 2-1);
          }
        }
      oldA = newA;
      oldB = newB;
      返回结果;
      }
      int setState()
        {
      如果(状态== 0)
          {
      setLights(HIGH,LOW,LOW);
      targetCount = longPeriod;
      状态= 1;
          }
      否则(状态== 1)
          {
      setLights(HIGH,HIGH,LOW);
      targetCount = shortPeriod;
      状态= 2;
          }
      否则(状态== 2)
          {
      setLights(LOW,LOW,HIGH);
      targetCount = longPeriod;
      状态= 3;
          }
      否则,如果(state == 3)
          {
      setLights(LOW,HIGH,LOW);
      targetCount = shortPeriod;
      状态= 0;
          }
        }
      无效setLights(int红色,int黄色,int green)
      {
      digitalWrite(redPin,红色);
      digitalWrite(黄色针,黄色);
      digitalWrite(greenPin,绿色);