极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 26396|回复: 17

Atmega最简系统+8个LED效果闪烁

[复制链接]
发表于 2012-11-8 00:06:21 | 显示全部楼层 |阅读模式
本帖最后由 smokesea 于 2012-11-8 00:24 编辑

代码就开源吧,本就是给儿子的模型车准备的.代码个人而言还是非常满意的,因为灯的效果是可以随时进行调整和组织的.并且,如果只调整标准效果的话简单调整数组即可.
就算是开发更多的效果需要添加的代码也很少.
这次亲身体验了"ATMega8L最小系统"后,感觉可以玩儿的东西更多了.
关键是太便宜了,哈哈.
视频等审核通过后就放上来,效果很赞的哦.
视频链接





  1. int ERR = 0;

  2. //所有灯默认关闭
  3. #define ACTION_TYPE_STAND           -1//标准动作 ACTION的影响范围,ACTION的亮度,ACTION持续时间
  4. #define ACTION_TYPE_LOOP        -2//想要重复的ACTION的ID 想要重复的次数,第三个参数意思是当前已经重复了几次 >0表示重复的次数,<=0表示永远循环
  5. #define ACTION_TYPE_ACTION_END  -4//每个Action的结束标记
  6. //独立的特效类型
  7. #define ACTION_TYPE_ACTION_ROLLING -5 //跑马灯
  8. #define ACTION_TYPE_ACTION_MID_2_SIDE -6 //从中间向两边点亮
  9. #define ACTION_TYPE_ACTION_SIDE_2_MID -7 //从两边向中间点亮


  10. //定义ACTION的控制范围 >0的就表示单个灯的表现
  11. #define ACTION_ORDER_ALL         -11  //所有灯
  12. #define ACTION_ORDER_ODD           -12 //奇数灯
  13. #define ACTION_ORDER_EVEN          -13 //偶数灯

  14. //定义ACTION的类型 >0表示指定亮度
  15. #define ACTION_LIGHT_HIGH  -111 //灯亮度最大
  16. #define ACTION_LIGHT_MID   -123 //灯亮度剧中
  17. #define ACTION_LIGHT_LOW   -124 //灯关闭

  18. //定义ACTION的持续时间
  19. #define ACTION_KEEP_LONG  200
  20. #define ACTION_KEEP_SHORT  100
  21. #define ACTION_KEEP_ZERO   0 //等待为0,只为控制某些灯

  22. //ACTION列表,最多容纳255个action,当action开始play时将每个ACTIONTYPE的索引记录到这里
  23. int action_index = 0;
  24. int action_list[255];
  25. //ACTION的明细,一行是一组
  26. int action_detail[]={
  27.                 //所有灯亮起
  28.                 ACTION_TYPE_STAND,ACTION_ORDER_ALL,ACTION_LIGHT_HIGH,ACTION_KEEP_SHORT,ACTION_TYPE_ACTION_END,
  29.                 //所有灯降低亮度
  30.                 ACTION_TYPE_STAND,ACTION_ORDER_ALL,ACTION_LIGHT_LOW,ACTION_KEEP_SHORT,ACTION_TYPE_ACTION_END,
  31.                 //从0开始重复
  32.                 ACTION_TYPE_LOOP,0,2,0/*重复了几次*/,ACTION_TYPE_ACTION_END,
  33.                
  34.                 //奇数灯高亮
  35.                 ACTION_TYPE_STAND,ACTION_ORDER_EVEN,ACTION_LIGHT_LOW,ACTION_KEEP_ZERO,ACTION_TYPE_ACTION_END,
  36.                 ACTION_TYPE_STAND,ACTION_ORDER_ODD,ACTION_LIGHT_HIGH,ACTION_KEEP_LONG,ACTION_TYPE_ACTION_END,
  37.                
  38.                 //偶数灯高亮
  39.                 ACTION_TYPE_STAND,ACTION_ORDER_ODD,ACTION_LIGHT_LOW,ACTION_KEEP_ZERO,ACTION_TYPE_ACTION_END,
  40.                 ACTION_TYPE_STAND,ACTION_ORDER_EVEN,ACTION_LIGHT_HIGH,ACTION_KEEP_LONG,ACTION_TYPE_ACTION_END,
  41.                 //从2行开始重复
  42.                 ACTION_TYPE_LOOP,-4,2,0/*重复了几次*/,ACTION_TYPE_ACTION_END,

  43.                 //跑马灯
  44.                 ACTION_TYPE_ACTION_ROLLING,4/*循环总次数*/,ACTION_TYPE_ACTION_END,
  45.                
  46.                 //从中间向两边点亮
  47.                 ACTION_TYPE_ACTION_MID_2_SIDE,ACTION_TYPE_ACTION_END,
  48.                 //从两边向两边点亮
  49.                 ACTION_TYPE_ACTION_SIDE_2_MID,ACTION_TYPE_ACTION_END,
  50.                 //从-2行开始重复
  51.                 ACTION_TYPE_LOOP,-2,2,0/*重复了几次*/,ACTION_TYPE_ACTION_END,
  52.                 //跑马灯
  53.                 ACTION_TYPE_ACTION_ROLLING,2/*循环总次数*/,ACTION_TYPE_ACTION_END,
  54. //奇数灯高亮
  55.                 ACTION_TYPE_STAND,ACTION_ORDER_EVEN,ACTION_LIGHT_LOW,ACTION_KEEP_ZERO,ACTION_TYPE_ACTION_END,
  56.                 ACTION_TYPE_STAND,ACTION_ORDER_ODD,ACTION_LIGHT_HIGH,ACTION_KEEP_LONG,ACTION_TYPE_ACTION_END,
  57.                
  58.                 //偶数灯高亮
  59.                 ACTION_TYPE_STAND,ACTION_ORDER_ODD,ACTION_LIGHT_LOW,ACTION_KEEP_ZERO,ACTION_TYPE_ACTION_END,
  60.                 ACTION_TYPE_STAND,ACTION_ORDER_EVEN,ACTION_LIGHT_HIGH,ACTION_KEEP_LONG,ACTION_TYPE_ACTION_END,
  61. //从-2行开始重复
  62.                 ACTION_TYPE_LOOP,-2,2,0/*重复了几次*/,ACTION_TYPE_ACTION_END,
  63.                 //从0开始重复
  64.                 ACTION_TYPE_LOOP,0,2,0/*重复了几次*/,ACTION_TYPE_ACTION_END
  65.                
  66.         };



  67. int leds[]={2,3,4,5,6,7,8,9};
  68. int led_count = 8;


  69. int action_detail_index = 0;

  70. int set_led_light(int pin, int light)
  71. {
  72.         if(light > 0)
  73.         {
  74.                 return light;
  75.         }
  76.        
  77.         if(light == ACTION_LIGHT_HIGH)
  78.         {
  79.                 digitalWrite(pin,HIGH);
  80.         }else if(light = ACTION_LIGHT_LOW)
  81.         {
  82.                 digitalWrite(pin,LOW);
  83.         }
  84. }

  85. void action_play()
  86. {
  87.         int ac = sizeof(action_detail)/sizeof(int);
  88.         for(int i = action_detail_index; i < ac; i++)
  89.         {
  90.                 if(action_detail[action_detail_index] == ACTION_TYPE_STAND)
  91.                 {
  92.                         parser_action_stand();
  93.                         return ;
  94.                 }else if(action_detail[action_detail_index] == ACTION_TYPE_LOOP)
  95.                 {
  96.                         parser_action_loop();
  97.                         return ;
  98.                 }else if(action_detail[action_detail_index] == ACTION_TYPE_ACTION_ROLLING)
  99.                 {
  100.                         parser_action_rolling();
  101.                 }
  102.                 else if(action_detail[action_detail_index] == ACTION_TYPE_ACTION_MID_2_SIDE)
  103.                 {
  104.                         parser_action_mid_2_side();
  105.                 }
  106.                 else if(action_detail[action_detail_index] == ACTION_TYPE_ACTION_SIDE_2_MID)
  107.                 {
  108.                         parser_action_side_2_mid();
  109.                 }
  110.         }
  111.         action_detail_index = 0;
  112.         action_index = 0;
  113.         //全部播放完毕,清除loop类型的计数
  114.         int act_idx = 0;
  115.         for(int i = 0 ; i < 255; i++)
  116.         {
  117.                 if(action_list[i]==-1)
  118.                 {
  119.                         break;
  120.                 }
  121.                 act_idx = action_list[i];
  122.                 if(action_detail[act_idx] == ACTION_TYPE_LOOP)
  123.                 {
  124.                         action_detail[act_idx+3] = 0;
  125.                 }
  126.         }
  127. }
  128. //解析标准事件
  129. void parser_action_stand()
  130. {
  131.         action_list[action_index] = action_detail_index;
  132.         int order = action_detail[action_detail_index+1];
  133.         int light = action_detail[action_detail_index+2];
  134.         int _delay = action_detail[action_detail_index+3];
  135.         int end = action_detail[action_detail_index+4];
  136.         if(end != ACTION_TYPE_ACTION_END)
  137.         {
  138.                 Serial.print("action_detail_index");
  139.                 Serial.println(action_detail_index);
  140.                 ERR = 1;
  141.                 return ;
  142.         }
  143.         //设置索引
  144.         action_detail_index+=5;
  145.         action_index++;
  146.        
  147.         if(order == ACTION_ORDER_ALL)
  148.         {
  149.                 for(int i = 0; i < led_count;i++)
  150.                 {
  151.                         set_led_light(leds[i],light);
  152.                 }
  153.                
  154.         }else if(order == ACTION_ORDER_ODD )
  155.         {
  156.                 for(int i = 0; i < led_count;i++)
  157.                 {
  158.                         if(i%2==1)
  159.                         {
  160.                                 set_led_light(leds[i],light);
  161.                         }
  162.                 }
  163.                
  164.         }else if(order == ACTION_ORDER_EVEN)
  165.         {
  166.                 for(int i = 0; i < led_count;i++)
  167.                 {
  168.                         if(i%2==0)
  169.                         {
  170.                                 set_led_light(leds[i],light);
  171.                         }
  172.                 }
  173.         }
  174.         if(delay>0)
  175.         {
  176.                 delay(_delay);
  177.         }
  178. }

  179. //解析循环事件
  180. void parser_action_loop()
  181. {
  182.         action_list[action_index] = action_detail_index;
  183.         int act_idx = action_detail[action_detail_index+1];
  184.         int loop_count = action_detail[action_detail_index+2];
  185.         int count = action_detail[action_detail_index+3];
  186.         int end = action_detail[action_detail_index+4];
  187.         if(end != ACTION_TYPE_ACTION_END)
  188.         {
  189.                 ERR = 2;
  190.                 return ;
  191.         }
  192.         //永远重复
  193.         if(loop_count <=0)
  194.         {
  195.                 action_detail_index=0;
  196.                 action_index=0;
  197.                 return ;
  198.         }
  199.         count++;
  200.         action_detail[action_detail_index+3]=count;
  201.         //当前循环的次数>规定次数了,继续往下走
  202.         if(count > loop_count)
  203.         {
  204.                 //设置索引
  205.                 action_detail_index+=5;
  206.                 action_index++;
  207.         }else
  208.         {
  209.                 if(act_idx >= 0)
  210.                 {
  211.                         action_index=act_idx;
  212.                 }else
  213.                 {
  214.                         action_index+=act_idx;
  215.                         if(action_index < 0)
  216.                         {
  217.                                 action_detail_index = 0;
  218.                         }
  219.                 }
  220.                 action_detail_index = action_list[action_index];
  221.         }
  222.        
  223. }

  224. //解析跑马灯
  225. void parser_action_rolling()
  226. {
  227.         action_list[action_index] = action_detail_index;
  228.        
  229.         int loop = action_detail[action_detail_index+1];
  230.         int end = action_detail[action_detail_index+2];
  231.         if(end != ACTION_TYPE_ACTION_END)
  232.         {
  233.                 ERR = 3;
  234.                 return ;
  235.         }
  236.         for(int i = 0 ; i < led_count;i++)
  237.         {
  238.                 set_led_light(leds[i], ACTION_LIGHT_LOW);
  239.         }
  240.         delay(10);
  241.         //奇数从左向右,偶数从右向左
  242.         for(int l = 0 ; l < loop; l++)
  243.         {
  244.                 if(l %2 == 1)
  245.                 {
  246.                        
  247.                         for(int i = 0 ; i < led_count ; i++)
  248.                         {
  249.                                 set_led_light(leds[i],ACTION_LIGHT_HIGH);
  250.                                
  251.                                 delay(50);
  252.                         }
  253.                         for(int i = 0 ; i < led_count ; i++)
  254.                         {
  255.                                 set_led_light(leds[i],ACTION_LIGHT_LOW);
  256.                                 delay(50);
  257.                         }
  258.                 }else
  259.                 {
  260.                         for(int i = led_count -1 ; i >= 0 ; i--)
  261.                         {
  262.                                 set_led_light(leds[i],ACTION_LIGHT_HIGH);
  263.                                 delay(50);
  264.                         }
  265.                         for(int i = led_count - 1 ; i >= 0 ; i--)
  266.                         {
  267.                                 set_led_light(leds[i],ACTION_LIGHT_LOW);
  268.                                 delay(50);
  269.                         }
  270.                 }
  271.         }
  272.         action_detail_index+=3;
  273.         action_index++;
  274. }

  275. //解析从中间向两边点亮
  276. void parser_action_mid_2_side()
  277. {
  278.         action_list[action_index] = action_detail_index;
  279.         int _end = action_detail[action_detail_index+1];
  280.         if(_end != ACTION_TYPE_ACTION_END)
  281.         {
  282.                 ERR = 4;
  283.                 return ;
  284.         }
  285.         for(int i = 0 ; i < led_count;i++)
  286.         {
  287.                 set_led_light(leds[i], ACTION_LIGHT_LOW);
  288.         }
  289.         delay(10);
  290.         //从中间向两边点亮
  291.         int mid = led_count / 2;
  292.         for(int l = 0 ; l < led_count / 2; l++)
  293.         {
  294.                 set_led_light(leds[mid - l - 1], ACTION_LIGHT_HIGH);
  295.                 set_led_light(leds[mid + l] , ACTION_LIGHT_HIGH);
  296.                 delay(100);
  297.         }
  298.         action_detail_index+=2;
  299.         action_index++;
  300. }

  301. //解析从两边向中间点亮
  302. void parser_action_side_2_mid()
  303. {
  304.         action_list[action_index] = action_detail_index;
  305.         int _end = action_detail[action_detail_index+1];
  306.         if(_end != ACTION_TYPE_ACTION_END)
  307.         {
  308.                 ERR = 5;
  309.                 return ;
  310.         }
  311.         for(int i = 0 ; i < led_count;i++)
  312.         {
  313.                 set_led_light(leds[i], ACTION_LIGHT_LOW);
  314.         }
  315.         delay(10);
  316.         //从中间向两边点亮
  317.         int mid = led_count / 2;
  318.         for(int l = 0 ; l < led_count / 2; l++)
  319.         {
  320.                 set_led_light(leds[l] , ACTION_LIGHT_HIGH);
  321.                 set_led_light(leds[led_count - 1 -l] , ACTION_LIGHT_HIGH);
  322.                 delay(100);
  323.         }
  324.         action_detail_index+=2;
  325.         action_index++;
  326. }


  327. void setup()
  328. {
  329.   for(int i = 2 ; i < 10; i++)
  330.   {
  331.     pinMode(i,OUTPUT);
  332.     digitalWrite(i,LOW);
  333.   }
  334.   for(int i = 0 ; i < 255; i++)
  335.   {
  336.           action_list[i]=-1;
  337.   }
  338.   Serial.begin(9600);
  339. }

  340. void loop()
  341. {
  342.         if(ERR != 0)
  343.         {
  344.                 delay(1000);
  345.                 Serial.print("Error");
  346.                 Serial.println(ERR);
  347.                 return ;
  348.         }
  349.   action_play();
  350.   delay(8);
  351. }

