极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 81110|回复: 28

Arduino 8x8x8的光立方制作过程(附电路图和程式码)

[复制链接]
发表于 2013-6-18 00:05:02 | 显示全部楼层 |阅读模式
本帖最后由 p857211 于 2013-6-18 00:19 编辑




视频:Arduino 8x8x8 Cube    http://www.youtube.com/watch?v=1YnRPepOV4E
先前实验制作的4x4x4           http://www.youtube.com/watch?v=daCo3CaDQsA



  1. #include <avr/interrupt.h>
  2. #include <string.h>
  3. #define AXIS_X 1
  4. #define AXIS_Y 2
  5. #define AXIS_Z 3

  6. volatile unsigned char cube[8][8];
  7. volatile int current_layer = 0;

  8. void setup()
  9. {
  10.   int i;
  11.   
  12.   for(i=0; i<14; i++)
  13.     pinMode(i, OUTPUT);
  14.   

  15.   DDRC = 0xff;
  16.   PORTC = 0x00;
  17.   

  18.   TCCR2A = 0x00;
  19.   TCCR2B = 0x00;

  20.   TCCR2A |= (0x01 << WGM21);
  21.   OCR2A = 10;
  22.   TCNT2 = 0x00;
  23.   TCCR2B |= (0x01 << CS22) | (0x01 << CS21);
  24.   
  25.   TIMSK2 |= (0x01 << OCIE2A);
  26. }

  27. ISR (TIMER2_COMPA_vect)
  28. {
  29.   int i;
  30.   

  31.   PORTC = 0x00;
  32.   PORTB &= 0x0f;
  33.   
  34.   PORTB |= 0x08;
  35.   
  36.   for (i=0; i<8; i++)
  37.   {
  38.     PORTD = cube[current_layer][i];
  39.     PORTB = (PORTB & 0xF8) | (0x07 & (i+1));
  40.   }
  41.   
  42.   PORTB &= 0b00110111;
  43.   
  44.   if (current_layer < 6)
  45.   {
  46.     PORTC = (0x01 << current_layer);
  47.   } else if (current_layer == 6)
  48.   {
  49.     digitalWrite(12, HIGH);
  50.   } else
  51.   {
  52.     digitalWrite(13, HIGH);
  53.   }
  54.   
  55.   current_layer++;
  56.   
  57.   if (current_layer == 8)
  58.     current_layer = 0;
  59. }

  60. void loop()
  61. {
  62.   int i,x,y,z;
  63.   
  64.   while (true)
  65.   { effect_planboing(AXIS_Z, 1000);
  66.     effect_planboing(AXIS_Y, 1000);
  67.     effect_planboing(AXIS_X, 1000);
  68.     effect_boxside_randsend_parallel (AXIS_X, 0, 100, 1);
  69.     effect_boxside_randsend_parallel (AXIS_X, 1, 100, 1);
  70.     effect_boxside_randsend_parallel (AXIS_Y, 0, 100, 1);
  71.     effect_boxside_randsend_parallel (AXIS_Y, 1, 100, 1);
  72.     effect_boxside_randsend_parallel (AXIS_Z, 0, 100, 1);
  73.     effect_boxside_randsend_parallel (AXIS_Z, 1, 100, 1);
  74.     effect_random_filler(20,1);
  75.     effect_random_filler(20,0);
  76.     effect_random_filler(20,1);
  77.     effect_random_filler(20,0);
  78.   space(100);
  79.   space(100);

  80.   firework(-2,1,600);
  81.    
  82.     effect_planboing(AXIS_Z, 400);
  83.     effect_planboing(AXIS_Y, 400);
  84.     effect_planboing(AXIS_X, 400);
  85.    
  86.     effect_blinky2();
  87.    
  88.     effect_random_filler(75,1);
  89.     effect_random_filler(75,0);
  90.    
  91.     effect_rain(100);
  92.     space(100);
  93.    
  94.    
  95.     effect_boxside_randsend_parallel (AXIS_X, 0, 150, 1);
  96.     effect_boxside_randsend_parallel (AXIS_X, 1, 150, 1);
  97.     effect_boxside_randsend_parallel (AXIS_Y, 0, 150, 1);
  98.     effect_boxside_randsend_parallel (AXIS_Y, 1, 150, 1);
  99.     effect_boxside_randsend_parallel (AXIS_Z, 0, 150, 1);
  100.     effect_boxside_randsend_parallel (AXIS_Z, 1, 150, 1);
  101.     firework(0,0,0);
  102.   firework(-2,-2,50);
  103.   firework(1,1,-250);
  104.   firework(0,1,200);
  105.   firework(1,-3,400);
  106.   firework(2,-3,600);
  107.   firework(2,1,500);
  108.   firework(2,-2,200);  
  109.   firework(2,1,0);
  110.   firework(0,0,0);
  111.   


  112.   firework(2,-2,500);
  113.    
  114.   }
  115. }


  116. // ==========================================================================================
  117. //   Effect functions


  118. void space(int iterations){

  119.         int i, ii;
  120.         int rnd_y;
  121.         int rnd_z;
  122.         int rnd_num;
  123.         int time;
  124.        
  125.         time = 700;

  126.         for (ii=0;ii<iterations;ii++)
  127.         {
  128.                 time = time - (iterations/15);
  129.                 rnd_num = rand()%4;
  130.                
  131.                 for (i=0; i < rnd_num;i++)
  132.                 {
  133.                         rnd_y = rand()%8;
  134.                         rnd_z = rand()%8;
  135.                         setvoxel(7,rnd_y,rnd_z);
  136.                 }
  137.                
  138.                 delay_ms(time);
  139.                 shift(AXIS_X,-1);
  140.         }

  141.         for (ii=0;ii<iterations;ii++)
  142.         {
  143.                 time = time + (iterations/15);
  144.                 rnd_num = rand()%4;
  145.                
  146.                 for (i=0; i < rnd_num;i++)
  147.                 {
  148.                         rnd_y = rand()%8;
  149.                         rnd_z = rand()%8;
  150.                         setvoxel(7,rnd_y,rnd_z);
  151.                 }
  152.                
  153.                 delay_ms(time);
  154.                 shift(AXIS_X,-1);
  155.         }

  156. }



  157. void firework(int i,int j, int time){

  158. fill(0x00);

  159. setvoxel(3-i,4-j,0);  
  160. delay_ms(900-time);

  161. clrvoxel(3-i,4-j,0);  
  162. setvoxel(4-i,4-j,1);  
  163. delay_ms(1200-time);

  164. clrvoxel(4-i,4-j,1);  
  165. setvoxel(4-i,5-j,2);  
  166. delay_ms(1400-time);

  167. clrvoxel(4-i,5-j,2);  
  168. setvoxel(3-i,5-j,3);  
  169. delay_ms(1700-time);

  170. clrvoxel(3-i,5-j,3);  
  171. setvoxel(3-i,4-j,4);  
  172. delay_ms(2000-time);

  173. clrvoxel(3-i,4-j,4);  
  174. setvoxel(4-i,4-j,5);  
  175. delay_ms(2000-time);

  176. clrvoxel(4-i,4-j,5);  
  177. setvoxel(4-i,3-j,6);  
  178. delay_ms(2000-time);

  179. //Explode
  180. clrvoxel(4-i,3-j,6);
  181. setvoxel(4-i,3-j,7);
  182. setvoxel(4-i,4-j,6);
  183. setvoxel(4-i,2-j,6);
  184. setvoxel(3-i,3-j,6);
  185. setvoxel(5-i,3-j,6);
  186. delay_ms(2000-time);

  187. shift(AXIS_Z,-1);
  188. setvoxel(4-i,5-j,5);
  189. setvoxel(4-i,1-j,5);
  190. setvoxel(2-i,3-j,5);
  191. setvoxel(6-i,3-j,5);
  192. delay_ms(900-time);

  193. shift(AXIS_Z,-1);
  194. setvoxel(4-i,6-j,3);
  195. setvoxel(4-i,0-j,3);
  196. setvoxel(1-i,3-j,3);
  197. setvoxel(7-i,3-j,3);
  198. delay_ms(900-time);

  199. shift(AXIS_Z,-1);
  200. setvoxel(4-i,7-j,1);
  201. setvoxel(3-i,0-j,1);
  202. setvoxel(0-i,3-j,1);
  203. setvoxel(7-i,2-j,1);
  204. delay_ms(1400-time);

  205. shift(AXIS_Z,-1);
  206. delay_ms(1400-time);

  207. shift(AXIS_Z,-1);
  208. delay_ms(1400-time);

  209. shift(AXIS_Z,-1);
  210. delay_ms(1400-time);

  211. shift(AXIS_Z,-1);
  212. delay_ms(700-time);

  213. fill(0x00);

  214. }


  215. // ==========================================================================================

  216. void draw_positions_axis (char axis, unsigned char positions[64], int invert)
  217. {
  218.         int x, y, p;
  219.        
  220.         fill(0x00);
  221.        
  222.         for (x=0; x<8; x++)
  223.         {
  224.                 for (y=0; y<8; y++)
  225.                 {
  226.                         if (invert)
  227.                         {
  228.                                 p = (7-positions[(x*8)+y]);
  229.                         } else
  230.                         {
  231.                                 p = positions[(x*8)+y];
  232.                         }
  233.                
  234.                         if (axis == AXIS_Z)
  235.                                 setvoxel(x,y,p);
  236.                                
  237.                         if (axis == AXIS_Y)
  238.                                 setvoxel(x,p,y);
  239.                                
  240.                         if (axis == AXIS_X)
  241.                                 setvoxel(p,y,x);
  242.                 }
  243.         }
  244.        
  245. }


  246. void effect_boxside_randsend_parallel (char axis, int origin, int delay, int mode)
  247. {
  248.         int i;
  249.         int done;
  250.         unsigned char cubepos[64];
  251.         unsigned char pos[64];
  252.         int notdone = 1;
  253.         int notdone2 = 1;
  254.         int sent = 0;
  255.        
  256.         for (i=0;i<64;i++)
  257.         {
  258.                 pos[i] = 0;
  259.         }
  260.        
  261.         while (notdone)
  262.         {
  263.                 if (mode == 1)
  264.                 {
  265.                         notdone2 = 1;
  266.                         while (notdone2 && sent<64)
  267.                         {
  268.                                 i = rand()%64;
  269.                                 if (pos[i] == 0)
  270.                                 {
  271.                                         sent++;
  272.                                         pos[i] += 1;
  273.                                         notdone2 = 0;
  274.                                 }
  275.                         }
  276.                 } else if (mode == 2)
  277.                 {
  278.                         if (sent<64)
  279.                         {
  280.                                 pos[sent] += 1;
  281.                                 sent++;
  282.                         }
  283.                 }
  284.                
  285.                 done = 0;
  286.                 for (i=0;i<64;i++)
  287.                 {
  288.                         if (pos[i] > 0 && pos[i] <7)
  289.                         {
  290.                                 pos[i] += 1;
  291.                         }
  292.                                
  293.                         if (pos[i] == 7)
  294.                                 done++;
  295.                 }
  296.                
  297.                 if (done == 64)
  298.                         notdone = 0;
  299.                
  300.                 for (i=0;i<64;i++)
  301.                 {
  302.                         if (origin == 0)
  303.                         {
  304.                                 cubepos[i] = pos[i];
  305.                         } else
  306.                         {
  307.                                 cubepos[i] = (7-pos[i]);
  308.                         }
  309.                 }
  310.                
  311.                
  312.                 delay_ms(delay);
  313.                 draw_positions_axis(axis,cubepos,0);

  314.         }
  315.        
  316. }


  317. void effect_rain (int iterations)
  318. {
  319.         int i, ii;
  320.         int rnd_x;
  321.         int rnd_y;
  322.         int rnd_num;
  323.        
  324.         for (ii=0;ii<iterations;ii++)
  325.         {
  326.                 rnd_num = rand()%4;
  327.                
  328.                 for (i=0; i < rnd_num;i++)
  329.                 {
  330.                         rnd_x = rand()%8;
  331.                         rnd_y = rand()%8;
  332.                         setvoxel(rnd_x,rnd_y,7);
  333.                 }
  334.                
  335.                 delay_ms(1000);
  336.                 shift(AXIS_Z,-1);
  337.         }
  338. }

  339. // Set or clear exactly 512 voxels in a random order.
  340. void effect_random_filler (int delay, int state)
  341. {
  342.         int x,y,z;
  343.         int loop = 0;
  344.        
  345.        
  346.         if (state == 1)
  347.         {
  348.                 fill(0x00);
  349.         } else
  350.         {
  351.                 fill(0xff);
  352.         }
  353.        
  354.         while (loop<511)
  355.         {
  356.                 x = rand()%8;
  357.                 y = rand()%8;
  358.                 z = rand()%8;

  359.                 if ((state == 0 && getvoxel(x,y,z) == 0x01) || (state == 1 && getvoxel(x,y,z) == 0x00))
  360.                 {
  361.                         altervoxel(x,y,z,state);
  362.                         delay_ms(delay);
  363.                         loop++;
  364.                 }       
  365.         }
  366. }


  367. void effect_blinky2()
  368. {
  369.         int i,r;
  370.         fill(0x00);
  371.        
  372.         for (r=0;r<2;r++)
  373.         {
  374.                 i = 750;
  375.                 while (i>0)
  376.                 {
  377.                         fill(0x00);
  378.                         delay_ms(i);
  379.                        
  380.                         fill(0xff);
  381.                         delay_ms(100);
  382.                        
  383.                         i = i - (15+(1000/(i/10)));
  384.                 }
  385.                
  386.                 delay_ms(1000);
  387.                
  388.                 i = 750;
  389.                 while (i>0)
  390.                 {
  391.                         fill(0x00);
  392.                         delay_ms(751-i);
  393.                        
  394.                         fill(0xff);
  395.                         delay_ms(100);
  396.                        
  397.                         i = i - (15+(1000/(i/10)));
  398.                 }
  399.         }

  400. }

  401. // Draw a plane on one axis and send it back and forth once.
  402. void effect_planboing (int plane, int speed)
  403. {
  404.         int i;
  405.         for (i=0;i<8;i++)
  406.         {
  407.                 fill(0x00);
  408.         setplane(plane, i);
  409.                 delay_ms(speed);
  410.         }
  411.        
  412.         for (i=7;i>=0;i--)
  413.         {
  414.                 fill(0x00);
  415.         setplane(plane,i);
  416.                 delay_ms(speed);
  417.         }
  418. }




  419. // ==========================================================================================
  420. //   Draw functions
  421. // ==========================================================================================


  422. // Set a single voxel to ON
  423. void setvoxel(int x, int y, int z)
  424. {
  425.         if (inrange(x,y,z))
  426.                 cube[z][y] |= (1 << x);
  427. }


  428. // Set a single voxel to ON
  429. void clrvoxel(int x, int y, int z)
  430. {
  431.         if (inrange(x,y,z))
  432.                 cube[z][y] &= ~(1 << x);
  433. }



  434. // This function validates that we are drawing inside the cube.
  435. unsigned char inrange(int x, int y, int z)
  436. {
  437.         if (x >= 0 && x < 8 && y >= 0 && y < 8 && z >= 0 && z < 8)
  438.         {
  439.                 return 0x01;
  440.         } else
  441.         {
  442.                 // One of the coordinates was outside the cube.
  443.                 return 0x00;
  444.         }
  445. }

  446. // Get the current status of a voxel
  447. unsigned char getvoxel(int x, int y, int z)
  448. {
  449.         if (inrange(x,y,z))
  450.         {
  451.                 if (cube[z][y] & (1 << x))
  452.                 {
  453.                         return 0x01;
  454.                 } else
  455.                 {
  456.                         return 0x00;
  457.                 }
  458.         } else
  459.         {
  460.                 return 0x00;
  461.         }
  462. }

  463. // In some effect we want to just take bool and write it to a voxel
  464. // this function calls the apropriate voxel manipulation function.
  465. void altervoxel(int x, int y, int z, int state)
  466. {
  467.         if (state == 1)
  468.         {
  469.                 setvoxel(x,y,z);
  470.         } else
  471.         {
  472.                 clrvoxel(x,y,z);
  473.         }
  474. }

  475. // Flip the state of a voxel.
  476. // If the voxel is 1, its turned into a 0, and vice versa.
  477. void flpvoxel(int x, int y, int z)
  478. {
  479.         if (inrange(x, y, z))
  480.                 cube[z][y] ^= (1 << x);
  481. }

  482. // Makes sure x1 is alwas smaller than x2
  483. // This is usefull for functions that uses for loops,
  484. // to avoid infinite loops
  485. void argorder(int ix1, int ix2, int *ox1, int *ox2)
  486. {
  487.         if (ix1>ix2)
  488.         {
  489.                 int tmp;
  490.                 tmp = ix1;
  491.                 ix1= ix2;
  492.                 ix2 = tmp;
  493.         }
  494.         *ox1 = ix1;
  495.         *ox2 = ix2;
  496. }

  497. // Sets all voxels along a X/Y plane at a given point
  498. // on axis Z
  499. void setplane_z (int z)
  500. {
  501.         int i;
  502.         if (z>=0 && z<8)
  503.         {
  504.                 for (i=0;i<8;i++)
  505.                         cube[z][i] = 0xff;
  506.         }
  507. }

  508. // Clears voxels in the same manner as above
  509. void clrplane_z (int z)
  510. {
  511.         int i;
  512.         if (z>=0 && z<8)
  513.         {
  514.                 for (i=0;i<8;i++)
  515.                         cube[z][i] = 0x00;
  516.         }
  517. }

  518. void setplane_x (int x)
  519. {
  520.         int z;
  521.         int y;
  522.         if (x>=0 && x<8)
  523.         {
  524.                 for (z=0;z<8;z++)
  525.                 {
  526.                         for (y=0;y<8;y++)
  527.                         {
  528.                                 cube[z][y] |= (1 << x);
  529.                         }
  530.                 }
  531.         }
  532. }

  533. void clrplane_x (int x)
  534. {
  535.         int z;
  536.         int y;
  537.         if (x>=0 && x<8)
  538.         {
  539.                 for (z=0;z<8;z++)
  540.                 {
  541.                         for (y=0;y<8;y++)
  542.                         {
  543.                                 cube[z][y] &= ~(1 << x);
  544.                         }
  545.                 }
  546.         }
  547. }

  548. void setplane_y (int y)
  549. {
  550.         int z;
  551.         if (y>=0 && y<8)
  552.         {
  553.                 for (z=0;z<8;z++)
  554.                         cube[z][y] = 0xff;
  555.         }
  556. }

  557. void clrplane_y (int y)
  558. {
  559.         int z;
  560.         if (y>=0 && y<8)
  561.         {
  562.                 for (z=0;z<8;z++)
  563.                         cube[z][y] = 0x00;
  564.         }
  565. }

  566. void setplane (char axis, unsigned char i)
  567. {
  568.     switch (axis)
  569.     {
  570.         case AXIS_X:
  571.             setplane_x(i);
  572.             break;
  573.         
  574.        case AXIS_Y:
  575.             setplane_y(i);
  576.             break;

  577.        case AXIS_Z:
  578.             setplane_z(i);
  579.             break;
  580.     }
  581. }

  582. void clrplane (char axis, unsigned char i)
  583. {
  584.     switch (axis)
  585.     {
  586.         case AXIS_X:
  587.             clrplane_x(i);
  588.             break;
  589.         
  590.        case AXIS_Y:
  591.             clrplane_y(i);
  592.             break;

  593.        case AXIS_Z:
  594.             clrplane_z(i);
  595.             break;
  596.     }
  597. }

  598. // Fill a value into all 64 byts of the cube buffer
  599. // Mostly used for clearing. fill(0x00)
  600. // or setting all on. fill(0xff)
  601. void fill (unsigned char pattern)
  602. {
  603.         int z;
  604.         int y;
  605.         for (z=0;z<8;z++)
  606.         {
  607.                 for (y=0;y<8;y++)
  608.                 {
  609.                         cube[z][y] = pattern;
  610.                 }
  611.         }
  612. }



  613. // Draw a box with all walls drawn and all voxels inside set
  614. void box_filled(int x1, int y1, int z1, int x2, int y2, int z2)
  615. {
  616.         int iy;
  617.         int iz;

  618.         argorder(x1, x2, &x1, &x2);
  619.         argorder(y1, y2, &y1, &y2);
  620.         argorder(z1, z2, &z1, &z2);

  621.         for (iz=z1;iz<=z2;iz++)
  622.         {
  623.                 for (iy=y1;iy<=y2;iy++)
  624.                 {
  625.                         cube[iz][iy] |= byteline(x1,x2);
  626.                 }
  627.         }

  628. }

  629. // Darw a hollow box with side walls.
  630. void box_walls(int x1, int y1, int z1, int x2, int y2, int z2)
  631. {
  632.         int iy;
  633.         int iz;
  634.        
  635.         argorder(x1, x2, &x1, &x2);
  636.         argorder(y1, y2, &y1, &y2);
  637.         argorder(z1, z2, &z1, &z2);

  638.         for (iz=z1;iz<=z2;iz++)
  639.         {
  640.                 for (iy=y1;iy<=y2;iy++)
  641.                 {       
  642.                         if (iy == y1 || iy == y2 || iz == z1 || iz == z2)
  643.                         {
  644.                                 cube[iz][iy] = byteline(x1,x2);
  645.                         } else
  646.                         {
  647.                                 cube[iz][iy] |= ((0x01 << x1) | (0x01 << x2));
  648.                         }
  649.                 }
  650.         }

  651. }

  652. // Draw a wireframe box. This only draws the corners and edges,
  653. // no walls.
  654. void box_wireframe(int x1, int y1, int z1, int x2, int y2, int z2)
  655. {
  656.         int iy;
  657.         int iz;

  658.         argorder(x1, x2, &x1, &x2);
  659.         argorder(y1, y2, &y1, &y2);
  660.         argorder(z1, z2, &z1, &z2);

  661.         // Lines along X axis
  662.         cube[z1][y1] = byteline(x1,x2);
  663.         cube[z1][y2] = byteline(x1,x2);
  664.         cube[z2][y1] = byteline(x1,x2);
  665.         cube[z2][y2] = byteline(x1,x2);

  666.         // Lines along Y axis
  667.         for (iy=y1;iy<=y2;iy++)
  668.         {
  669.                 setvoxel(x1,iy,z1);
  670.                 setvoxel(x1,iy,z2);
  671.                 setvoxel(x2,iy,z1);
  672.                 setvoxel(x2,iy,z2);
  673.         }

  674.         // Lines along Z axis
  675.         for (iz=z1;iz<=z2;iz++)
  676.         {
  677.                 setvoxel(x1,y1,iz);
  678.                 setvoxel(x1,y2,iz);
  679.                 setvoxel(x2,y1,iz);
  680.                 setvoxel(x2,y2,iz);
  681.         }

  682. }

  683. // Returns a byte with a row of 1's drawn in it.
  684. // byteline(2,5) gives 0b00111100
  685. char byteline (int start, int end)
  686. {
  687.         return ((0xff<<start) & ~(0xff<<(end+1)));
  688. }

  689. // Flips a byte 180 degrees.
  690. // MSB becomes LSB, LSB becomes MSB.
  691. char flipbyte (char byte)
  692. {
  693.         char flop = 0x00;

  694.         flop = (flop & 0b11111110) | (0b00000001 & (byte >> 7));
  695.         flop = (flop & 0b11111101) | (0b00000010 & (byte >> 5));
  696.         flop = (flop & 0b11111011) | (0b00000100 & (byte >> 3));
  697.         flop = (flop & 0b11110111) | (0b00001000 & (byte >> 1));
  698.         flop = (flop & 0b11101111) | (0b00010000 & (byte << 1));
  699.         flop = (flop & 0b11011111) | (0b00100000 & (byte << 3));
  700.         flop = (flop & 0b10111111) | (0b01000000 & (byte << 5));
  701.         flop = (flop & 0b01111111) | (0b10000000 & (byte << 7));
  702.         return flop;
  703. }

  704. // Draw a line between any coordinates in 3d space.
  705. // Uses integer values for input, so dont expect smooth animations.
  706. void line(int x1, int y1, int z1, int x2, int y2, int z2)
  707. {
  708.         float xy;        // how many voxels do we move on the y axis for each step on the x axis
  709.         float xz;        // how many voxels do we move on the y axis for each step on the x axis
  710.         unsigned char x,y,z;
  711.         unsigned char lasty,lastz;

  712.         // We always want to draw the line from x=0 to x=7.
  713.         // If x1 is bigget than x2, we need to flip all the values.
  714.         if (x1>x2)
  715.         {
  716.                 int tmp;
  717.                 tmp = x2; x2 = x1; x1 = tmp;
  718.                 tmp = y2; y2 = y1; y1 = tmp;
  719.                 tmp = z2; z2 = z1; z1 = tmp;
  720.         }

  721.        
  722.         if (y1>y2)
  723.         {
  724.                 xy = (float)(y1-y2)/(float)(x2-x1);
  725.                 lasty = y2;
  726.         } else
  727.         {
  728.                 xy = (float)(y2-y1)/(float)(x2-x1);
  729.                 lasty = y1;
  730.         }

  731.         if (z1>z2)
  732.         {
  733.                 xz = (float)(z1-z2)/(float)(x2-x1);
  734.                 lastz = z2;
  735.         } else
  736.         {
  737.                 xz = (float)(z2-z1)/(float)(x2-x1);
  738.                 lastz = z1;
  739.         }



  740.         // For each step of x, y increments by:
  741.         for (x = x1; x<=x2;x++)
  742.         {
  743.                 y = (xy*(x-x1))+y1;
  744.                 z = (xz*(x-x1))+z1;
  745.                 setvoxel(x,y,z);
  746.         }
  747.        
  748. }

  749. // 動畫Delay迴圈
  750. void delay_ms(uint16_t x)
  751. {
  752.   uint8_t y, z;
  753.   for ( ; x > 0 ; x--){
  754.     for ( y = 0 ; y < 90 ; y++){
  755.       for ( z = 0 ; z < 6 ; z++){
  756.         asm volatile ("nop");
  757.       }
  758.     }
  759.   }
  760. }



  761. //Z axiz的下雨動畫
  762. void shift (char axis, int direction)
  763. {
  764.         int i, x ,y;
  765.         int ii, iii;
  766.         int state;

  767.         for (i = 0; i < 8; i++)
  768.         {
  769.                 if (direction == -1)
  770.                 {
  771.                         ii = i;
  772.                 } else
  773.                 {
  774.                         ii = (7-i);
  775.                 }       
  776.        
  777.        
  778.                 for (x = 0; x < 8; x++)
  779.                 {
  780.                         for (y = 0; y < 8; y++)
  781.                         {
  782.                                 if (direction == -1)
  783.                                 {
  784.                                         iii = ii+1;
  785.                                 } else
  786.                                 {
  787.                                         iii = ii-1;
  788.                                 }
  789.                                
  790.                                 if (axis == AXIS_Z)
  791.                                 {
  792.                                         state = getvoxel(x,y,iii);
  793.                                         altervoxel(x,y,ii,state);
  794.                                 }
  795.                                
  796.                                 if (axis == AXIS_Y)
  797.                                 {
  798.                                         state = getvoxel(x,iii,y);
  799.                                         altervoxel(x,ii,y,state);
  800.                                 }
  801.                                
  802.                                 if (axis == AXIS_X)
  803.                                 {
  804.                                         state = getvoxel(iii,y,x);
  805.                                         altervoxel(ii,y,x,state);
  806.                                 }
  807.                         }
  808.                 }
  809.         }
  810.        
  811.         if (direction == -1)
  812.         {
  813.                 i = 7;
  814.         } else
  815.         {
  816.                 i = 0;
  817.         }       
  818.        
  819.         for (x = 0; x < 8; x++)
  820.         {
  821.                 for (y = 0; y < 8; y++)
  822.                 {
  823.                         if (axis == AXIS_Z)
  824.                                 clrvoxel(x,y,i);
  825.                                
  826.                         if (axis == AXIS_Y)
  827.                                 clrvoxel(x,i,y);
  828.                        
  829.                         if (axis == AXIS_X)
  830.                                 clrvoxel(i,y,x);
  831.                 }
  832.         }
  833. }
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x

