4桁7セグメントLEDを使う

4桁7セグメントLEDは仕組みとしては、7セグメントLEDと同じ
6、8、9、12番ピンを指定するとそれぞれの桁が表示される
各桁を0.01秒ずつ順番に表示しているが、目視すると同時に表示されているように見える

#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番ピンをタクトスイッチ
#define DIGIT_1 11
#define DIGIT_10 12
#define DIGIT_100 13
#define DIGIT_1000 14
#define DP 15 // ドット

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

void led_output(byte num){

  switch(num) {
      case 0:  // 4桁7セグメントではアノードコモンを使用しており、7セグメントLEDの逆
      digitalWrite(TERMINAL_A, LOW);
      digitalWrite(TERMINAL_B, LOW);
      digitalWrite(TERMINAL_C, LOW);
      digitalWrite(TERMINAL_D, HIGH);
      digitalWrite(TERMINAL_F, LOW);
      digitalWrite(TERMINAL_G, LOW);
      digitalWrite(TERMINAL_H, LOW);
      break;
      case 1:
      digitalWrite(TERMINAL_A, LOW);
      digitalWrite(TERMINAL_B, HIGH);
      digitalWrite(TERMINAL_C, HIGH);
      digitalWrite(TERMINAL_D, HIGH);
      digitalWrite(TERMINAL_F, LOW);
      digitalWrite(TERMINAL_G, HIGH);
      digitalWrite(TERMINAL_H, HIGH);
      break;
      case 2:
      digitalWrite(TERMINAL_A, LOW);
      digitalWrite(TERMINAL_B, LOW);
      digitalWrite(TERMINAL_C, HIGH);
      digitalWrite(TERMINAL_D, LOW);
      digitalWrite(TERMINAL_F, HIGH);
      digitalWrite(TERMINAL_G, LOW);
      digitalWrite(TERMINAL_H, LOW);
      break;
      case 3:
      digitalWrite(TERMINAL_A, LOW);
      digitalWrite(TERMINAL_B, LOW);
      digitalWrite(TERMINAL_C, HIGH);
      digitalWrite(TERMINAL_D, LOW);
      digitalWrite(TERMINAL_F, LOW);
      digitalWrite(TERMINAL_G, LOW);
      digitalWrite(TERMINAL_H, HIGH);
      break;
      case 4:
      digitalWrite(TERMINAL_A, LOW);
      digitalWrite(TERMINAL_B, HIGH);
      digitalWrite(TERMINAL_C, LOW);
      digitalWrite(TERMINAL_D, LOW);
      digitalWrite(TERMINAL_F, LOW);
      digitalWrite(TERMINAL_G, HIGH);
      digitalWrite(TERMINAL_H, HIGH);
      break;
      case 5:
      digitalWrite(TERMINAL_A, HIGH);
      digitalWrite(TERMINAL_B, LOW);
      digitalWrite(TERMINAL_C, LOW);
      digitalWrite(TERMINAL_D, LOW);
      digitalWrite(TERMINAL_F, LOW);
      digitalWrite(TERMINAL_G, LOW);
      digitalWrite(TERMINAL_H, HIGH);
      break;
      case 6:
      digitalWrite(TERMINAL_A, HIGH);
      digitalWrite(TERMINAL_B, LOW);
      digitalWrite(TERMINAL_C, LOW);
      digitalWrite(TERMINAL_D, LOW);
      digitalWrite(TERMINAL_F, LOW);
      digitalWrite(TERMINAL_G, LOW);
      digitalWrite(TERMINAL_H, LOW);
      break;
      case 7:
      digitalWrite(TERMINAL_A, LOW);
      digitalWrite(TERMINAL_B, LOW);
      digitalWrite(TERMINAL_C, HIGH);
      digitalWrite(TERMINAL_D, HIGH);
      digitalWrite(TERMINAL_F, LOW);
      digitalWrite(TERMINAL_G, HIGH);
      digitalWrite(TERMINAL_H, HIGH);
      break;
      case 8:
      digitalWrite(TERMINAL_A, LOW);
      digitalWrite(TERMINAL_B, LOW);
      digitalWrite(TERMINAL_C, LOW);
      digitalWrite(TERMINAL_D, LOW);
      digitalWrite(TERMINAL_F, LOW);
      digitalWrite(TERMINAL_G, LOW);
      digitalWrite(TERMINAL_H, LOW);
      break;
      case 9:
      digitalWrite(TERMINAL_A, LOW);
      digitalWrite(TERMINAL_B, LOW);
      digitalWrite(TERMINAL_C, LOW);
      digitalWrite(TERMINAL_D, LOW);
      digitalWrite(TERMINAL_F, LOW);
      digitalWrite(TERMINAL_G, LOW);
      digitalWrite(TERMINAL_H, HIGH);
      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(DP, OUTPUT);

  pinMode(DIGIT_1, OUTPUT);
  pinMode(DIGIT_10, OUTPUT);
  pinMode(DIGIT_100, OUTPUT);
  pinMode(DIGIT_1000, OUTPUT);
  
  pinMode(TACT_SW, INPUT); // タクトスイッチに接続した10番ピンをインプットに設定

  digitalWrite(DIGIT_1, LOW);
  digitalWrite(DIGIT_10, LOW);
  digitalWrite(DIGIT_100, LOW);
  digitalWrite(DIGIT_1000, LOW);

  led_output(counter);
}

void clear_num(void){
  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);
  digitalWrite(DP, HIGH);
}

void loop() {

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

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

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

  num = counter/ 1000;
  clear_num();
  digitalWrite(DIGIT_1000, HIGH);
  delay(1);
  led_output(num);
  delay(5);
  digitalWrite(DIGIT_1000, LOW);

  num = counter % 1000;
  num = counter/ 100;
  clear_num();
  digitalWrite(DIGIT_100, HIGH);
  delay(1);
  led_output(num);
  delay(5);
  digitalWrite(DIGIT_100, LOW);

  num = counter % 100;
  num = counter/ 10;
  clear_num();
  digitalWrite(DIGIT_10, HIGH);
  delay(1);
  led_output(num);
  delay(5);
  digitalWrite(DIGIT_10, LOW);

  num = counter % 10;
  clear_num();
  digitalWrite(DIGIT_1, HIGH);
  delay(1);
  led_output(num);
  delay(5);
  digitalWrite(DIGIT_1, LOW);
  
  state_old = state;

}

配線

実行

dav

あれ、全然意図した動きになってない
配線が違う
4桁7セグメントLEDのピンに合わせて配線を修正します。
また、プログラムのアノードカソードの箇所も修正を加えます。

mde

デジタル時計が何やってるか大体わかった。
なるほどね、ハードウェアとマイコンとプログラムってことか。世界観変わったわ。