Arduino 入门程序示例之一片 LED

[复制链接]
2094|2
 楼主| starsnow 发表于 2016-11-25 23:21 | 显示全部楼层 |阅读模式
本帖最后由 starsnow 于 2016-11-25 23:23 编辑

概述
从点到线,从线到面。现在开始要来一片的 LED 了,一大波的 LED 正在到来!

示例程序
因为手头没有现成的模块,手头只有 595,所以这里每一个示例程序都是使用 74HC595 扩展 IO 口的。后面不多加备注了。
现成的模块还有其它专门的驱动芯片的,程序写起来就更简单了,要根据具体的驱动芯片来决定程序,这个程序不通用的哦。

点阵显示静止的心
别看到静止两个字,这里点阵可是动态扫描的哟。所以程序里不能有 delay() 等阻塞主函数的延时函数。
  1. // ----------------------------------------------------------------------------
  2. // LEDLattice.ino
  3. //
  4. // Created 2015-06-07
  5. // By seesea <seesea2517#gmail#com>
  6. //
  7. // LED 点阵
  8. // 使用两个 74HC595 驱动单色 LED 点阵
  9. //
  10. // ----------------------------------------------------------------------------

  11. const unsigned char latchPin = 10; // 595 的 ST_CP
  12. const unsigned char clockPin = 9;  // 595 的 SH_CP
  13. const unsigned char dataPin  = 8;  // 595 的 DS

  14. #define SIZE 7  // 点阵的行列数

  15. // 每一行的模值
  16. const byte row[] =
  17. {
  18.   0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80
  19. };

  20. // 心形的取模
  21. const byte heart[] =
  22. {
  23.     0xFF, 0x93, 0x6D, 0x7D, 0xBB, 0xD7, 0xEF, 0xFF
  24. };

  25. void setup ()
  26. {
  27.     pinMode(latchPin, OUTPUT);
  28.     pinMode(clockPin, OUTPUT);
  29.     pinMode(dataPin,  OUTPUT);
  30. }


  31. void loop()
  32. {
  33.     showArrPic(heart);
  34. }

  35. // 显示数组所代表的图片
  36. void showArrPic(const byte arr[])
  37. {
  38.     for (unsigned char i = 0; i < 8; ++i)
  39.     {   
  40.         digitalWrite(latchPin, LOW);
  41.         shiftOut(dataPin, clockPin, LSBFIRST, (byte) arr[i]);
  42.         shiftOut(dataPin, clockPin, LSBFIRST, (byte) row[i]);
  43.         digitalWrite(latchPin, HIGH);
  44.     }
  45. }

  46. // 另一种方式显示数组所代表的图片
  47. void showArrPic2(const byte arr[])
  48. {
  49.     static unsigned char i = 0;
  50.    
  51.     digitalWrite(latchPin, LOW);
  52.     shiftOut(dataPin, clockPin, LSBFIRST, arr[i]);
  53.     shiftOut(dataPin, clockPin, LSBFIRST, row[i]);
  54.     digitalWrite(latchPin, HIGH);
  55.    
  56.     if (++i >= SIZE)
  57.         i = 0;
  58. }

简单看看接线吧,没有现成的模块,都是手工现搭的,纯天然……不建议自己搭,麻烦耗时还容易出错。(偷懒只在 VCC 处接了一个限流电阻,效果也还过得去,不怕麻烦的同学可以多加几个 :D )
飘落的心
没错,它动起来了,所以它是动态扫描的。好吧,前面的语句是错误的示范 :D
这个程序示范了一个位移动画。
  1. // ----------------------------------------------------------------------------
  2. // latticeDownHeart.ino
  3. //
  4. // Created 2015-06-07
  5. // By seesea <seesea2517#gmail#com>
  6. //
  7. // 点阵动画:飘落的心
  8. // 使用两个 74HC595 驱动单色 LED 点阵,显示一个向下飘落的心,展示位移动画
  9. //
  10. // ----------------------------------------------------------------------------

  11. const unsigned char latchPin = 10; // 595 的 ST_CP
  12. const unsigned char clockPin = 9;  // 595 的 SH_CP
  13. const unsigned char dataPin  = 8;  // 595 的 DS

  14. const unsigned long delayMs  = 90; // 图像运动延时时间

  15. #define SIZE 8  // 点阵的行列数

  16. // 每一行的模值
  17. const byte row[] =
  18. {
  19.   0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80
  20. };

  21. // 心形的取模
  22. const byte heart[] =
  23. {
  24.     0xFF, 0x93, 0x6D, 0x7D, 0xBB, 0xD7, 0xEF, 0xFF
  25. };

  26. void setup ()
  27. {
  28.     pinMode(latchPin, OUTPUT);
  29.     pinMode(clockPin, OUTPUT);
  30.     pinMode(dataPin,  OUTPUT);
  31. }

  32. void loop()
  33. {
  34.     static char y = -8;
  35.     static unsigned long lastTick = millis();
  36.    
  37.     // 延时一定时间后,移动坐标
  38.     if (millis() - lastTick > delayMs)
  39.     {
  40.         ++y;
  41.         lastTick = millis();
  42.         
  43.         if (y > SIZE)
  44.             y = -8;
  45.     }
  46.    
  47.     showArrPic(heart, 0, y);
  48. }

  49. // 显示数组所代表的图片
  50. // 以 (x, y) 为左上角起点位置,默认在 (0, 0) 位置显示
  51. // 坐标系:
  52. //  +------>
  53. //  |      x
  54. //  |
  55. //  v y
  56. //
  57. void showArrPic(const byte arr[], char x, char y)
  58. {
  59.     for (char i = 0; i < SIZE; ++i)
  60.     {   
  61.         if (i + x < 0 || i + x >= SIZE ||
  62.             i + y < 0 || i + y >= SIZE)
  63.         {
  64.             continue;
  65.         }

  66.         digitalWrite(latchPin, LOW);
  67.         shiftOut(dataPin, clockPin, LSBFIRST, (byte) arr[i + x]);
  68.         shiftOut(dataPin, clockPin, LSBFIRST, (byte) row[i + y]);
  69.         digitalWrite(latchPin, HIGH);
  70.     }
  71. }

