プログラミング素人のArduino

技術屋の末端。プログラミングは専門外。 電気回路も専門外です。 コードに間違いなど見つけられたら、気軽にコメントください。 VC#始めました。

LED Cube 3x3x3 (ソース)

LED Cube 完成の本文は↓
http://ameblo.jp/sn348/entry-12183723558.html



/*
LED Cube のソース
*/

int l, m, n, i, j, k ;
int wait = 100 ;
int no;
void setup() {

int n = 1;
int i = 1;
for (i = 1; i < 14; i++) {
pinMode(i, OUTPUT);
}
pinMode(A0, OUTPUT);
pinMode(A1, OUTPUT);
pinMode(A2, OUTPUT);
}
void loop() {

int LED(int); //プロトタイプ宣言
void ptn5(int, int, int, int, int); //プロトタイプ宣言
void ptn8(int, int, int, int, int, int, int, int); //プロトタイプ宣言
void ptn9(int, int, int, int, int, int, int, int, int); //プロトタイプ宣言

wait = 120;
/* 順列 */
for (i = 1; i < 3; i++) {
for (j = 1; j < 28; j++) {
LED(j);
}
}

/* 螺旋 */
for (j = 1; j < 4; j++) {
ptn8(1, 2, 3, 6, 9, 8, 7, 4);
ptn8(10, 11, 12, 15, 18, 17, 16, 13);
ptn8(19, 20, 21, 24, 27, 26, 25, 22);
}

/* 花火 */
wait = 200;
for (j = 1; j < 10; j++) {
ptn8(j, j, j, j, j + 9, j + 18, j + 18, j + 18);
}

wait = 5;
/* blue */
for (i = 1; i < 3; i++) {
for (j = 1; j < 20; j++) {
ptn8(2, 4, 8, 6, 2, 4, 8, 6);
}
for (j = 1; j < 16; j++) {
ptn5(10, 16, 18, 12, 14);
}
for (j = 1; j < 20; j++) {
ptn8(20, 22, 26, 24, 20, 22, 26, 24);
}
}

/* white */
for (i = 1; i < 3; i++) {
for (j = 1; j < 16; j++) {
ptn5(1, 7, 9, 3, 5);
}
for (j = 1; j < 20; j++) {
ptn8(11, 13, 15, 17, 11, 13, 15, 17);
}
for (j = 1; j < 16; j++) {
ptn5(19, 21, 23, 25, 27);
}
}

/* All blue */
for (i = 1; i < 30; i++) {
ptn8(2, 4, 8, 6, 2, 4, 8, 6);
ptn5(10, 16, 18, 12, 14);
ptn8(20, 22, 26, 24, 20, 22, 26, 24);
}

/* All white */
for (i = 1; i < 30; i++) {
ptn5(1, 7, 9, 3, 5);
ptn8(11, 13, 15, 17, 11, 13, 15, 17);
ptn5(19, 21, 23, 25, 25);
}

wait = 60;
/* 螺旋 */
for (j = 1; j < 4; j++) {
ptn8(1, 2, 3, 6, 9, 8, 7, 4);
ptn8(10, 11, 12, 15, 18, 17, 16, 13);
ptn8(19, 20, 21, 24, 27, 26, 25, 22);
}

wait = 2;
/* 壁x */
for (i = 1; i < 40; i++) {
ptn8(24, 12, 18, 6, 24, 12, 18, 6);
ptn5(21, 27, 15, 3, 9);
}
for (i = 1; i < 40; i++) {
ptn8(23, 11, 17, 5, 23, 11, 17, 5);
ptn5(20, 26, 14, 2, 8);
}
for (i = 1; i < 40; i++) {
ptn8(22, 10, 16, 4, 22, 10, 16, 4);
ptn5(19, 25, 13, 1, 7);
}

wait = 2;
/* 壁y */
for (i = 1; i < 50; i++) {
ptn8(20, 12, 10, 2, 20, 12, 10, 2);
ptn5(19, 21, 11, 1, 3);
}
for (i = 1; i < 50; i++) {
ptn8(23, 13, 15, 5, 23, 13, 15, 5);
ptn5(22, 24, 14, 4, 6);
}
for (i = 1; i < 50; i++) {
ptn8(26, 18, 16, 8, 26, 18, 16, 8);
ptn5(27, 25, 17, 9, 7);
}

/* 絨毯 */
wait = 2;
for (k = 1; k < 3; k++)
for (j = 0; j < 4; j++) {
for (i = 1; i < 50; i++) {
ptn9(19 - j * 9, 20 - j * 9, 21 - j * 9, 22 - j * 9, 23 - j * 9, 24 - j * 9, 25 - j * 9, 26 - j * 9, 27 - j * 9);
}
}

/* 散歩 */
wait = 400;
ptn9(6, 15, 24, 23, 22, 13, 4, 7, 16);
ptn9(25, 26, 23, 14, 5, 6, 9, 18, 17);
ptn9(16, 13, 4, 1, 4, 5, 6, 15, 24);
ptn9(23, 22, 13, 4, 7, 8, 9, 6, 15);
ptn8(12, 11, 20, 23, 14, 17, 18, 15);

} //Loop End