评分

参与人数 1 +2 收起 理由
幻生幻灭 + 2 赞两个!

查看全部评分

回复

使用道具 举报

发表于 2013-6-18 07:09:22 | 显示全部楼层
这小线飞的,真赞!
回复 支持 反对

使用道具 举报

发表于 2013-6-18 07:53:33 | 显示全部楼层
视频怎么是Youtube?
回复 支持 反对

使用道具 举报

发表于 2013-6-18 08:47:14 | 显示全部楼层
有密集恐惧症和缺乏耐心毅力者请勿模仿哈
回复 支持 反对

使用道具 举报

发表于 2013-6-18 09:13:33 | 显示全部楼层
厉害!膜拜
回复 支持 反对

使用道具 举报

发表于 2013-6-18 09:51:28 | 显示全部楼层
我焊的时候是一个一个用尺量的,焊得不错
回复 支持 反对

使用道具 举报

发表于 2013-6-18 20:03:19 | 显示全部楼层
视频只有9’’。
回复 支持 反对

使用道具 举报

发表于 2013-6-18 20:11:04 | 显示全部楼层
第一个视频只有9’’。
回复 支持 反对

使用道具 举报

发表于 2013-6-19 02:04:28 | 显示全部楼层
蛋疼的男人
回复 支持 反对

