8*8LED点阵显示爱心的Arduino实现

七夕前用树莓派实验了下8*8 LED点阵显示一颗爱心图案。其实这个功能用Arduino更为合适。撇开成本因素,树莓派实现是依赖一个死循环的,我关注了一下CPU使用率,运行期间可能会升到27%的样子,摸了一下CPU散热片,发热还是有一点的。所以决定在Arduino上再来一次。

原理不变,可以继续参考树莓派的那篇。地址:http://blog.csdn.net/ki1381/article/details/52134935

源代码如下,接线方法在头部的定义中。loop中顺便加了一点循环和延时,实现了闪烁效果。

/*
   BOTH COL & ROW CHIPS ARE 74HC595
 */

#define RowData 10        // ROW CHIP PIN 14
 #define RowDataInput 8    // ROW CHIP PIN 11
 #define RowConfirm 9      // ROW CHIP PIN 12

#define ColData 7         // COL CHIP PIN 14
 #define ColDataInput 5    // COL CHIP PIN 11
 #define ColConfirm 6      // COL CHIP PIN 12

//#define EyeDelay 5

void setup() {
   // initialize digital pin 13 as an output.
   pinMode(5, OUTPUT);
   pinMode(6, OUTPUT);
   pinMode(7, OUTPUT);

  pinMode(8, OUTPUT);
   pinMode(9, OUTPUT);
   pinMode(10, OUTPUT);
 }

void ConfirmRowDataInput()
 {
   digitalWrite(RowDataInput, LOW);
   digitalWrite(RowDataInput, HIGH);
 }

void ConfirmRowDataAll()
 {
   digitalWrite(RowConfirm, LOW);
   digitalWrite(RowConfirm, HIGH);
 }

void ConfirmColDataInput()
 {
   digitalWrite(ColDataInput, LOW);
   digitalWrite(ColDataInput, HIGH);
 }

void ConfirmColDataAll()
 {
   digitalWrite(ColConfirm, LOW);
   digitalWrite(ColConfirm, HIGH);
 }

void SelectRow(int ID)
 {
   for (int i = 1; i <= 8; i++)
   {
     if (i == ID)
     {
       digitalWrite(RowData, HIGH);
     }
     else
     {
       digitalWrite(RowData, LOW);
     }

    ConfirmRowDataInput();

  }
   ConfirmRowDataAll();
 }

void SelectCol(char Data[8])
 {
   for (int i = 0; i < 8; i++)
   {
     if (Data[i] == '0')
       digitalWrite(ColData, HIGH);
     else
       digitalWrite(ColData, LOW);
     ConfirmColDataInput();
   }
   ConfirmColDataAll();
 }

void ClearAll()
 {
   for (int i = 1; i <= 8; i++)
   {
     digitalWrite(RowData, LOW);
     ConfirmRowDataInput();
   }
   ConfirmRowDataAll();

  for (int i = 1; i <= 8; i++)
   {
     digitalWrite(ColData, HIGH);
     ConfirmColDataInput();
   }
   ConfirmColDataAll();

}

void ShowHeart()
 {
   ClearAll();
   SelectRow(1);
   SelectCol("01101100");
   //delay(EyeDelay);

  ClearAll();
   SelectRow(2);
   SelectCol("10010010");
   //delay(EyeDelay);

  ClearAll();
   SelectRow(3);
   SelectCol("10000010");
   //delay(EyeDelay);

  ClearAll();
   SelectRow(4);
   SelectCol("01000100");
   //delay(EyeDelay);

  ClearAll();
   SelectRow(5);
   SelectCol("00101000");
   //delay(EyeDelay);

  ClearAll();
   SelectRow(6);
   SelectCol("00010000");
   //delay(EyeDelay);

}


 void loop() {
   for (int i = 0; i < 200; i++)
   {
     ClearAll();
     ShowHeart();
   }

  ClearAll();
   delay(800);

}






你可能感兴趣的:(树莓派,&,Arduino)