/* 7セグメントLED1桁(8) ルーレット 押しボタンで動作 */ // 入出力ポート int PortSw = 13; // インターバル、ループ long Interval0 = 50; long Interval1 = 100; int Loop1 = 8; long Interval2 = 1000; int Loop2 = 5; /* 7セグメントLEDパターン 0 5 1 6 4 2 3 7 */ // LEDポート変換 int LedPort[] = {2,3,5,6,7,9,8,4}; // フォント // bit 76543210 int Font[] = {B00111111 //0 ,B00000110 //1 ,B01011011 //2 ,B01001111 //3 ,B01100110 //4 ,B01101101 //5 ,B01111101 //6 ,B00100111 //7 ,B01111111 //8 ,B01101111 //9 ,B00000001 //LED-A ,B00000010 //LED-B ,B00000100 //LED-C ,B00001000 //LED-D ,B00010000 //LED-E ,B00100000 //LED-F }; void setup() { // LEDポートを出力モードに for (int i = 0; i <= 7; i++){ pinMode(LedPort[i], OUTPUT); } // スイッチポートを入力モードに pinMode(PortSw, INPUT); // 乱数初期化 randomSeed(analogRead(0)); } void loop() { int x; int RoundLoop; int r; // ボタンが押されるまでぐるぐる回る RoundLoop = 10; while (digitalRead(PortSw) == HIGH) { x = LedNumberOn(RoundLoop); delay(Interval0); RoundLoop++; if (RoundLoop > 15) { RoundLoop = 10; } } // 数字を減速しながら表示 for (int i = 0; i <= Loop1; i++) { r = random(0, 10); x = LedNumberOn(r); delay(Interval1 * i); } // 数字を決定、点滅 r = random(0, 10); for (int i = 0; i <= Loop2; i++) { x = LedNumberOn(r); delay(Interval2); x = LedClr(); delay(Interval2); } } /* LED1桁数字表示関数 */ int LedNumberOn(int p){ int Mask = 1; for (int i = 0; i <= 7; i++){ if ((Font[p] & Mask) != 0){ digitalWrite(LedPort[i], HIGH); } else { digitalWrite(LedPort[i], LOW); } Mask = Mask * 2; } } /* LEDクリア関数 */ int LedClr(){ for (int i = 0; i <= 7; i++){ digitalWrite(LedPort[i], LOW); } }