复制代码



本帖子中包含更多资源

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

x
回复

使用道具 举报

发表于 2012-11-8 08:26:07 | 显示全部楼层
线好多
回复 支持 反对

使用道具 举报

发表于 2012-11-8 08:44:43 | 显示全部楼层
这次亲身体验了"ATMega8L最小系统"后,感觉可以玩儿的东西更多了.
关键是太便宜了


同感啊,8L的低电压用纽扣电池应该可以带动,可以做很多小型应用
回复 支持 反对

使用道具 举报

发表于 2012-11-8 09:19:13 | 显示全部楼层
8L的低电压用纽扣电池应该可以带动+1,支持的就是这个!哈哈!
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-11-8 11:23:32 | 显示全部楼层
玩具车是12v的电池盒,找了一个贴片的ASM1117来稳压到5v,焊接的时候可把我整惨了。中午买个焊台去
回复 支持 反对

使用道具 举报

发表于 2012-11-8 12:31:03 | 显示全部楼层
smokesea 发表于 2012-11-8 11:23
玩具车是12v的电池盒,找了一个贴片的ASM1117来稳压到5v,焊接的时候可把我整惨了。中午买个焊台去

为了个玩具买个焊台是因为静电?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-11-8 14:32:10 | 显示全部楼层
ttyp 发表于 2012-11-8 12:31
为了个玩具买个焊台是因为静电?

