完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
由于是自己探索,基本刚刚起步学习,算是基本没有学过系统的知识,而且时间比较紧迫,有些问题实在没办法解决,希望能得到大家的帮助,谢谢!
首先,在用arduino驱动的光立方中能不能不使用电阻? 其次,我在一个视频中看到他利用了三极管做每层的开关,请问这个是必要的吗,而且这些开关起到什么作用? 最后,请问能不能帮我讲解一些下面的代码各部分的具体含义和效果功能。 再次感谢! #include #define XAXIS 0 #define YAXIS 1 #define ZAXIS 2 #define POS_X 0 #define NEG_X 1 #define POS_Z 2 #define NEG_Z 3 #define POS_Y 4 #define NEG_Y 5 #define BUTTON_PIN 8 #define RED_LED 5 #define GREEN_LED 7 #define TOTAL_EFFECTS 8 #define RAIN 0 #define PLANE_BOING 1 #define SEND_VOXELS 2 #define WOOP_WOOP 3 #define CUBE_JUMP 4 #define GLOW 5 #define TEXT 6 #define LIT 7 #define RAIN_tiME 260 #define PLANE_BOING_TIME 220 #define SEND_VOXELS_TIME 140 #define WOOP_WOOP_TIME 350 #define CUBE_JUMP_TIME 200 #define GLOW_TIME 8 #define TEXT_TIME 300 #define CLOCK_TIME 500 #define REMAIN_TIME 25000 //uint8_t characters[10][8] = { // {0x3C, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x3C}, //0 // {0x10, 0x18, 0x14, 0x10, 0x10, 0x10, 0x10, 0x3C}, //1 // {0x3C, 0x42, 0x40, 0x40, 0x3C, 0x02, 0x02, 0x7E}, //2 // {0x3C, 0x40, 0x40, 0x3C, 0x40, 0x40, 0x42, 0x3C}, //3 // {0x22, 0x22, 0x22, 0x22, 0x7E, 0x20, 0x20, 0x20}, //4 // {0x7E, 0x02, 0x02, 0x3E, 0x40, 0x40, 0x42, 0x3C}, //5 // {0x3C, 0x02, 0x02, 0x3E, 0x42, 0x42, 0x42, 0x3C}, //6 // {0x3C, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40}, //7 // {0x3C, 0x42, 0x42, 0x3C, 0x42, 0x42, 0x42, 0x3C}, //8 // {0x3C, 0x42, 0x42, 0x42, 0x3C, 0x40, 0x40, 0x3C}, //9 //}; //uint8_t characters[10][8] = {{0x1C,0x22,0x22,0x22,0x22,0x22,0x22,0x1C},//0 //{0x08,0x18,0x08,0x08,0x08,0x08,0x08,0x1C},//1 //{0x1C,0x22,0x02,0x02,0x1C,0x20,0x20,0x3E},//2 //{0x1C,0x22,0x02,0x1C,0x02,0x02,0x22,0x1C},//3 //{0x08,0x18,0x28,0x48,0x7C,0x08,0x08,0x08},//4 //{0x3E,0x20,0x20,0x3E,0x02,0x02,0x22,0x1C},//5 //{0x1C,0x22,0x20,0x3C,0x22,0x22,0x22,0x1C},//6 //{0x3E,0x02,0x04,0x08,0x10,0x10,0x10,0x10},//7 //{0x1C,0x22,0x22,0x1C,0x22,0x22,0x22,0x1C},//8 //{0x1C,0x22,0x22,0x22,0x1E,0x02,0x22,0x1C},//9 //}; uint8_t characters[40][8]={{0x1C,0x22,0x22,0x22,0x22,0x22,0x22,0x1C},//0 {0x08,0x18,0x08,0x08,0x08,0x08,0x08,0x1C},//1 {0x1C,0x22,0x02,0x02,0x1C,0x20,0x20,0x3E},//2 {0x1C,0x22,0x02,0x1C,0x02,0x02,0x22,0x1C},//3 {0x08,0x18,0x28,0x48,0x7C,0x08,0x08,0x08},//4 {0x3E,0x20,0x20,0x3E,0x02,0x02,0x22,0x1C},//5 {0x1C,0x22,0x20,0x3C,0x22,0x22,0x22,0x1C},//6 {0x3E,0x02,0x04,0x08,0x10,0x10,0x10,0x10},//7 {0x1C,0x22,0x22,0x1C,0x22,0x22,0x22,0x1C},//8 {0x1C,0x22,0x22,0x22,0x1E,0x02,0x22,0x1C},//9 {0x00,0x1C,0x22,0x22,0x22,0x3E,0x22,0x22},//A {0x00,0x3C,0x22,0x22,0x3E,0x22,0x22,0x3C},//B {0x00,0x1C,0x22,0x20,0x20,0x20,0x22,0x1C},//C {0x00,0x3C,0x22,0x22,0x22,0x22,0x22,0x3C},//D {0x00,0x3E,0x20,0x20,0x3E,0x20,0x20,0x3E},//E {0x00,0x3E,0x20,0x20,0x3E,0x20,0x20,0x20},//F {0x00,0x1C,0x22,0x20,0x3E,0x22,0x22,0x1C},//G {0x00,0x22,0x22,0x22,0x3E,0x22,0x22,0x22},//H {0x00,0x1C,0x08,0x08,0x08,0x08,0x08,0x1C},//I {0x00,0x3E,0x08,0x08,0x08,0x08,0x28,0x18},//J {0x00,0x20,0x2C,0x30,0x20,0x30,0x2C,0x20},//K {0x00,0x20,0x20,0x20,0x20,0x20,0x20,0x3E},//L {0x00,0x42,0x66,0x5A,0x42,0x42,0x42,0x42},//M {0x00,0x00,0x2C,0x32,0x22,0x22,0x22,0x22},//n {0x00,0x1C,0x22,0x22,0x22,0x22,0x22,0x1C},//O {0x00,0x3C,0x22,0x22,0x3C,0x20,0x20,0x20},//P {0x00,0x1C,0x22,0x22,0x22,0x2A,0x26,0x1F},//Q {0x00,0x38,0x24,0x24,0x38,0x30,0x28,0x24},//R {0x00,0x1C,0x22,0x20,0x1C,0x02,0x22,0x1C},//S {0x00,0x3E,0x08,0x08,0x08,0x08,0x08,0x08},//T {0x00,0x42,0x42,0x42,0x42,0x42,0x42,0x3C},//U {0x00,0x22,0x22,0x22,0x14,0x14,0x08,0x00},//V {0x00,0x41,0x41,0x49,0x55,0x55,0x63,0x41},//W {0x00,0x00,0x42,0x24,0x18,0x18,0x24,0x42},//X {0x00,0x22,0x22,0x14,0x08,0x10,0x20,0x00},//Y {0x00,0x3E,0x02,0x04,0x08,0x10,0x20,0x3E},//Z }; char *str="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; uint8_t BitReverseTable256[] = { 0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0, 0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0, 0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8, 0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0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 0xAB, 0x6B, 0xEB, 0x1B, 0x9B, 0x5B, 0xDB, 0x3B, 0xBB, 0x7B, 0xFB, 0x07, 0x87, 0x47, 0xC7, 0x27, 0xA7, 0x67, 0xE7, 0x17, 0x97, 0x57, 0xD7, 0x37, 0xB7, 0x77, 0xF7, 0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF, 0x6F, 0xEF, 0x1F, 0x9F, 0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF }; uint8_t cube[8][8]; uint8_t currentEffect; uint16_t timer; uint64_t randomTimer; bool loading; bool loopMode; uint64_t loopTimer; void setup() { Serial.begin(2000000); loading = true; loopMode = true; randomTimer = 0; currentEffect = RAIN; SPI.begin(); SPI.beginTransaction(SPISettings(8000000, MSBFIRST, SPI_MODE0)); pinMode(BUTTON_PIN, INPUT_PULLUP); // pinMode(RED_LED, OUTPUT); // pinMode(GREEN_LED, OUTPUT); randomSeed(analogRead(0)); digitalWrite(GREEN_LED, HIGH); } void loop() { //Serial.println("start loop"); randomTimer++; loopTimer++; if(loopMode){ if (currentEffect >= TOTAL_EFFECTS) { currentEffect = 0; } if(loopTimer>REMAIN_TIME){ loopTimer=0; clearCube(); loading = true; timer = 0; currentEffect++; randomSeed(randomTimer); randomTimer = 0; delay(100); } } if (digitalRead(BUTTON_PIN) == LOW) { clearCube(); loading = true; timer = 0; currentEffect++; if(loopMode){ currentEffect=0; } loopMode=false; if (currentEffect >= TOTAL_EFFECTS) { currentEffect = 100; loopMode=true; loopTimer=0; } randomSeed(randomTimer); randomTimer = 0; // digitalWrite(RED_LED, HIGH); // digitalWrite(GREEN_LED, LOW); delay(500); // digitalWrite(RED_LED, LOW); // digitalWrite(GREEN_LED, HIGH); } switch (currentEffect) { case RAIN: rain(); break; case PLANE_BOING: planeBoing(); break; case SEND_VOXELS: sendVoxels(); break; case WOOP_WOOP: woopWoop(); break; case CUBE_JUMP: cubeJump(); break; case GLOW: glow(); break; case TEXT: text("YANGGENGBABA"); break; case LIT: lit(); break; // default: rain(); } // Serial.print("currentEffect:"); // Serial.println(currentEffect); if(currentEffect>=0){ renderCube(); } } void renderCube() { for (uint8_t i = 0; i < 8; i++) { digitalWrite(SS, LOW); SPI.transfer(0x01 << i); for (uint8_t j = 0; j < 8; j++) { SPI.transfer(cube[j]); } digitalWrite(SS, HIGH); } } void rain() { if (loading) { clearCube(); loading = false; } timer++; if (timer > RAIN_TIME) { timer = 0; shift(NEG_Y); uint8_t numDrops = random(0, 5); for (uint8_t i = 0; i < numDrops; i++) { setVoxel(random(0, 8), 7, random(0, 8)); } } } uint8_t planePosition = 0; uint8_t planeDirection = 0; bool looped = false; void planeBoing() { if (loading) { clearCube(); uint8_t axis = random(0, 3); planePosition = random(0, 2) * 7; setPlane(axis, planePosition); if (axis == XAXIS) { if (planePosition == 0) { planeDirection = POS_X; } else { planeDirection = NEG_X; } } else if (axis == YAXIS) { if (planePosition == 0) { planeDirection = POS_Y; } else { planeDirection = NEG_Y; } } else if (axis == ZAXIS) { if (planePosition == 0) { planeDirection = POS_Z; } else { planeDirection = NEG_Z; } } timer = 0; looped = false; loading = false; } timer++; if (timer > PLANE_BOING_TIME) { timer = 0; shift(planeDirection); if (planeDirection % 2 == 0) { planePosition++; if (planePosition == 7) { if (looped) { loading = true; } else { planeDirection++; looped = true; } } } else { planePosition--; if (planePosition == 0) { if (looped) { loading = true; } else { planeDirection--; looped = true; } } } } } uint8_t selX = 0; uint8_t selY = 0; uint8_t selZ = 0; uint8_t sendDirection = 0; bool sending = false; void sendVoxels() { if (loading) { // Serial.println("loading sendVoxels"); clearCube(); for (uint8_t x = 0; x < 8; x++) { for (uint8_t z = 0; z < 8; z++) { setVoxel(x, random(0, 2) * 7, z); } } loading = false; selX = 0; selY = 0; selZ = 0; sendDirection = 0; sending = false; } timer++; if (timer > SEND_VOXELS_TIME) { timer = 0; if (!sending) { selX = random(0, 8); selZ = random(0, 8); if (getVoxel(selX, 0, selZ)) { selY = 0; sendDirection = POS_Y; } else if (getVoxel(selX, 7, selZ)) { selY = 7; sendDirection = NEG_Y; } sending = true; } else { if (sendDirection == POS_Y) { selY++; setVoxel(selX, selY, selZ); clearVoxel(selX, selY - 1, selZ); if (selY == 7) { sending = false; } } else { selY--; setVoxel(selX, selY, selZ); clearVoxel(selX, selY + 1, selZ); if (selY == 0) { sending = false; } } } } } uint8_t cubeSize = 0; bool cubeExpanding = true; void woopWoop() { if (loading) { clearCube(); cubeSize = 2; cubeExpanding = true; loading = false; } timer++; if (timer > WOOP_WOOP_TIME) { timer = 0; if (cubeExpanding) { cubeSize += 2; if (cubeSize == 8) { cubeExpanding = false; } } else { cubeSize -= 2; if (cubeSize == 2) { cubeExpanding = true; } } clearCube(); drawCube(4 - cubeSize / 2, 4 - cubeSize / 2, 4 - cubeSize / 2, cubeSize); } } uint8_t xPos; uint8_t yPos; uint8_t zPos; void cubeJump() { if (loading) { clearCube(); xPos = random(0, 2) * 7; yPos = random(0, 2) * 7; zPos = random(0, 2) * 7; cubeSize = 8; cubeExpanding = false; loading = false; } timer++; if (timer > CUBE_JUMP_TIME) { timer = 0; clearCube(); if (xPos == 0 && yPos == 0 && zPos == 0) { drawCube(xPos, yPos, zPos, cubeSize); } else if (xPos == 7 && yPos == 7 && zPos == 7) { drawCube(xPos + 1 - cubeSize, yPos + 1 - cubeSize, zPos + 1 - cubeSize, cubeSize); } else if (xPos == 7 && yPos == 0 && zPos == 0) { drawCube(xPos + 1 - cubeSize, yPos, zPos, cubeSize); } else if (xPos == 0 && yPos == 7 && zPos == 0) { drawCube(xPos, yPos + 1 - cubeSize, zPos, cubeSize); } else if (xPos == 0 && yPos == 0 && zPos == 7) { drawCube(xPos, yPos, zPos + 1 - cubeSize, cubeSize); } else if (xPos == 7 && yPos == 7 && zPos == 0) { drawCube(xPos + 1 - cubeSize, yPos + 1 - cubeSize, zPos, cubeSize); } else if (xPos == 0 && yPos == 7 && zPos == 7) { drawCube(xPos, yPos + 1 - cubeSize, zPos + 1 - cubeSize, cubeSize); } else if (xPos == 7 && yPos == 0 && zPos == 7) { drawCube(xPos + 1 - cubeSize, yPos, zPos + 1 - cubeSize, cubeSize); } if (cubeExpanding) { cubeSize++; if (cubeSize == 8) { cubeExpanding = false; xPos = random(0, 2) * 7; yPos = random(0, 2) * 7; zPos = random(0, 2) * 7; } } else { cubeSize--; if (cubeSize == 1) { cubeExpanding = true; } } } } bool glowing; uint16_t glowCount = 0; void glow() { if (loading) { clearCube(); glowCount = 0; glowing = true; loading = false; } timer++; if (timer > GLOW_TIME) { timer = 0; if (glowing) { if (glowCount < 448) { do { selX = random(0, 8); selY = random(0, 8); selZ = random(0, 8); } while (getVoxel(selX, selY, selZ)); setVoxel(selX, selY, selZ); glowCount++; } else if (glowCount < 512) { lightCube(); glowCount++; } else { glowing = false; glowCount = 0; } } else { if (glowCount < 448) { do { selX = random(0, 8); selY = random(0, 8); selZ = random(0, 8); } while (!getVoxel(selX, selY, selZ)); clearVoxel(selX, selY, selZ); glowCount++; } else { clearCube(); glowing = true; glowCount = 0; } } } } uint8_t charCounter = 0; uint8_t charPosition = 0; void text(char string[]) { uint8_t len=strlen(string); if (loading) { clearCube(); charPosition = -1; charCounter = 0; loading = false; } timer++; if (timer > TEXT_TIME) { timer = 0; shift(NEG_Z); charPosition++; if (charPosition == 7) { charCounter++; if (charCounter > len - 1) { charCounter = 0; } charPosition = 0; } if (charPosition == 0) { for (uint8_t i = 0; i < 8; i++) { //cube[0] = BitReverseTable256[characters[string[charCounter] - '0'] & 0xff]; char *pch =strchr(str,string[charCounter]); cube[0] = BitReverseTable256[characters[(int)(pch-str)] & 0xff]; } } } } void lit() { if (loading) { clearCube(); for(uint8_t i=0; i<8; i++) { for(uint8_t j=0; j<8; j++) { cube[j] = 0xFF; } } loading = false; } } void setVoxel(uint8_t x, uint8_t y, uint8_t z) { cube[7 - y][7 - z] |= (0x01 << x); } void clearVoxel(uint8_t x, uint8_t y, uint8_t z) { cube[7 - y][7 - z] ^= (0x01 << x); } bool getVoxel(uint8_t x, uint8_t y, uint8_t z) { return (cube[7 - y][7 - z] & (0x01 << x)) == (0x01 << x); } void setPlane(uint8_t axis, uint8_t i) { for (uint8_t j = 0; j < 8; j++) { for (uint8_t k = 0; k < 8; k++) { if (axis == XAXIS) { setVoxel(i, j, k); } else if (axis == YAXIS) { setVoxel(j, i, k); } else if (axis == ZAXIS) { setVoxel(j, k, i); } } } } void shift(uint8_t dir) { if (dir == POS_X) { for (uint8_t y = 0; y < 8; y++) { for (uint8_t z = 0; z < 8; z++) { cube[y][z] = cube[y][z] << 1; } } } else if (dir == NEG_X) { for (uint8_t y = 0; y < 8; y++) { for (uint8_t z = 0; z < 8; z++) { cube[y][z] = cube[y][z] >> 1; } } } else if (dir == POS_Y) { for (uint8_t y = 1; y < 8; y++) { for (uint8_t z = 0; z < 8; z++) { cube[y - 1][z] = cube[y][z]; } } for (uint8_t i = 0; i < 8; i++) { cube[7] = 0; } } else if (dir == NEG_Y) { for (uint8_t y = 7; y > 0; y--) { for (uint8_t z = 0; z < 8; z++) { cube[y][z] = cube[y - 1][z]; } } for (uint8_t i = 0; i < 8; i++) { cube[0] = 0; } } else if (dir == POS_Z) { for (uint8_t y = 0; y < 8; y++) { for (uint8_t z = 1; z < 8; z++) { cube[y][z - 1] = cube[y][z]; } } for (uint8_t i = 0; i < 8; i++) { cube[7] = 0; } } else if (dir == NEG_Z) { for (uint8_t y = 0; y < 8; y++) { for (uint8_t z = 7; z > 0; z--) { cube[y][z] = cube[y][z - 1]; } } for (uint8_t i = 0; i < 8; i++) { cube[0] = 0; } } } void drawCube(uint8_t x, uint8_t y, uint8_t z, uint8_t s) { for (uint8_t i = 0; i < s; i++) { setVoxel(x, y + i, z); setVoxel(x + i, y, z); setVoxel(x, y, z + i); setVoxel(x + s - 1, y + i, z + s - 1); setVoxel(x + i, y + s - 1, z + s - 1); setVoxel(x + s - 1, y + s - 1, z + i); setVoxel(x + s - 1, y + i, z); setVoxel(x, y + i, z + s - 1); setVoxel(x + i, y + s - 1, z); setVoxel(x + i, y, z + s - 1); setVoxel(x + s - 1, y, z + i); setVoxel(x, y + s - 1, z + i); } } void lightCube() { for (uint8_t i = 0; i < 8; i++) { for (uint8_t j = 0; j < 8; j++) { cube[j] = 0xFF; } } } void clearCube() { for (uint8_t i = 0; i < 8; i++) { for (uint8_t j = 0; j < 8; j++) { cube[j] = 0; } } } |
|
相关推荐
2 个讨论
|
|
不知道硬件连接,软件你就是瞎蒙
|
|
|
|
|
|
1072 浏览 0 评论
344 浏览 0 评论
632 浏览 0 评论
开源项目!打造一款FPV头部追踪相机,让你仿佛置身遥控车之中!
1079 浏览 0 评论
1060 浏览 0 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-20 15:22 , Processed in 0.835980 second(s), Total 63, Slave 46 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号