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/



arduino随笔(3)_第1张图片

你可能感兴趣的:(软件与计算)