是带机械臂的那种,焊小东西太费劲了,没事瞎玩儿呗,就是个业余爱好
回复 支持 反对

使用道具 举报

发表于 2012-11-8 18:17:39 | 显示全部楼层
加点pwm吧  
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-11-8 18:26:36 | 显示全部楼层
Malc 发表于 2012-11-8 18:17
加点pwm吧

ATMega8只有两个pwm,所以没有使用
回复 支持 反对

使用道具 举报

发表于 2012-11-8 18:44:56 | 显示全部楼层
smokesea 发表于 2012-11-8 18:26
ATMega8只有两个pwm,所以没有使用

软件pwm,要多少有多少
回复 支持 反对

使用道具 举报

发表于 2012-11-8 22:05:01 | 显示全部楼层
{:soso_e179:}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-11-9 10:06:59 | 显示全部楼层
Malc 发表于 2012-11-8 18:44
软件pwm,要多少有多少

太慢了,8L有点吃不消
回复 支持 反对

使用道具 举报

发表于 2012-12-17 00:53:39 | 显示全部楼层
{:soso_e136:}代码好长,跑马灯那么长的呢
回复 支持 反对

使用道具 举报

发表于 2012-12-17 00:54:43 | 显示全部楼层
效果不错,哈哈
回复 支持 反对

使用道具 举报

发表于 2012-12-21 12:57:22 | 显示全部楼层
真心看不懂这个写程序的方式

能告诉我这样的程序是用的什么原理写出来的吗?
回复 支持 反对

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-9 04:02 , Processed in 0.043188 second(s), 25 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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