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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#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の奥深さが魅力的になってきた。