咦,手机拍下的效果还有拖影,将就点吧,意思到了就行:):

走动的小人
这里演示了一个逐帧动画。
  1. // ----------------------------------------------------------------------------
  2. // LEDLattice.ino
  3. //
  4. // Created 2015-06-07
  5. // By seesea <seesea2517#gmail#com>
  6. //
  7. // 点阵动画:人物走路的逐帧动画演示
  8. // 使用两个 74HC595 驱动单色 LED 点阵
  9. //
  10. // ----------------------------------------------------------------------------

  11. const unsigned char latchPin = 10; // 595 的 ST_CP
  12. const unsigned char clockPin = 9;  // 595 的 SH_CP
  13. const unsigned char dataPin  = 8;  // 595 的 DS

  14. const unsigned long frameDelayMs = 200;

  15. #define FRAME_NUM 4   // 关键帧帧数
  16. #define SIZE      8   // 点阵的行列数

  17. // 每一行的模值
  18. const byte row[] =
  19. {
  20.     0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80
  21. };

  22. // 人物走路的关键帧的取模
  23. const byte man[FRAME_NUM][SIZE] =
  24. {
  25.     {0xE7,0xE7,0xFF,0xC3,0xC5,0xE7,0xDB,0xBD},
  26.     {0xE7,0xE7,0xFF,0xE7,0xE3,0xE7,0xEB,0xDB},
  27.     {0xE7,0xE7,0xFF,0xE7,0xE7,0xE7,0xE7,0xE7},
  28.     {0xE7,0xE7,0xFF,0xE7,0xE3,0xE7,0xEB,0xDB}
  29. };

  30. void setup ()
  31. {
  32.     pinMode(latchPin, OUTPUT);
  33.     pinMode(clockPin, OUTPUT);
  34.     pinMode(dataPin,  OUTPUT);
  35. }

  36. void loop()
  37. {
  38.     static unsigned long lastTick = millis();
  39.     static unsigned char frame = 0;

  40.     // 延时一帧时间后,进入下一帧
  41.     if (millis() - lastTick >= frameDelayMs)
  42.     {
  43.         lastTick = millis();

  44.         ++frame;
  45.         if (frame >= FRAME_NUM)
  46.             frame = 0;
  47.     }

  48.     showArrPic(man[frame]);
  49. }

  50. // 显示数组所代表的图片
  51. void showArrPic(const byte arr[])
  52. {
  53.     for (unsigned char i = 0; i < SIZE; ++i)
  54.     {
  55.         digitalWrite(latchPin, LOW);
  56.         shiftOut(dataPin, clockPin, LSBFIRST, (byte) arr[i]);
  57.         shiftOut(dataPin, clockPin, LSBFIRST, (byte) row[i]);
  58.         digitalWrite(latchPin, HIGH);
  59.     }
  60. }

没有绘画天分,这全是手工点出来的点阵越看越不像,大家将就点吧:

netlhx 发表于 2016-11-26 11:30 | 显示全部楼层
好看,我也要学点灯
 楼主| starsnow 发表于 2016-11-26 12:02 | 显示全部楼层
netlhx 发表于 2016-11-26 11:30
好看,我也要学点灯

谢谢支持~~等灯等登!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

3

主题

17

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部