// Arduino 7seg LED Demo for Arduino IDE ver.0011 // by Musashinodenpa #include // 要インストール #define ROTATIONDELAY 2000 // 7セグLED用フォント (PGFEDCBA) unsigned char font[16] = { 0b00111111, //0 0b00000110, //1 0b01011011, 0b01001111, 0b01100110, 0b01101101, 0b01111101, 0b00100111, 0b01111111, 0b01011111, //9 0b01011111, //A 0b01111100, 0b01011000, 0b00111110, 0b01111011, 0b01110001 //F }; // 回転アニメーション unsigned char rotate[6] = { 0b00000001, 0b00000010, 0b00000100, 0b00001000, 0b00010000, 0b00100000 }; unsigned char digit, ascii, rotationMode, pos; int incomingByte, counter; void setAnodes(byte pattern) { // アノードへ出力。高速化のためポートを直接叩いてます PORTD = (pattern << 2) | (PORTD & 0b00000011); PORTB = (pattern >> 6) | (PORTB & 0b11111100); } void displayHEX(void) { digitalWrite(10 + (digit % 2), LOW); if(digit % 2 == 1) { setAnodes(font[ascii%16]); // 下の桁 } else { setAnodes(font[ascii/16]); // 上の桁 } digitalWrite(10 + (++digit % 2), HIGH); } void displayRotate(void) { digitalWrite(10 + (digit % 2), LOW); setAnodes(rotate[pos]); digitalWrite(10 + (++digit % 2), HIGH); } void setup() { Serial.begin(9600); // タイマーを使用(FrequencyTimer2ライブラリはArduino0012では動作せず) FrequencyTimer2::disable(); // ピン11のトグリングは無効 FrequencyTimer2::setOnOverflow(displayHEX); FrequencyTimer2::setPeriod(5000); // 間隔(マイクロ秒) // pin 2から11までを7セグLEDに使用 // 2-8はアノード(9はdp用にリザーブ) int i; for (i=2; i<=10; i++) { pinMode(i, OUTPUT); } // 10と11がカソード(トランジスタに接続) digitalWrite(10, LOW); digitalWrite(11, LOW); } void loop() { if (Serial.available() > 0) { incomingByte = Serial.read(); if(incomingByte == 62) { // 62 = '>' の場合は右回転 FrequencyTimer2::setOnOverflow(displayRotate); rotationMode = 1; } else if(incomingByte == 60) { // 60 = '<'の場合は左回転 FrequencyTimer2::setOnOverflow(displayRotate); rotationMode = 2; } else { // シリアルで受けた文字のASCIIコードを表示 FrequencyTimer2::setOnOverflow(displayHEX); ascii = incomingByte; Serial.println(incomingByte, HEX); rotationMode = 0; } } // 回転動作 if(rotationMode == 1 && counter++ == ROTATIONDELAY) { counter = 0; if(++pos == 6) pos = 0; } else if(rotationMode == 2 && counter++ == ROTATIONDELAY) { counter = 0; if(pos-- == 0) pos = 5; } }