使用道具 举报

发表于 2014-1-8 18:15:43 | 显示全部楼层
谢谢楼主!!!
回复 支持 反对

使用道具 举报

发表于 2014-1-8 20:05:45 | 显示全部楼层
楼主真心强大啊!
回复 支持 反对

使用道具 举报

发表于 2014-1-9 03:18:56 | 显示全部楼层
楼主问一下你这个代码看的懂吗??
还有arduino的i/o口是怎么接的,分别是哪几个i/o口接到板子上,哪几个接138??哪几个接74hc574??还有哪几个接地线??
回复 支持 反对

使用道具 举报

发表于 2014-1-9 03:19:37 | 显示全部楼层

楼主问一下你这个代码看的懂吗??
firework();啥意思??百度不到。。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-1-14 10:32:57 | 显示全部楼层
谷东昌 发表于 2014-1-9 03:19
楼主问一下你这个代码看的懂吗??
firework();啥意思??百度不到。。

firrework(); 那是煙火的動畫程式
運算都寫在副程式裡
在loop裡用函數把它呼叫出來
這樣方便 隨意編排動畫順序
而不會導致 重復復製相同的程式 亂七八糟 讓自己看不懂
回复 支持 反对

使用道具 举报

发表于 2014-1-20 21:33:24 | 显示全部楼层
p857211 发表于 2014-1-14 10:32
firrework(); 那是煙火的動畫程式
運算都寫在副程式裡
在loop裡用函數把它呼叫出來

谢过。。。。。。。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-19 11:06 , Processed in 0.058738 second(s), 30 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表