查看: 10434|回复: 6

[经验] LilyPad Arduino USB 板的有趣程序

[复制链接]
  • TA的每日心情
    开心
    2021-12-10 15:56
  • 签到天数: 2675 天

    连续签到: 1 天

    [LV.Master]伴坛终老

    发表于 2015-7-22 10:50:51 | 显示全部楼层 |阅读模式
    分享到:
    本帖最后由 slotg 于 2015-7-22 10:54 编辑

    DSC01302.JPG

    这是对 6 月份铜板兑换的 LilyPad Arduino USB 开发板所做的小程序。

    LilyPad Arduino USB 板最初的目标就是设计成可装配在个人随身物品或是纺织品上的穿戴式微控板,透过导电的织布来连接 LilyPad 板与 LED 灯来产生一些炫目的效果。最近我将板子连接上 RGB LED 灯与振动开关设计了 2 个有趣的程序,板子的连接方式如下:

    D11     B
    D10     R
    D9      G
    D3      振动开关

    第一个程序:

    在平常的情况下 RGB LED 灯以每间隔 1 秒钟的时间更换显示颜色,当侦测到振动开关接点接触时会以目前的颜色快速闪烁 2 秒钟,然后再恢复到每间隔 1 秒钟更换显示颜色。

    程序呼叫了 millis() 函数获取板子运行的时间长度做为程序延时使用,返回的数据类型是 unsigned long,每一个单位是 1ms,类型 unsigned long 的数据长度是 32 位,可以表示的范围是 0 到 4,294,967,295 (2^32 - 1),这个意思是表示板子在程序不关电连续运行 50 天之后返回的数据会产生 overflow 的情况!不过我们先不考虑这个问题。

    程序代码:
    1. const int RLEDPin = 10;
    2. const int GLEDPin = 9;
    3. const int BLEDPin = 11;
    4. const int SWPin = 3;

    5. byte idx = 0;
    6. unsigned long preMillis1 = 0;
    7. unsigned long preMillis2 = 0;
    8. int duty = 1000;
    9. boolean fg_sw = false;

    10. void setup() {
    11.   pinMode(RLEDPin, OUTPUT);
    12.   pinMode(GLEDPin, OUTPUT);
    13.   pinMode(BLEDPin, OUTPUT);
    14.   pinMode(SWPin, INPUT);
    15. }

    16. void loop() {
    17.   unsigned long curMillis = millis();

    18.   if(digitalRead(SWPin) == LOW && fg_sw == false) {
    19.     preMillis2 = curMillis;
    20.     duty = 70;
    21.     fg_sw = true;
    22.     preMillis1 = curMillis + duty;
    23.   }

    24.   if(fg_sw == true) {
    25.     if(curMillis - preMillis2 >= 2000) {    // 2 sec.
    26.       duty = 1000;
    27.       fg_sw = false;
    28.       preMillis1 = curMillis + duty;
    29.     }
    30.   }

    31.   if(curMillis - preMillis1 >= duty) {
    32.     preMillis1 = curMillis;

    33.     if(fg_sw == false) {
    34.       NextColor();    // LED display next color
    35.     }
    36.     else {
    37.       LEDflash();     // LED flash
    38.     }
    39.   }

    40. }

    41. // LED display next color
    42. void NextColor() {
    43.   digitalWrite(RLEDPin, LOW);
    44.   digitalWrite(GLEDPin, LOW);
    45.   digitalWrite(BLEDPin, LOW);

    46.   if(++idx >= 3) {
    47.     idx = 0;
    48.   }

    49.   DsLED();    // Display LED color
    50. }

    51. // LED flash
    52. void LEDflash() {
    53.   static boolean flg = false;

    54.   flg = !flg;
    55.   if(flg == false) {
    56.     digitalWrite(RLEDPin, LOW);
    57.     digitalWrite(GLEDPin, LOW);
    58.     digitalWrite(BLEDPin, LOW);
    59.   }
    60.   else {
    61.     DsLED();    // Display LED color
    62.   }
    63. }

    64. // Display LED color
    65. void DsLED() {
    66.   switch(idx) {
    67.     case 0:
    68.       digitalWrite(RLEDPin, HIGH);
    69.       break;
    70.     case 1:
    71.       digitalWrite(GLEDPin, HIGH);
    72.       break;
    73.     case 2:
    74.       digitalWrite(BLEDPin, HIGH);
    75.       break;
    76.   }
    77. }
    复制代码
    视频演示:
    http://v.youku.com/v_show/id_XMTI5MDUwOTk3Mg==.html
    DEMO11.gif


    第二个程序:

    每摇晃一次板子会将 RGB LED 灯的显示更换颜色,在更换颜色后程序会有 0.2 秒的时间不再侦测摇动开关的动作,这个是为了消除接点弹跳的影响。

    程序代码:
    1. const int RLEDPin = 10;
    2. const int GLEDPin = 9;
    3. const int BLEDPin = 11;
    4. const int SWPin = 3;

    5. byte idx = 0;
    6. unsigned long preMillis = 0;
    7. boolean fg_sw = false;

    8. void setup() {
    9.   pinMode(RLEDPin, OUTPUT);
    10.   pinMode(GLEDPin, OUTPUT);
    11.   pinMode(BLEDPin, OUTPUT);
    12.   pinMode(SWPin, INPUT);

    13.   DsLED();    // Display LED color
    14. }

    15. void loop() {
    16.   unsigned long curMillis = millis();

    17.   if(digitalRead(SWPin) == LOW && fg_sw == false) {
    18.     preMillis = curMillis;
    19.     fg_sw = true;
    20.     NextColor();    // LED display next color
    21.   }

    22.   if(fg_sw == true) {
    23.     if(curMillis - preMillis >= 200) {
    24.       fg_sw = false;
    25.     }
    26.   }
    27. }

    28. // LED display next color
    29. void NextColor() {
    30.   digitalWrite(RLEDPin, LOW);
    31.   digitalWrite(GLEDPin, LOW);
    32.   digitalWrite(BLEDPin, LOW);

    33.   if(++idx >= 3) {
    34.     idx = 0;
    35.   }

    36.   DsLED();    // Display LED color
    37. }

    38. // Display LED color
    39. void DsLED() {
    40.   switch(idx) {
    41.     case 0:
    42.       digitalWrite(RLEDPin, HIGH);
    43.       break;
    44.     case 1:
    45.       digitalWrite(GLEDPin, HIGH);
    46.       break;
    47.     case 2:
    48.       digitalWrite(BLEDPin, HIGH);
    49.       break;
    50.   }
    51. }
    复制代码
    视频演示:
    http://v.youku.com/v_show/id_XMTI5MDUxMjMwNA==.html
    DEMO22.gif


    评分

    参与人数 1与非币 +5 收起 理由
    loveeeboard + 5 三周年铜板双倍!

    查看全部评分

    回复

    使用道具 举报

  • TA的每日心情
    奋斗
    2020-9-28 10:10
  • 签到天数: 1018 天

    连续签到: 1 天

    [LV.10]以坛为家III

    发表于 2015-7-22 17:33:31 | 显示全部楼层
    好贴!!!
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    无聊
    2017-3-23 12:01
  • 签到天数: 7 天

    连续签到: 1 天

    [LV.3]偶尔看看II

    发表于 2015-7-22 17:35:14 | 显示全部楼层
    好 很好 非常好 顶
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    郁闷
    2013-9-12 12:12
  • 签到天数: 1 天

    连续签到: 1 天

    [LV.1]初来乍到

    发表于 2015-7-22 17:35:38 | 显示全部楼层
    高大上!!!
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    奋斗
    2020-2-7 11:49
  • 签到天数: 2193 天

    连续签到: 33 天

    [LV.Master]伴坛终老

    发表于 2015-7-23 15:19:34 | 显示全部楼层
    赞一个!还有源码,真不错!
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2017-7-21 12:58
  • 签到天数: 907 天

    连续签到: 2 天

    [LV.10]以坛为家III

    发表于 2015-8-13 09:23:27 | 显示全部楼层
    非常good!!!10000
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    无聊
    2020-6-20 21:18
  • 签到天数: 8 天

    连续签到: 1 天

    [LV.3]偶尔看看II

    发表于 2020-1-7 01:01:46 | 显示全部楼层
    挺有意思的
    回复 支持 反对

    使用道具 举报

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

    本版积分规则

    关闭

    站长推荐上一条 /2 下一条



    手机版|小黑屋|与非网

    GMT+8, 2024-4-20 02:21 , Processed in 0.190771 second(s), 30 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.