先從簡單的LED控制開始吧
另外,因為我不是軟體出身,所以code寫得很爛,請多包涵:D

上面的影片,主要是利用Arduino的數位輸出腳,來控制8x8的LED
顯示0~9的字型

首先,利用手動來控制 8x8 LED發亮,並且了解一下腳位如何控制
這顆8x8 LED是在光華商場對面樓下的電子材料行買的
料號: LM12883SRWK 共陰極 矩陣式8x8 LED

如何確認控制腳位? 先在excel框好8x8的格子

左邊的圖,上面那排是LED的正極(Anode)腳位4,7,2,8,12,1,14,9
左邊那排是LED的負極(Cathode)腳位13,6,5,10,3,11,15,16


如果要讓第一行,第一列(a)的LED發亮,就要在腳位4灌電壓,腳位13下地
(b) = pin7 (+),pin6 (GND) 依此類推
但唯一的問題就是,因為這只有16支腳,若要同時間做出fig-2這種顯示是無法的
因為,當你亮(j) = pin4(+) , pin6(GND) , 同時要亮(k) = pin7(+) , pin13(GND)
這時候pin4(+) , pin13(-) = (a) , pin7(+) , pin6(GND) = (b)就會變成fig-3
不過這問題,稍後我們可以利用軟體的小技巧來達到fig-2的效果


接下來在excel上面先畫好我們想要的字型,例如0~9
因為Arduino Uno只有提供14支數位的輸出腳,所以我們用6x6的格子來畫
所以6+6=12支腳,另外我們正腳位使用7,2,8,12,1,14
對應Arduino的數位輸出腳1~6 , 負腳位依此類推


根據這個電路圖,在每個LED正腳位回到DIGITAL輸出之間串220ohm的電阻
避免過大電流燒掉LED或是造成亮度不均的問題

接下來是原始碼