/*LED 9連続Pattern*/
void ptn9(int p1, int p2, int p3, int p4, int p5, int p6, int p7, int p8, int p9) {
LED(p1);
LED(p2);
LED(p3);
LED(p4);
LED(p5);
LED(p6);
LED(p7);
LED(p8);
LED(p9);
}

/*LED 8連続Pattern*/
void ptn8(int p1, int p2, int p3, int p4, int p5, int p6, int p7, int p8) {
LED(p1);
LED(p2);
LED(p3);
LED(p4);
LED(p5);
LED(p6);
LED(p7);
LED(p8);
}

/*LED 5連続Pattern*/
void ptn5(int p1, int p2, int p3, int p4, int p5) {
LED(p1);
LED(p2);
LED(p3);
LED(p4);
LED(p5);
}

/*↓LEDを一つずつONするための関数*/
int LED(int no) {
if (no == 1) {
//1
digitalWrite(2, HIGH); //+ 消したいとき
digitalWrite(3, LOW); //+
digitalWrite(4, LOW); //+
digitalWrite(5, LOW); //-
digitalWrite(6, HIGH); //-
digitalWrite(8, HIGH); //-
digitalWrite(9, HIGH); //-
digitalWrite(10, HIGH); //-
digitalWrite(11, HIGH); //-
digitalWrite(A0, HIGH); //-
digitalWrite(A1, HIGH); //-
digitalWrite(A2, HIGH); //-
delay(wait);
return 0;
}
else if (no == 2) {
//2
digitalWrite(2, LOW); //+
digitalWrite(3, HIGH); //+
digitalWrite(4, LOW); //+
digitalWrite(5, LOW); //-
digitalWrite(6, HIGH); //-
digitalWrite(8, HIGH); //-
digitalWrite(9, HIGH); //-
digitalWrite(10, HIGH); //-
digitalWrite(11, HIGH); //-
digitalWrite(A0, HIGH); //-
digitalWrite(A1, HIGH); //-
digitalWrite(A2, HIGH); //-
delay(wait);
return 0;
}
else if (no == 3) {
digitalWrite(2, LOW); //+
digitalWrite(3, LOW); //+
digitalWrite(4, HIGH); //+
digitalWrite(5, LOW); //-
digitalWrite(6, LOW); //-
digitalWrite(8, HIGH); //-
digitalWrite(9, HIGH); //-
digitalWrite(10, HIGH); //-
digitalWrite(11, HIGH); //-
digitalWrite(A0, HIGH); //-
digitalWrite(A1, HIGH); //-
digitalWrite(A2, HIGH); //-
delay(wait);
return 0;
}

else if (no == 4) {
digitalWrite(2, HIGH); //+
digitalWrite(3, LOW); //+
digitalWrite(4, LOW); //+
digitalWrite(5, HIGH); //-
digitalWrite(6, LOW); //-
digitalWrite(8, HIGH); //-
digitalWrite(9, HIGH); //-
digitalWrite(10, HIGH); //-
digitalWrite(11, HIGH); //-
digitalWrite(A0, HIGH); //-
digitalWrite(A1, HIGH); //-
digitalWrite(A2, HIGH); //-
delay(wait);
return 0;
}

else if (no == 5) {
digitalWrite(2, LOW); //+
digitalWrite(3, HIGH); //+
digitalWrite(4, LOW); //+
digitalWrite(5, HIGH); //-
digitalWrite(6, LOW); //-
digitalWrite(8, HIGH); //-
digitalWrite(9, HIGH); //-
digitalWrite(10, HIGH); //-
digitalWrite(11, HIGH); //-
digitalWrite(A0, HIGH); //-
digitalWrite(A1, HIGH); //-
digitalWrite(A2, HIGH); //-
delay(wait);
return 0;
}

else if (no == 6) {
digitalWrite(2, LOW); //+
digitalWrite(3, LOW); //+
digitalWrite(4, HIGH); //+
digitalWrite(5, HIGH); //-
digitalWrite(6, LOW); //-
digitalWrite(8, HIGH); //-
digitalWrite(9, HIGH); //-
digitalWrite(10, HIGH); //-
digitalWrite(11, HIGH); //-
digitalWrite(A0, HIGH); //-
digitalWrite(A1, HIGH); //-
digitalWrite(A2, HIGH); //-
delay(wait);
return 0;
}

else if (no == 7) {
digitalWrite(2, HIGH); //+
digitalWrite(3, LOW); //+
digitalWrite(4, LOW); //+
digitalWrite(5, HIGH); //-
digitalWrite(6, HIGH); //-
digitalWrite(8, LOW); //-
digitalWrite(9, HIGH); //-
digitalWrite(10, HIGH); //-
digitalWrite(11, HIGH); //-
digitalWrite(A0, HIGH); //-
digitalWrite(A1, HIGH); //-
digitalWrite(A2, HIGH); //-
delay(wait);
return 0;
}

else if (no == 8) {
digitalWrite(2, LOW); //+
digitalWrite(3, HIGH); //+
digitalWrite(4, LOW); //+
digitalWrite(5, HIGH); //-
digitalWrite(6, HIGH); //-
digitalWrite(8, LOW); //-
digitalWrite(9, HIGH); //-
digitalWrite(10, HIGH); //-
digitalWrite(11, HIGH); //-
digitalWrite(A0, HIGH); //-
digitalWrite(A1, HIGH); //-
digitalWrite(A2, HIGH); //-
delay(wait);
return 0;
}

else if (no == 9) {
digitalWrite(2, LOW); //+
digitalWrite(3, LOW); //+
digitalWrite(4, HIGH); //+
digitalWrite(5, HIGH); //-
digitalWrite(6, HIGH); //-
digitalWrite(8, LOW); //-
digitalWrite(9, HIGH); //-
digitalWrite(10, HIGH); //-
digitalWrite(11, HIGH); //-
digitalWrite(A0, HIGH); //-
digitalWrite(A1, HIGH); //-
digitalWrite(A2, HIGH); //-
delay(wait);
return 0;
}

else if (no == 10) {
digitalWrite(2, HIGH); //+
digitalWrite(3, LOW); //+
digitalWrite(4, LOW); //+
digitalWrite(5, HIGH); //-
digitalWrite(6, HIGH); //-
digitalWrite(8, HIGH); //-
digitalWrite(9, LOW); //-
digitalWrite(10, HIGH); //-
digitalWrite(11, HIGH); //-
digitalWrite(A0, HIGH); //-
digitalWrite(A1, HIGH); //-
digitalWrite(A2, HIGH); //-
delay(wait);
return 0;
}

else if (no == 11) {
digitalWrite(2, LOW); //+
digitalWrite(3, HIGH); //+
digitalWrite(4, LOW); //+
digitalWrite(5, HIGH); //-
digitalWrite(6, HIGH); //-
digitalWrite(8, HIGH); //-
digitalWrite(9, LOW); //-
digitalWrite(10, HIGH); //-
digitalWrite(11, HIGH); //-
digitalWrite(A0, HIGH); //-
digitalWrite(A1, HIGH); //-
digitalWrite(A2, HIGH); //-
delay(wait);
return 0;
}

else if (no == 12) {
digitalWrite(2, LOW); //+
digitalWrite(3, LOW); //+
digitalWrite(4, HIGH); //+
digitalWrite(5, HIGH); //-
digitalWrite(6, HIGH); //-
digitalWrite(8, HIGH); //-
digitalWrite(9, LOW); //-
digitalWrite(10, HIGH); //-
digitalWrite(11, HIGH); //-
digitalWrite(A0, HIGH); //-
digitalWrite(A1, HIGH); //-
digitalWrite(A2, HIGH); //-
delay(wait);
return 0;
}

else if (no == 13) {
digitalWrite(2, HIGH); //+
digitalWrite(3, LOW); //+
digitalWrite(4, LOW); //+
digitalWrite(5, HIGH); //-
digitalWrite(6, HIGH); //-
digitalWrite(8, HIGH); //-
digitalWrite(9, HIGH); //-
digitalWrite(10, LOW); //-
digitalWrite(11, HIGH); //-
digitalWrite(A0, HIGH); //-
digitalWrite(A1, HIGH); //-
digitalWrite(A2, HIGH); //-
delay(wait);
return 0;
}

else if (no == 14) {
digitalWrite(2, LOW); //+
digitalWrite(3, HIGH); //+
digitalWrite(4, LOW); //+
digitalWrite(5, HIGH); //-
digitalWrite(6, HIGH); //-
digitalWrite(8, HIGH); //-
digitalWrite(9, HIGH); //-
digitalWrite(10, LOW); //-
digitalWrite(11, HIGH); //-
digitalWrite(A0, HIGH); //-
digitalWrite(A1, HIGH); //-
digitalWrite(A2, HIGH); //-
delay(wait);
return 0;
}

else if (no == 15) {
digitalWrite(2, LOW); //+
digitalWrite(3, LOW); //+
digitalWrite(4, HIGH); //+
digitalWrite(5, HIGH); //-
digitalWrite(6, HIGH); //-
digitalWrite(8, HIGH); //-
digitalWrite(9, HIGH); //-
digitalWrite(10, LOW); //-
digitalWrite(11, HIGH); //-
digitalWrite(A0, HIGH); //-
digitalWrite(A1, HIGH); //-
digitalWrite(A2, HIGH); //-
delay(wait);
return 0;
}

else if (no == 16) {
digitalWrite(2, HIGH); //+
digitalWrite(3, LOW); //+
digitalWrite(4, LOW); //+
digitalWrite(5, HIGH); //-
digitalWrite(6, HIGH); //-
digitalWrite(8, HIGH); //-
digitalWrite(9, HIGH); //-
digitalWrite(10, HIGH); //-
digitalWrite(11, LOW); //-
digitalWrite(A0, HIGH); //-
digitalWrite(A1, HIGH); //-
digitalWrite(A2, HIGH); //-
delay(wait);
return 0;
}

else if (no == 17) {
digitalWrite(2, LOW); //+
digitalWrite(3, HIGH); //+
digitalWrite(4, LOW); //+
digitalWrite(5, HIGH); //-
digitalWrite(6, HIGH); //-
digitalWrite(8, HIGH); //-
digitalWrite(9, HIGH); //-
digitalWrite(10, HIGH); //-
digitalWrite(11, LOW); //-
digitalWrite(A0, HIGH); //-
digitalWrite(A1, HIGH); //-
digitalWrite(A2, HIGH); //-
delay(wait);
return 0;
}

else if (no == 18) {
digitalWrite(2, LOW); //+
digitalWrite(3, LOW); //+
digitalWrite(4, HIGH); //+
digitalWrite(5, HIGH); //-
digitalWrite(6, HIGH); //-
digitalWrite(8, HIGH); //-
digitalWrite(9, HIGH); //-
digitalWrite(10, HIGH); //-
digitalWrite(11, LOW); //-
digitalWrite(A0, HIGH); //-
digitalWrite(A1, HIGH); //-
digitalWrite(A2, HIGH); //-
delay(wait);
return 0;
}

else if (no == 19) {
digitalWrite(2, HIGH); //+
digitalWrite(3, LOW); //+
digitalWrite(4, LOW); //+
digitalWrite(5, HIGH); //-
digitalWrite(6, HIGH); //-
digitalWrite(8, HIGH); //-
digitalWrite(9, HIGH); //-
digitalWrite(10, HIGH); //-
digitalWrite(11, HIGH); //-
digitalWrite(A0, LOW); //-
digitalWrite(A1, HIGH); //-
digitalWrite(A2, HIGH); //-
delay(wait);
return 0;
}
else if (no == 20) {
digitalWrite(2, LOW); //+
digitalWrite(3, HIGH); //+
digitalWrite(4, LOW); //+
digitalWrite(5, HIGH); //-
digitalWrite(6, HIGH); //-
digitalWrite(8, HIGH); //-
digitalWrite(9, HIGH); //-
digitalWrite(10, HIGH); //-
digitalWrite(11, HIGH); //-
digitalWrite(A0, LOW); //-
digitalWrite(A1, HIGH); //-
digitalWrite(A2, HIGH); //-
delay(wait);
return 0;
}
else if (no == 21) {
digitalWrite(2, LOW); //+
digitalWrite(3, LOW); //+
digitalWrite(4, HIGH); //+
digitalWrite(5, HIGH); //-
digitalWrite(6, HIGH); //-
digitalWrite(8, HIGH); //-
digitalWrite(9, HIGH); //-
digitalWrite(10, HIGH); //-
digitalWrite(11, HIGH); //-
digitalWrite(A0, LOW); //-
digitalWrite(A1, HIGH); //-
digitalWrite(A2, HIGH); //-
delay(wait);
return 0;
}
else if (no == 22) {
digitalWrite(2, HIGH); //+
digitalWrite(3, LOW); //+
digitalWrite(4, LOW); //+
digitalWrite(5, HIGH); //-
digitalWrite(6, HIGH); //-
digitalWrite(8, HIGH); //-
digitalWrite(9, HIGH); //-
digitalWrite(10, HIGH); //-
digitalWrite(11, HIGH); //-
digitalWrite(A0, HIGH); //-
digitalWrite(A1, LOW); //-
digitalWrite(A2, HIGH); //-
delay(wait);
return 0;
}
else if (no == 23) {
digitalWrite(2, LOW); //+
digitalWrite(3, HIGH); //+
digitalWrite(4, LOW); //+
digitalWrite(5, HIGH); //-
digitalWrite(6, HIGH); //-
digitalWrite(8, HIGH); //-
digitalWrite(9, HIGH); //-
digitalWrite(10, HIGH); //-
digitalWrite(11, HIGH); //-
digitalWrite(A0, HIGH); //-
digitalWrite(A1, LOW); //-
digitalWrite(A2, HIGH); //-
delay(wait);
return 0;
}
else if (no == 24) {
digitalWrite(2, LOW); //+
digitalWrite(3, LOW); //+
digitalWrite(4, HIGH); //+
digitalWrite(5, HIGH); //-
digitalWrite(6, HIGH); //-
digitalWrite(8, HIGH); //-
digitalWrite(9, HIGH); //-
digitalWrite(10, HIGH); //-
digitalWrite(11, HIGH); //-
digitalWrite(A0, HIGH); //-
digitalWrite(A1, LOW); //-
digitalWrite(A2, HIGH); //-
delay(wait);
return 0;
}
else if (no == 25) {
digitalWrite(2, HIGH); //+
digitalWrite(3, LOW); //+
digitalWrite(4, LOW); //+
digitalWrite(5, HIGH); //-
digitalWrite(6, HIGH); //-
digitalWrite(8, HIGH); //-
digitalWrite(9, HIGH); //-
digitalWrite(10, HIGH); //-
digitalWrite(11, HIGH); //-
digitalWrite(A0, HIGH); //-
digitalWrite(A1, HIGH); //-
digitalWrite(A2, LOW); //-
delay(wait);
return 0;
}
else if (no == 26) {
digitalWrite(2, LOW); //+
digitalWrite(3, HIGH); //+
digitalWrite(4, LOW); //+
digitalWrite(5, HIGH); //-
digitalWrite(6, HIGH); //-
digitalWrite(8, HIGH); //-
digitalWrite(9, HIGH); //-
digitalWrite(10, HIGH); //-
digitalWrite(11, HIGH); //-
digitalWrite(A0, HIGH); //-
digitalWrite(A1, HIGH); //-
digitalWrite(A2, LOW); //-
delay(wait);
return 0;
}
else if (no == 27) {
digitalWrite(2, LOW); //+
digitalWrite(3, LOW); //+
digitalWrite(4, HIGH); //+
digitalWrite(5, HIGH); //-
digitalWrite(6, HIGH); //-
digitalWrite(8, HIGH); //-
digitalWrite(9, HIGH); //-
digitalWrite(10, HIGH); //-
digitalWrite(11, HIGH); //-
digitalWrite(A0, HIGH); //-
digitalWrite(A1, HIGH); //-
digitalWrite(A2, LOW); //-
delay(wait);
return 0;
}
}
int LED0() {//all off
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(A0, LOW); //-
digitalWrite(A1, LOW); //-
digitalWrite(A2, LOW); //-
delay(wait);
return 0;
}

int LED_all() {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(A0, LOW); //-
digitalWrite(A1, LOW); //-
digitalWrite(A2, LOW); //-
delay(wait);
return 0;
}



LED Cube 完成の本文は↓
http://ameblo.jp/sn348/entry-12183723558.html