arduino随笔(3)

红绿灯


//2016-4-12
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

#include "TM1637.h"
#define CLK 3//pins definitions for TM1637 and can be changed to other ports       
#define DIO 2
#define R_COLOR 12
#define G_COLOR 13


TM1637 tm1637(CLK,DIO);
int num=0;
int pinmode[2]={R_COLOR,G_COLOR};
int ledno=0;
int oldled=-1;


void setup()
{
  tm1637.init();
  tm1637.set(BRIGHT_TYPICAL);//BRIGHT_TYPICAL = 2,BRIGHT_DARKEST = 0,BRIGHTEST = 7;    
  pinMode(pinmode[0],OUTPUT);    
  pinMode(pinmode[1],OUTPUT); 
}

void loop()
{     
     if (num >0){
         num--;  
     }
     else{   
         if (oldled!=-1){
             closeLed(oldled);             
         }  
         showLed(ledno);
         oldled=ledno;         

         if (ledno==0){
             ledno=1;
         }else{
             ledno=0;
         }    
         num=10;
     }   
     showNumber();     
     delay(1000);      
}

void showNumber(){
     int8_t NumTab[] = {0,1,2,3,4,5,6,7,8,9};//0~9
     int8_t ListDisp[4];
     unsigned char i = 0;
     unsigned char count = 0;
     int numshow[]={num/1000,num%1000/100,num%100/10,num%10};
     for(unsigned char BitSelect = 0;BitSelect < 4;BitSelect ++)
     {
        ListDisp[BitSelect] = numshow[BitSelect];
     }
     tm1637.display(0,ListDisp[0]);
     tm1637.display(1,ListDisp[1]); 
     tm1637.display(2,ListDisp[2]);
     tm1637.display(3,ListDisp[3]);   
}

void showLed(int index){
    digitalWrite(pinmode[index],HIGH); 
}

void closeLed(int index){
    digitalWrite(pinmode[index],LOW);  
}

本博客所有内容是原创,如果转载请注明来源

http://blog.csdn.net/myhaspl/




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值