void num0(int a) //設定數字0要怎麼顯示,請參考上圖
{
digitalWrite(7, LOW); //把想亮的列設成Low,不想亮的設成High
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(3, HIGH); //把pin3設成High(+5V),這時就會出現fig-1
delay(a); //停頓一個時間
digitalWrite(3, LOW); //把pin3設成Low,這時候就會全部不亮

digitalWrite(7, LOW); //把想亮的列設成Low,不想亮的設成High
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
digitalWrite(12, LOW);
digitalWrite(4, HIGH); //把pin4設成High(+5V),這時就會出現fig-2
delay(a); //停頓一個時間
digitalWrite(4, LOW); //把pin4設成Low,這時候就會全部不亮

digitalWrite(7, LOW); //把想亮的列設成Low,不想亮的設成High
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(5, HIGH); //把pin5設成High(+5V),這時就會出現fig-3
delay(a); //停頓一個時間
digitalWrite(5, LOW); //把pin5設成Low,這時候就會全部不亮
}
//由於delay的時間非常短,利用人眼視覺暫留的方式快速的亮暗每一行
//最後眼睛就會看到0這個數字,也就是fig-4
//下列1~9的字型,就按照數字0的方式編寫
void num1(int a)
{
digitalWrite(7, HIGH);
digitalWrite(8, LOW);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
digitalWrite(12, LOW);
digitalWrite(3, HIGH);
delay(a);
digitalWrite(3, LOW);

digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(4, HIGH);
delay(a);
digitalWrite(4, LOW);

digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
digitalWrite(12, LOW);
digitalWrite(5, HIGH);
delay(a);
digitalWrite(5, LOW);
}
void num2(int a)
{
digitalWrite(7, LOW);
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(3, HIGH);
delay(a);
digitalWrite(3, LOW);

digitalWrite(7, LOW);
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
digitalWrite(12, LOW);
digitalWrite(4, HIGH);
delay(a);
digitalWrite(4, LOW);

digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
digitalWrite(12, LOW);
digitalWrite(5, HIGH);
delay(a);
digitalWrite(5, LOW);
}
void num3(int a)
{
digitalWrite(7, LOW);
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
digitalWrite(12, LOW);
digitalWrite(3, HIGH);
delay(a);
digitalWrite(3, LOW);

digitalWrite(7, LOW);
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
digitalWrite(12, LOW);
digitalWrite(4, HIGH);
delay(a);
digitalWrite(4, LOW);

digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(5, HIGH);
delay(a);
digitalWrite(5, LOW);
}
void num4(int a)
{
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
digitalWrite(12, HIGH);
digitalWrite(3, HIGH);
delay(a);
digitalWrite(3, LOW);

digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
digitalWrite(12, HIGH);
digitalWrite(4, HIGH);
delay(a);
digitalWrite(4, LOW);

digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(5, HIGH);
delay(a);
digitalWrite(5, LOW);
}
void num5(int a)
{
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
digitalWrite(12, LOW);
digitalWrite(3, HIGH);
delay(a);
digitalWrite(3, LOW);

digitalWrite(7, LOW);
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
digitalWrite(12, LOW);
digitalWrite(4, HIGH);
delay(a);
digitalWrite(4, LOW);

digitalWrite(7, LOW);
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(5, HIGH);
delay(a);
digitalWrite(5, LOW);
}
void num6(int a)
{
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(3, HIGH);
delay(a);
digitalWrite(3, LOW);

digitalWrite(7, LOW);
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
digitalWrite(12, LOW);
digitalWrite(4, HIGH);
delay(a);
digitalWrite(4, LOW);

digitalWrite(7, LOW);
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(5, HIGH);
delay(a);
digitalWrite(5, LOW);
}
void num7(int a)
{
digitalWrite(7, LOW);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
digitalWrite(12, HIGH);
digitalWrite(3, HIGH);
delay(a);
digitalWrite(3, LOW);

digitalWrite(7, LOW);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
digitalWrite(12, HIGH);
digitalWrite(4, HIGH);
delay(a);
digitalWrite(4, LOW);

digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(5, HIGH);
delay(a);
digitalWrite(5, LOW);
}
void num8(int a)
{
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(3, HIGH);
delay(a);
digitalWrite(3, LOW);

digitalWrite(7, LOW);
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
digitalWrite(12, LOW);
digitalWrite(4, HIGH);
delay(a);
digitalWrite(4, LOW);

digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(5, HIGH);
delay(a);
digitalWrite(5, LOW);
}
void num9(int a)
{
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
digitalWrite(12, HIGH);
digitalWrite(3, HIGH);
delay(a);
digitalWrite(3, LOW);

digitalWrite(7, LOW);
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
digitalWrite(12, HIGH);
digitalWrite(4, HIGH);
delay(a);
digitalWrite(4, LOW);

digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(5, HIGH);
delay(a);
digitalWrite(5, LOW);
}
void setup() //設定Arduino的digital pin 1~12為輸出
{
pinMode(1, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
}
int displayspeed = 50; //設定顯示每個數字間所花的時間
int blinking = 8; //設定一個數字閃爍的程度,這個值可微調
int a = 0; //設定一個參數a=0

void loop() //主程式
{
while(a < displayspeed) //讓0這個字多跑幾次迴圈,來達到數字0顯示的時間
{
num0(blinking); //把blinking=8這個值,塞入副程式的delay時間
a++;
}
a = 0; //重置a=0
while(a < displayspeed)
{
num1(blinking);
a++;
}
a = 0;
while(a < displayspeed)
{
num2(blinking);
a++;
}
a = 0;
while(a < displayspeed)
{
num3(blinking);
a++;
}
a = 0;
while(a < displayspeed)
{
num4(blinking);
a++;
}
a = 0;
while(a < displayspeed)
{
num5(blinking);
a++;
}
a = 0;
while(a < displayspeed)
{
num6(blinking);
a++;
}
a = 0;
while(a < displayspeed)
{
num7(blinking);
a++;
}
a = 0;
while(a < displayspeed)
{
num8(blinking);
a++;
}
a = 0;
while(a < displayspeed)
{
num9(blinking);
a++;
}
a = 0;
}


以上
有問題的請發問
ps:我的code寫得很爛,請鞭小力一點.....XD



歡迎加入粉絲團
有即時的網站更新通知,還可以留言發問唷
在圖示點"讚",就可以啦~~
arrow
arrow
    創作者介紹
    創作者 洛斯里克 的頭像
    洛斯里克

    洛斯里克的夏天

    洛斯里克 發表在 痞客邦 留言(4) 人氣()