Skip to content

Instantly share code, notes, and snippets.

@jp0511

jp0511/LED Cube

Last active Aug 29, 2015
Embed
What would you like to do?
プログラムは、http://blog.livedoor.jp/syfone/archives/31809095.html を参考にして作成してみました。
int loopCount = 0;//10ループするとカウントされる
int sceanNow = 1;//現在のシーン
const int sceanMax = 7;//最大シーン数
int led[] = {
5, 6, 7, 8, 9, 10, 11, 12, 13};
int layer[] = {
2, 3, 4};
int cycletime = 200;
int TEMP_LED[]={
0x000, 0x000, 0x000}; //左からトップ層ミドル層ボトム層
int TOP[] ={
0x000, 0x000, 0x000, 0x000
};
int MID[] ={
0x000, 0x000, 0x000, 0x000
};
int BOT[] ={
0x000, 0x000, 0x000, 0x000
};
//初期化
int format[] ={
0x000, 0x000, 0x000, 0x000
};
int Max[] ={
0x1ff, 0x1ff, 0x1ff, 0x1ff
};
int pattern1[] = {
0x010, 0x010, 0x010, 0x010
};
int pattern2[] = {
0x028, 0x101, 0x082, 0x044
};
int pattern4[] = {
0x145, 0x0aa, 0x145, 0x0aa
};
int pattern8_1[] = {
0x145, 0x145, 0x145, 0x145
};
int pattern8_2[] = {
0x0aa, 0x0aa, 0x0aa, 0x0aa
};
int finish_TOP[] = {
0x1ff, 0x1fe, 0x1f4, 0x1c0, 0x100, 0x000, 0x000, 0x000
};
int finish_MID[] = {
0x1ff, 0x1ff, 0x1fe, 0x1f4, 0x1c0, 0x100, 0x000, 0x000
};
int finish_BOT[] ={
0x1ff, 0x1ff, 0x1ff, 0x1fe, 0x1f4, 0x1c0, 0x100, 0x000
};
void AllOFF(){
for(int i = 0; i < (sizeof(led) / sizeof(int)); i++){ //sizeof()はarray.length
digitalWrite(led[i], LOW);
}
for(int i = 0; i < (sizeof(layer) / sizeof(int)); i++){
digitalWrite(layer[i], HIGH);
}
}
void DynaDrive(int pat[], int ms){ //1msで上下のレイヤーを点灯消灯 配列の先頭のポインタが与えられる
for(int n = 0; n < cycletime / 3; n++){
for(int L = 0; L < (sizeof(layer) / sizeof(int)); L++){
for(int temp = 0; temp<(sizeof(led) / sizeof(int)); temp++){
if(bitRead(pat[L], temp) == B01){
digitalWrite(led[temp], HIGH);
}
else{
digitalWrite(led[temp], LOW);
}
}
digitalWrite(layer[L], LOW);
delayMicroseconds(ms);
digitalWrite(layer[L], HIGH);
delayMicroseconds(ms);
}
}
}
void setup() {
for(int i = 0; i < (sizeof(led) / sizeof(int)); i++){
pinMode(led[i], OUTPUT);
}
for(int i = 0; i < (sizeof(layer) / sizeof(int)); i++){
pinMode(layer[i], OUTPUT);
}
AllOFF();
}
void loop() {
SceanSelect(TOP, MID, BOT, (sceanNow = countNumAddition(++loopCount, sceanNow)));
if(sceanNow == sceanMax - 1){
for(int n = 0; n < (sizeof(TOP) / sizeof(int)); n++){
TEMP_LED[0] = TOP[n];
TEMP_LED[1] = MID[n];
TEMP_LED[2] = BOT[n];
DynaDrive(TEMP_LED, 1000);
AllOFF();
}
for(int n = 0; n < (sizeof(finish_TOP) / sizeof(int)); n++){
TEMP_LED[0] = finish_TOP[n];
TEMP_LED[1] = finish_MID[n];
TEMP_LED[2] = finish_BOT[n];
DynaDrive(TEMP_LED, 300);
AllOFF();
}
sceanNow = 1;
delay(2000);
}
else{
for(int n = 0; n < (sizeof(TOP) / sizeof(int)); n++){
TEMP_LED[0] = TOP[n];
TEMP_LED[1] = MID[n];
TEMP_LED[2] = BOT[n];
DynaDrive(TEMP_LED, 300);
AllOFF();
}
}
}
void SceanSelect(int T[], int M[], int B[], int scean){
switch(scean){
case 1:
for(int n = 0; n < sizeof(TOP) / sizeof(int); n++){
T[n] = format[n];
M[n] = pattern1[n];
B[n] = format[n];
}
break;
case 2:
for(int n = 0; n < sizeof(TOP) / sizeof(int); n++){
T[n] = format[n];
M[n] = pattern2[n];
B[n] = format[n];
}
break;
case 3:
for(int n = 0; n < (sizeof(TOP) / sizeof(int)); n++){
T[n] = pattern2[n];
M[n] = format[n];
B[n] = pattern2[n];
}
break;
case 4:
for(int n = 0; n < (sizeof(TOP) / sizeof(int)); n++){
T[n] = pattern4[n];
M[n] = format[n];
B[n] = pattern4[n];
}
break;
case 5:
for(int n = 0; n < (sizeof(TOP) / sizeof(int)); n++){
if(n % 2 == 0){
T[n] = pattern8_1[n];
M[n] = pattern8_2[n];
B[n] = pattern8_1[n];
}
else{
T[n] = pattern8_2[n];
M[n] = pattern8_1[n];
B[n] = pattern8_2[n];
}
}
break;
case 6:
for(int n = 0; n < (sizeof(TOP) / sizeof(int)); n++){
T[n] = Max[n];
M[n] = Max[n];
B[n] = Max[n];
}
break;
default:
for(int n = 0; n < (sizeof(TOP) / sizeof(int)); n++){
T[n] = format[n];
M[n] = format[n];
B[n] = format[n];
}
break;
}
}
int countNumAddition(int pcn, int scean){
if(pcn % 10 == 0){
scean++;//10周するごとにシーンを変化させる
}
if(scean >= sceanMax) {
scean = 1;//シーン1に戻す
}
return scean;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment