プログラム例)
//16進数を7セグメント表示器1個で表示する //2進数をLED4個で表示する //SW1(D8)で上昇,SW2(D9)で下降 //配列を使用LED→{8,4,2,1} //配列を使用7セグ→{a,b,c,d,e,f,g} int i,j; //整数型の宣言 int kazu = 1 ; int outpin ; int num0[]={1,1,1,1,1,1,0}; //配列の宣言 int num1[]={0,1,1,0,0,0,0}; int num2[]={1,1,0,1,1,0,1}; int num3[]={1,1,1,1,0,0,1}; int num4[]={0,1,1,0,0,1,1}; int num5[]={1,0,1,1,0,1,1}; int num6[]={1,0,1,1,1,1,1}; int num7[]={1,1,1,0,0,0,0}; int num8[]={1,1,1,1,1,1,1}; int num9[]={1,1,1,0,0,1,1}; int numa[]={1,1,1,0,1,1,1}; int numb[]={0,0,1,1,1,1,1}; int numc[]={1,0,0,1,1,1,0}; int numd[]={0,1,1,1,1,0,1}; int nume[]={1,0,0,1,1,1,1}; int numf[]={1,0,0,0,1,1,1}; int lamp0[]={0,0,0,0}; //配列の宣言 int lamp1[]={0,0,0,1}; int lamp2[]={0,0,1,0}; int lamp3[]={0,0,1,1}; int lamp4[]={0,1,0,0}; int lamp5[]={0,1,0,1}; int lamp6[]={0,1,1,0}; int lamp7[]={0,1,1,1}; int lamp8[]={1,0,0,0}; int lamp9[]={1,0,0,1}; int lampa[]={1,0,1,0}; int lampb[]={1,0,1,1}; int lampc[]={1,1,0,0}; int lampd[]={1,1,0,1}; int lampe[]={1,1,1,0}; int lampf[]={1,1,1,1}; void setup() { for (i=1;i<=7;i++){ pinMode(i,OUTPUT); //D1からD7までを出力に設定 } for (i=10;i<=13;i++){ //D10からD13までを出力に設定 pinMode(i,OUTPUT); } for (i=8;i<=9;i++){ //D8からD9までを入力に設定 pinMode(i,INPUT); } } void loop() { switch (kazu) { case 0: for (int i=1;i<=7;i++){ //「0」を表示。aからgまで7回繰り返し処理 if(num0[i-1]==1){ //「1」の時はそのセグメントを点灯させる digitalWrite(i,HIGH); } } j=3; outpin=10; for (int i=1;i<=4;i++){ //「0000」を表示 if(lamp0[j]==1){ digitalWrite(outpin,HIGH); } j--; outpin++; } break; case 1: for (int i=1;i<=7;i++){ //「1」を表示 if(num1[i-1]==1){ digitalWrite(i,HIGH); } } j=3; outpin=10; for (int i=1;i<=4;i++){ if(lamp1[j]==1){ digitalWrite(outpin,HIGH); } j--; outpin++; } break; case 2: for (int i=1;i<=7;i++){ //「2」を表示 if(num2[i-1]==1){ digitalWrite(i,HIGH); } } j=3; outpin=10; for (int i=1;i<=4;i++){ if(lamp2[j]==1){ digitalWrite(outpin,HIGH); } j--; outpin++; } break; case 3: for (int i=1;i<=7;i++){ //「3」を表示 if(num3[i-1]==1){ digitalWrite(i,HIGH); } } j=3; outpin=10; for (int i=1;i<=4;i++){ if(lamp3[j]==1){ digitalWrite(outpin,HIGH); } j--; outpin++; } break; case 4: for (int i=1;i<=7;i++){ //「4」を表示 if(num4[i-1]==1){ digitalWrite(i,HIGH); } } j=3; outpin=10; for (int i=1;i<=4;i++){ if(lamp4[j]==1){ digitalWrite(outpin,HIGH); } j--; outpin++; } break; case 5: for (int i=1;i<=7;i++){ //「5」を表示 if(num5[i-1]==1){ digitalWrite(i,HIGH); } } j=3; outpin=10; for (int i=1;i<=4;i++){ if(lamp5[j]==1){ digitalWrite(outpin,HIGH); } j--; outpin++; } break; case 6: for (int i=1;i<=7;i++){ //「6」を表示 if(num6[i-1]==1){ digitalWrite(i,HIGH); } } j=3; outpin=10; for (int i=1;i<=4;i++){ if(lamp6[j]==1){ digitalWrite(outpin,HIGH); } j--; outpin++; } break; case 7: for (int i=1;i<=7;i++){ //「7」を表示 if(num7[i-1]==1){ digitalWrite(i,HIGH); } } j=3; outpin=10; for (int i=1;i<=4;i++){ if(lamp7[j]==1){ digitalWrite(outpin,HIGH); } j--; outpin++; } break; case 8: for (int i=1;i<=7;i++){ //「8」を表示 if(num8[i-1]==1){ digitalWrite(i,HIGH); } } j=3; outpin=10; for (int i=1;i<=4;i++){ if(lamp8[j]==1){ digitalWrite(outpin,HIGH); } j--; outpin++; } break; case 9: for (int i=1;i<=7;i++){ //「9」を表示 if(num9[i-1]==1){ digitalWrite(i,HIGH); } } j=3; outpin=10; for (int i=1;i<=4;i++){ if(lamp9[j]==1){ digitalWrite(outpin,HIGH); } j--; outpin++; } break; case 10: for (int i=1;i<=7;i++){ //「A」を表示 if(numa[i-1]==1){ digitalWrite(i,HIGH); } } j=3; outpin=10; for (int i=1;i<=4;i++){ if(lampa[j]==1){ digitalWrite(outpin,HIGH); } j--; outpin++; } break; case 11: for (int i=1;i<=7;i++){ //「b」を表示 if(numb[i-1]==1){ digitalWrite(i,HIGH); } } j=3; outpin=10; for (int i=1;i<=4;i++){ if(lampb[j]==1){ digitalWrite(outpin,HIGH); } j--; outpin++; } break; case 12: for (int i=1;i<=7;i++){ //「C」を表示 if(numc[i-1]==1){ digitalWrite(i,HIGH); } } j=3; outpin=10; for (int i=1;i<=4;i++){ if(lampc[j]==1){ digitalWrite(outpin,HIGH); } j--; outpin++; } break; case 13: for (int i=1;i<=7;i++){ //「d」を表示 if(numd[i-1]==1){ digitalWrite(i,HIGH); } } j=3; outpin=10; for (int i=1;i<=4;i++){ if(lampd[j]==1){ digitalWrite(outpin,HIGH); } j--; outpin++; } break; case 14: for (int i=1;i<=7;i++){ //「E」を表示 if(nume[i-1]==1){ digitalWrite(i,HIGH); } } j=3; outpin=10; for (int i=1;i<=4;i++){ if(lampe[j]==1){ digitalWrite(outpin,HIGH); } j--; outpin++; } break; case 15: for (int i=1;i<=7;i++){ //「F」を表示 if(numf[i-1]==1){ digitalWrite(i,HIGH); } } j=3; outpin=10; for (int i=1;i<=4;i++){ if(lampf[j]==1){ digitalWrite(outpin,HIGH); } j--; outpin++; } break; } while(1){ if(digitalRead(8)==LOW){ //SW1が押されたら1増やす kazu++; if(kazu==16) kazu=0; break; } if(digitalRead(9)==LOW){ //SW2が押されたら1減らす kazu--; if(kazu==-1) kazu=15; break; } } for (int i=1;i<=7;i++){digitalWrite(i,LOW);} //7セグ消灯 for (int i=10;i<=13;i++){digitalWrite(i,LOW);} //LED消灯 delay(200); } //ここまで