ATtiny1616搭載オリジナルボードでエンコーダーモーターを動かす

ATtiny1616でエンコーダーモーターを動かす２の続きです。
https://qiita.com/usashirou/items/4dc1c7c28c142e90bd5b
今回は、ATtiny1616を使用したオリジナルボードを製作し、これを動かしていきます。
#ボード全容
![DSC_0487 (2).JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/285344/fe18bdf1-f766-b319-2820-3b5c5f1600ea.jpeg)
#回路図
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/285344/bda6e619-71f0-7a6a-7ebc-25da28bced61.png)
#プログラム
前回同様ArduinoTutorialsのプログラムを修正して製作しています。
https://github.com/curiores/ArduinoTutorials/blob/master/encoderControl/part3/part3.ino

重要なのはattachInterruptを入れる必要がある事です。

    attachInterrupt(digitalPinToInterrupt(ENCA), readEncoderA, RISING);
    attachInterrupt(digitalPinToInterrupt(ENCC), readEncoderB, RISING);

```
#define ENCA 2 // EncoderA
#define ENCB 3 // EncoderA
#define ENCC 14 // EncoderB
#define ENCD 15 // EncoderB
#define AIN1 12 // MotorA
#define AIN2 13 // MotorA
#define BIN1 10 // MotorB
#define BIN2 11 // MotorB
#define PWMA 0 // MotorA
#define PWMB 1 // MotorB
#define ADC 5 // ADC

int posA = 0;
int posB = 0;
int sensorValue = 0;        // value read from the pot

void setup() {
  Serial.begin(9600);

  pinMode(ENCA, INPUT);
  pinMode(ENCB, INPUT);
  pinMode(ENCC, INPUT);
  pinMode(ENCD, INPUT);

  attachInterrupt(digitalPinToInterrupt(ENCA), readEncoderA, RISING);
  attachInterrupt(digitalPinToInterrupt(ENCC), readEncoderB, RISING);
}

void loop() {
  setMotor(1, 150, AIN1, AIN2);
  delay(2000);
  Serial.print("EncoderA = ");
  Serial.println(posA);
  Serial.print("\r\n");
  Serial.print("EncoderB = ");
  Serial.println(posB);
  Serial.print("\r\n");
  sensorValue = analogRead(ADC);
  Serial.print("sensor = ");
  Serial.print(sensorValue);
  Serial.print("\r\n");
  setMotor(1, 250, AIN1, AIN2);
  delay(2000);
  Serial.print("EncoderA = ");
  Serial.println(posA);
  Serial.print("\r\n");
  Serial.print("EncoderB = ");
  Serial.println(posB);
  Serial.print("\r\n");
  sensorValue = analogRead(ADC);
  Serial.print("sensor = ");
  Serial.print(sensorValue);
  Serial.print("\r\n");
  setMotor(1, 350, AIN1, AIN2);
  delay(2000);
  Serial.print("EncoderA = ");
  Serial.println(posA);
  Serial.print("\r\n");
  Serial.print("EncoderB = ");
  Serial.println(posB);
  Serial.print("\r\n");
  sensorValue = analogRead(ADC);
  Serial.print("sensor = ");
  Serial.print(sensorValue);
  Serial.print("\r\n");
  setMotor(0, 0, AIN1, AIN2);
  Serial.print("EncoderA = ");
  Serial.println(posA);
  Serial.print("\r\n");
  Serial.print("EncoderB = ");
  Serial.println(posB);
  Serial.print("\r\n");
  sensorValue = analogRead(ADC);
  Serial.print("sensor = ");
  Serial.print(sensorValue);
  Serial.print("\r\n");
  delay(500);
  setMotor(-1, 350, AIN1, AIN2);
  delay(2000);
  Serial.print("EncoderA = ");
  Serial.println(posA);
  Serial.print("\r\n");
  Serial.print("EncoderB = ");
  Serial.println(posB);
  Serial.print("\r\n");
  sensorValue = analogRead(ADC);
  Serial.print("sensor = ");
  Serial.print(sensorValue);
  Serial.print("\r\n");
  setMotor(0, 0, AIN1, AIN2);
  Serial.print("EncoderA = ");
  Serial.println(posA);
  Serial.print("\r\n");
  Serial.print("EncoderB = ");
  Serial.println(posB);
  Serial.print("\r\n");
  sensorValue = analogRead(ADC);
  Serial.print("sensor = ");
  Serial.print(sensorValue);
  Serial.print("\r\n");
  delay(1500);
  Serial.print("EncoderA = ");
  Serial.println(posA);
  Serial.print("\r\n");
  Serial.print("EncoderB = ");
  Serial.println(posB);
  Serial.print("\r\n");
  sensorValue = analogRead(ADC);
  Serial.print("sensor = ");
  Serial.print(sensorValue);
  Serial.print("\r\n");
}

void setMotor(int dir, int pwmVal,  int in1, int in2) {
  if (dir == 1) {
    analogWrite(PWMA, pwmVal);
    digitalWrite(AIN1, HIGH);
    digitalWrite(AIN2, LOW);
    analogWrite(PWMB, pwmVal);
    digitalWrite(BIN1, HIGH);
    digitalWrite(BIN2, LOW);
  }
  else if (dir == -1) {
    analogWrite(PWMA, pwmVal);
    digitalWrite(AIN1, LOW);
    digitalWrite(AIN2, HIGH);
    analogWrite(PWMB, pwmVal);
    digitalWrite(BIN1, LOW);
    digitalWrite(BIN2, HIGH);
  }
  else {
    analogWrite(PWMA, pwmVal);
    digitalWrite(AIN1, LOW);
    digitalWrite(AIN2, LOW);
    analogWrite(PWMB, pwmVal);
    digitalWrite(BIN1, LOW);
    digitalWrite(BIN2, LOW);
  }
}

void readEncoderA() {
  int a = digitalRead(ENCB);
  if (a > 0) {
    posA++;
  }
  else {
    posA--;
  }
}

void readEncoderB() {
  int b = digitalRead(ENCC);
  if (b > 0) {
    posB++;
  }
  else {
    posB--;
  }
}
```
#出力
シリアル出力を見てみましょう
![スクリーンショット 2021-07-18 175105.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/285344/9bbcff26-d2c4-3463-32bd-babc3d06865d.png)