ArduinoでビットカウンタをLEDと7セグでつくってみました。
4桁の7セグと、8個のLEDなので普通に作ってしまうと、PINが足らないので、4桁の高速切り替え表示や、シフトレジスタを使うところポイントです。
あと超音波センサで測った距離も表示しようと思いましたが、今回はログ表示にとどめておきたいと思います。
7セグは高速で4桁表示しているので、ディレイなどを使って処理が遅れると表示が乱れてしまいます。このあたり意外と難しいと思いました。
参考:http://osoyoo.com/ja/2014/12/06/arduino-starter-kit/
ここのチュートリアルはとても参考になります。今回はkumanのボードですが、osoyooは他のブログでも使っています。
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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
int latchPin = 17; //Pin connected to Pin 12 of 74HC595 (Latch) int dataPin = 18; //Pin connected to Pin 14 of 74HC595 (Data) int clockPin = 19; //Pin connected to Pin 11 of 74HC595 (Clock) long cnt = 100; int a = 13; int b = 2; int c = 3; int d = 4; int e = 5; int f = 6; int g = 7; int p = 8; int d4 = 9; int d3 = 10; int d2 = 11; int d1 = 12; int x = 100; long del = 4000; int Trig = 16; // ana2 int Echo = 15; // ana1 int Duration; float Distance; void setup() { pinMode(latchPin, OUTPUT); pinMode(clockPin, OUTPUT); pinMode(dataPin, OUTPUT); Serial.begin(9600); pinMode(d1, OUTPUT); pinMode(d2, OUTPUT); pinMode(d3, OUTPUT); pinMode(d4, OUTPUT); pinMode(a, OUTPUT); pinMode(b, OUTPUT); pinMode(c, OUTPUT); pinMode(d, OUTPUT); pinMode(e, OUTPUT); pinMode(f, OUTPUT); pinMode(g, OUTPUT); pinMode(p, OUTPUT); pinMode(Trig,OUTPUT); pinMode(Echo,INPUT); } long old = 0; void loop() { long c = cnt / 100; if(old != c){ digitalWrite(latchPin, LOW); shiftOut(0); digitalWrite(latchPin, HIGH); //set latchPin low to allow data flow digitalWrite(latchPin, LOW); shiftOut(c % 256); //set latchPin to high to lock and send data digitalWrite(latchPin, HIGH); digitalWrite(Trig,LOW); delayMicroseconds(1); digitalWrite(Trig,HIGH); delayMicroseconds(11); digitalWrite(Trig,LOW); Duration = pulseIn(Echo,HIGH); if (Duration>0) { Distance = Duration/2; Distance = Distance*340*100/1000000; // ultrasonic speed is 340m/s = 34000cm/s = 0.034cm/us Serial.print(Duration); Serial.print(" us "); Serial.print(Distance); Serial.println(" cm"); } } showNum(cnt); if(old != c){ old = c; Serial.println(c); } cnt += 1; } void shiftOut(byte dataOut) { // Shift out 8 bits LSB first, on rising edge of clock boolean pinState; digitalWrite(dataPin, LOW); //clear shift register ready for sending data digitalWrite(clockPin, LOW); for (int i=0; i<=7; i++) { // for each bit in dataOut send out a bit digitalWrite(clockPin, LOW); //set clockPin to LOW prior to sending bit // if the value of DataOut and (logical AND) a bitmask // are true, set pinState to 1 (HIGH) if ( dataOut & (1<<i) ) { Serial.print(i); pinState = HIGH; } else { pinState = LOW; } //sets dataPin to HIGH or LOW depending on pinState digitalWrite(dataPin, pinState); //send bit out on rising edge of clock digitalWrite(clockPin, HIGH); } Serial.println(""); digitalWrite(clockPin, LOW); //stop shifting out data } void showNum(long n) { clearLEDs(); pickDigit(1); pickNumber((n/x/1000)%10); delayMicroseconds(del); clearLEDs(); pickDigit(2); pickNumber((n/x/100)%10); delayMicroseconds(del); clearLEDs(); pickDigit(3); dispDec(3); pickNumber((n/x/10)%10); delayMicroseconds(del); clearLEDs(); pickDigit(4); pickNumber(n/x%10); delayMicroseconds(del); } void pickDigit(int x) { digitalWrite(d1, HIGH); digitalWrite(d2, HIGH); digitalWrite(d3, HIGH); digitalWrite(d4, HIGH); switch(x) { case 1: digitalWrite(d1, LOW); break; case 2: digitalWrite(d2, LOW); break; case 3: digitalWrite(d3, LOW); break; default: digitalWrite(d4, LOW); break; } } void pickNumber(int x) { switch(x) { default: zero(); break; case 1: one(); break; case 2: two(); break; case 3: three(); break; case 4: four(); break; case 5: five(); break; case 6: six(); break; case 7: seven(); break; case 8: eight(); break; case 9: nine(); break; } } void dispDec(int x) { digitalWrite(p, HIGH); } void clearLEDs() { digitalWrite(a, LOW); digitalWrite(b, LOW); digitalWrite(c, LOW); digitalWrite(d, LOW); digitalWrite(e, LOW); digitalWrite(f, LOW); digitalWrite(g, LOW); digitalWrite(p, LOW); } void zero() { digitalWrite(a, HIGH); digitalWrite(b, HIGH); digitalWrite(c, HIGH); digitalWrite(d, HIGH); digitalWrite(e, HIGH); digitalWrite(f, HIGH); digitalWrite(g, LOW); } void one() { digitalWrite(a, LOW); digitalWrite(b, HIGH); digitalWrite(c, HIGH); digitalWrite(d, LOW); digitalWrite(e, LOW); digitalWrite(f, LOW); digitalWrite(g, LOW); } void two() { digitalWrite(a, HIGH); digitalWrite(b, HIGH); digitalWrite(c, LOW); digitalWrite(d, HIGH); digitalWrite(e, HIGH); digitalWrite(f, LOW); digitalWrite(g, HIGH); } void three() { digitalWrite(a, HIGH); digitalWrite(b, HIGH); digitalWrite(c, HIGH); digitalWrite(d, HIGH); digitalWrite(e, LOW); digitalWrite(f, LOW); digitalWrite(g, HIGH); } void four() { digitalWrite(a, LOW); digitalWrite(b, HIGH); digitalWrite(c, HIGH); digitalWrite(d, LOW); digitalWrite(e, LOW); digitalWrite(f, HIGH); digitalWrite(g, HIGH); } void five() { digitalWrite(a, HIGH); digitalWrite(b, LOW); digitalWrite(c, HIGH); digitalWrite(d, HIGH); digitalWrite(e, LOW); digitalWrite(f, HIGH); digitalWrite(g, HIGH); } void six() { digitalWrite(a, HIGH); digitalWrite(b, LOW); digitalWrite(c, HIGH); digitalWrite(d, HIGH); digitalWrite(e, HIGH); digitalWrite(f, HIGH); digitalWrite(g, HIGH); } void seven() { digitalWrite(a, HIGH); digitalWrite(b, HIGH); digitalWrite(c, HIGH); digitalWrite(d, LOW); digitalWrite(e, LOW); digitalWrite(f, LOW); digitalWrite(g, LOW); } void eight() { digitalWrite(a, HIGH); digitalWrite(b, HIGH); digitalWrite(c, HIGH); digitalWrite(d, HIGH); digitalWrite(e, HIGH); digitalWrite(f, HIGH); digitalWrite(g, HIGH); } void nine() { digitalWrite(a, HIGH); digitalWrite(b, HIGH); digitalWrite(c, HIGH); digitalWrite(d, HIGH); digitalWrite(e, LOW); digitalWrite(f, HIGH); digitalWrite(g, HIGH); } |
Arudinoはドキュメントが豊富なので(kumanのキットにはCD-ROMが付属)、パーツの使い方を学ぶのに最適ですね。