360도 PWM 로터리 엔코더 브레이크 아웃
360도 PWM 로터리 엔코더 브레이크 아웃
기술
이 유형의 스위치는 증 분식 로터리 엔코더입니다. 실제로 속도와 같은 동작 관련 정보를 제공하기 위해 쉽게 해석 할 수있는 신호를 제공하는 능력과 저렴한 비용으로 인해 모든 로터리 엔코더 중에서 가장 널리 사용됩니다.
회전을 통해 회전식 엔코더는 제한없이 회전식 전위차계 카운터와 달리 회전 중에 정방향 및 역방향으로 출력 펄스 주파수를 계산할 수 있습니다. 로터리 엔코더의 버튼을 사용하여 0부터 시작하는 초기 상태로 재설정 할 수 있습니다.
코드 테스트
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;
정수 개수 = 0;
무효 설정 ()
{
pinMode (aPin, INPUT);
pinMode (bPin, INPUT);
pinMode (buttonPin, INPUT);
pinMode (redPin, OUTPUT);
pinMode (yellowPin, OUTPUT);
pinMode (greenPin, OUTPUT);
}
무효 루프 ()
{
count ++;
if (digitalRead (buttonPin))
{
setLights (HIGH, HIGH, HIGH);
}
그밖에
{
int 변경 = getEncoderTurn ();
int newPeriod = longPeriod + (변경 * 1000);
if (newPeriod> = 1000 && newPeriod <= 10000)
{
longPeriod = newPeriod;
}
if (count> targetCount)
{
setState ();
카운트 = 0;
}
}
지연 (1);
}
int getEncoderTurn ()
{
// -1, 0 또는 +1 반환
static int oldA = LOW;
static int oldB = LOW;
int 결과 = 0;
int newA = digitalRead (aPin);
int newB = digitalRead (bPin);
if (newA! = oldA || newB! = oldB)
{
// 무언가 변경되었습니다.
if (oldA == LOW && newA == HIGH)
{
결과 =-(oldB * 2-1);
}
}
oldA = newA;
oldB = newB;
반환 결과;
}
int setState ()
{
if (상태 == 0)
{
setLights (HIGH, LOW, LOW);
targetCount = longPeriod;
상태 = 1;
}
그렇지 않으면 (상태 == 1)
{
setLights (HIGH, HIGH, LOW);
targetCount = shortPeriod;
상태 = 2;
}
그렇지 않으면 (상태 == 2)
{
setLights (LOW, LOW, HIGH);
targetCount = longPeriod;
상태 = 3;
}
그렇지 않으면 (상태 == 3)
{
setLights (LOW, HIGH, LOW);
targetCount = shortPeriod;
상태 = 0;
}
}
void setLights (int red, int yellow, int green)
{
digitalWrite (redPin, 빨간색);
digitalWrite (yellowPin, 노란색);
digitalWrite (greenPin, 녹색);
}