Arduino UNOでタクトスイッチのカウンタ

#define TERMINAL_A 3
#define TERMINAL_B 4
#define TERMINAL_C 5
#define TERMINAL_D 6
#define TERMINAL_F 7
#define TERMINAL_G 8
#define TERMINAL_H 9
#define TACT_SW 10  // 10番ピンをタクトスイッチ

byte counter = 0;  // Arduinoで使用できる1byteの型
byte state = 0;
byte state_old = 0;

void led_output(byte num){

  switch(num) {
      case 0:
      digitalWrite(TERMINAL_A, HIGH);
      digitalWrite(TERMINAL_B, HIGH);
      digitalWrite(TERMINAL_C, HIGH);
      digitalWrite(TERMINAL_D, LOW);
      digitalWrite(TERMINAL_F, HIGH);
      digitalWrite(TERMINAL_G, HIGH);
      digitalWrite(TERMINAL_H, HIGH);
      break;
      case 1:
      digitalWrite(TERMINAL_A, HIGH);
      digitalWrite(TERMINAL_B, LOW);
      digitalWrite(TERMINAL_C, LOW);
      digitalWrite(TERMINAL_D, LOW);
      digitalWrite(TERMINAL_F, HIGH);
      digitalWrite(TERMINAL_G, LOW);
      digitalWrite(TERMINAL_H, LOW);
      break;
      case 2:
      digitalWrite(TERMINAL_A, HIGH);
      digitalWrite(TERMINAL_B, HIGH);
      digitalWrite(TERMINAL_C, LOW);
      digitalWrite(TERMINAL_D, HIGH);
      digitalWrite(TERMINAL_F, LOW);
      digitalWrite(TERMINAL_G, HIGH);
      digitalWrite(TERMINAL_H, HIGH);
      break;
      case 3:
      digitalWrite(TERMINAL_A, HIGH);
      digitalWrite(TERMINAL_B, HIGH);
      digitalWrite(TERMINAL_C, LOW);
      digitalWrite(TERMINAL_D, HIGH);
      digitalWrite(TERMINAL_F, HIGH);
      digitalWrite(TERMINAL_G, HIGH);
      digitalWrite(TERMINAL_H, LOW);
      break;
      case 4:
      digitalWrite(TERMINAL_A, HIGH);
      digitalWrite(TERMINAL_B, LOW);
      digitalWrite(TERMINAL_C, HIGH);
      digitalWrite(TERMINAL_D, HIGH);
      digitalWrite(TERMINAL_F, HIGH);
      digitalWrite(TERMINAL_G, LOW);
      digitalWrite(TERMINAL_H, LOW);
      break;
      case 5:
      digitalWrite(TERMINAL_A, LOW);
      digitalWrite(TERMINAL_B, HIGH);
      digitalWrite(TERMINAL_C, HIGH);
      digitalWrite(TERMINAL_D, HIGH);
      digitalWrite(TERMINAL_F, HIGH);
      digitalWrite(TERMINAL_G, HIGH);
      digitalWrite(TERMINAL_H, LOW);
      break;
      case 6:
      digitalWrite(TERMINAL_A, LOW);
      digitalWrite(TERMINAL_B, HIGH);
      digitalWrite(TERMINAL_C, HIGH);
      digitalWrite(TERMINAL_D, HIGH);
      digitalWrite(TERMINAL_F, HIGH);
      digitalWrite(TERMINAL_G, HIGH);
      digitalWrite(TERMINAL_H, HIGH);
      break;
      case 7:
      digitalWrite(TERMINAL_A, HIGH);
      digitalWrite(TERMINAL_B, HIGH);
      digitalWrite(TERMINAL_C, LOW);
      digitalWrite(TERMINAL_D, LOW);
      digitalWrite(TERMINAL_F, HIGH);
      digitalWrite(TERMINAL_G, LOW);
      digitalWrite(TERMINAL_H, LOW);
      break;
      case 8:
      digitalWrite(TERMINAL_A, HIGH);
      digitalWrite(TERMINAL_B, HIGH);
      digitalWrite(TERMINAL_C, HIGH);
      digitalWrite(TERMINAL_D, HIGH);
      digitalWrite(TERMINAL_F, HIGH);
      digitalWrite(TERMINAL_G, HIGH);
      digitalWrite(TERMINAL_H, HIGH);
      break;
      case 9:
      digitalWrite(TERMINAL_A, HIGH);
      digitalWrite(TERMINAL_B, HIGH);
      digitalWrite(TERMINAL_C, HIGH);
      digitalWrite(TERMINAL_D, HIGH);
      digitalWrite(TERMINAL_F, HIGH);
      digitalWrite(TERMINAL_G, HIGH);
      digitalWrite(TERMINAL_H, LOW);
      break;

      default:
      digitalWrite(TERMINAL_A, HIGH);
      digitalWrite(TERMINAL_B, HIGH);
      digitalWrite(TERMINAL_C, HIGH);
      digitalWrite(TERMINAL_D, LOW);
      digitalWrite(TERMINAL_F, HIGH);
      digitalWrite(TERMINAL_G, HIGH);
      digitalWrite(TERMINAL_H, HIGH);
      break;  
    }      
}



void setup() {
  // 初期化
  pinMode(TERMINAL_A, OUTPUT);
  pinMode(TERMINAL_B, OUTPUT);
  pinMode(TERMINAL_C, OUTPUT);
  pinMode(TERMINAL_D, OUTPUT);
  pinMode(TERMINAL_F, OUTPUT);
  pinMode(TERMINAL_G, OUTPUT);
  pinMode(TERMINAL_H, OUTPUT);
  pinMode(TACT_SW, INPUT); // タクトスイッチに接続した10番ピンをインプットに設定

  led_output(counter);
}

void loop() {
  state = digitalRead(TACT_SW);  // 電流が流れているとHIGH, 流れていないとLOW

  if((state == HIGH) && (state_old == LOW)){
      counter++;
  } else {
  }

  if(counter >= 10){
    counter = 0;
  }

  led_output(counter);
  state_old = state;

  delay(50);
  
}

配線

実行結果

mde

loopの箇所だが、タクトスイッチから手を離すと、state == LOWとなるので、state_oldもLOWになる。
配線は7セグメントLEDのGRD(-)がブレッドボードで一緒になっているのでややこしく見えるが、タクトスイッチのHIGH LOWは10番ピンで感知している。

Arduinoは触りだけやったら辞めてラズパイに時間使おうと思ってたけど、Arduinoの奥深さが魅力的になってきた。