在使用查表法做出圖案之前,我們要中途來玩一個小東西,模擬天上的一閃一閃的星星,或是也可以說是模擬一閃一閃的螢火蟲。
要隨機點亮 LED 會用到 Arduino 的亂數相關函式,基本認識及用法請詳 http://pizgchen.blogspot.tw/2017/03/arduino.html
要隨機點亮 LED 會用到 Arduino 的亂數相關函式,基本認識及用法請詳 http://pizgchen.blogspot.tw/2017/03/arduino.html
程式碼1
//2017-03-12 LED_Cube4_04.ino
//以層方式,隨機點亮 LED
#define CUBE_SIZE 4
#define PLANE_SIZE CUBE_SIZE*CUBE_SIZE
#define PLANE_TIME 0
#define LED_TIME 2
int LEDPin[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, A0, A1};
int PlanePin[] = {A2 , A3, A4, A5};
void setup()
{
int pin;
for (pin = 0; pin < PLANE_SIZE; pin++) {
pinMode(LEDPin[pin], OUTPUT);
digitalWrite(LEDPin[pin], LOW);
}
for (pin = 0; pin < CUBE_SIZE; pin++) {
pinMode(PlanePin[pin], OUTPUT);
digitalWrite(PlanePin[pin], HIGH);
}
randomSeed(A0);
}
void loop()
{
for (int plane = 0; plane < CUBE_SIZE; plane++) {
digitalWrite(PlanePin[plane], LOW);
for (int col = 0; col < CUBE_SIZE; col++) {
for (int row = 0; row < CUBE_SIZE; row++)
{
int k = random(100);
if (k < 50) {
digitalWrite(LEDPin[col * CUBE_SIZE + row], HIGH);
delay(LED_TIME);
digitalWrite(LEDPin[col * CUBE_SIZE + row], LOW);
}
}
}
delay(PLANE_TIME);
digitalWrite(PlanePin[plane], HIGH);
}
}
來看一下影片
您可以改變一下 LED_TIME 的值,觀察是不是有不同的顯示效果。
程式碼2
程式也可以改成下列這樣,顯示的順序會與上方程式不同。
//2017-03-12 LED_Cube4_05.ino
//以柱方式,隨機點亮 LED
#define CUBE_SIZE 4
#define PLANE_SIZE CUBE_SIZE*CUBE_SIZE
#define PLANE_TIME 0
#define LED_TIME 20
int LEDPin[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, A0, A1};
int PlanePin[] = {A2 , A3, A4, A5};
void setup()
{
int pin;
for (pin = 0; pin < PLANE_SIZE; pin++) {
pinMode(LEDPin[pin], OUTPUT);
digitalWrite(LEDPin[pin], LOW);
}
for (pin = 0; pin < CUBE_SIZE; pin++) {
pinMode(PlanePin[pin], OUTPUT);
digitalWrite(PlanePin[pin], HIGH);
}
randomSeed(A0);
}
void loop()
{
for (int col = 0; col < CUBE_SIZE; col++) {
for (int row = 0; row < CUBE_SIZE; row++) {
for (int plane = 0; plane < CUBE_SIZE; plane++) {
digitalWrite(PlanePin[plane], LOW);
int k = random(100);
if (k < 50) {
digitalWrite(LEDPin[col * CUBE_SIZE + row], HIGH);
delay(LED_TIME);
digitalWrite(LEDPin[col * CUBE_SIZE + row], LOW);
}
delay(PLANE_TIME);
digitalWrite(PlanePin[plane], HIGH);
}
}
}
}
程式碼3
很明顯地,上面 2 支程式都讓 LED 顯示的太快了,一點兒也不像星星或螢火蟲閃爍。這回我們將更進一步讓它更接近真實,將原本順序顯示的層改為隨機,我把原本的程式碼 Mark 起來沒有刪除,目的是讓您參考比對。
//2017-03-12 LED_Cube4_06.ino
//以隨機柱方式,隨機點亮 LED
#define CUBE_SIZE 4
#define PLANE_SIZE CUBE_SIZE*CUBE_SIZE
#define PLANE_TIME 200
#define LED_TIME 50
int LEDPin[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, A0, A1};
int PlanePin[] = {A2 , A3, A4, A5};
void setup()
{
int pin;
for (pin = 0; pin < PLANE_SIZE; pin++) {
pinMode(LEDPin[pin], OUTPUT);
digitalWrite(LEDPin[pin], LOW);
}
for (pin = 0; pin < CUBE_SIZE; pin++) {
pinMode(PlanePin[pin], OUTPUT);
digitalWrite(PlanePin[pin], HIGH);
}
randomSeed(A0);
}
void loop()
{
for (int col = 0; col < CUBE_SIZE; col++) {
for (int row = 0; row < CUBE_SIZE; row++) {
// for (int plane = 0; plane < CUBE_SIZE; plane++) {
int plane = random(CUBE_SIZE);
digitalWrite(PlanePin[plane], LOW);
int k = random(100);
if (k < 50) {
digitalWrite(LEDPin[col * CUBE_SIZE + row], HIGH);
delay(LED_TIME);
digitalWrite(LEDPin[col * CUBE_SIZE + row], LOW);
}
delay(PLANE_TIME);
digitalWrite(PlanePin[plane], HIGH);
// }
}
}
}
程式碼4
下列程式是把上面程式精簡化,它們倆顯示的效果看起來相同。
//精簡以隨機柱方式,隨機點亮 LED
#define CUBE_SIZE 4
#define PLANE_SIZE CUBE_SIZE*CUBE_SIZE
#define PLANE_TIME 200
#define LED_TIME 50
int LEDPin[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, A0, A1};
int PlanePin[] = {A2 , A3, A4, A5};
void setup()
{
int pin;
for (pin = 0; pin < PLANE_SIZE; pin++) {
pinMode(LEDPin[pin], OUTPUT);
digitalWrite(LEDPin[pin], LOW);
}
for (pin = 0; pin < CUBE_SIZE; pin++) {
pinMode(PlanePin[pin], OUTPUT);
digitalWrite(PlanePin[pin], HIGH);
}
randomSeed(A0);
}
void loop()
{
int plane = random(CUBE_SIZE);
int cube = random(PLANE_SIZE);
int k = random(100);
digitalWrite(PlanePin[plane], LOW);
if (k < 50) {
digitalWrite(cube, HIGH);
delay(LED_TIME);
digitalWrite(cube, LOW);
}
delay(PLANE_TIME);
digitalWrite(PlanePin[plane], HIGH);
}
程式碼5
這是最終版本,在這兒將所有的數值全部隨機取出,顯示效果比前面幾個版本更佳了。
//2017-03-12 LED_Cube4_08.ino
//將所有數值亂數化,隨機點亮 LED
#define CUBE_SIZE 4
#define PLANE_SIZE CUBE_SIZE*CUBE_SIZE
int PLANE_TIME = 100;
int LED_TIME = 1000;
int LEDPin[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, A0, A1};
int PlanePin[] = {A2 , A3, A4, A5};
void setup()
{
int pin;
for (pin = 0; pin < PLANE_SIZE; pin++) {
pinMode(LEDPin[pin], OUTPUT);
digitalWrite(LEDPin[pin], LOW);
}
for (pin = 0; pin < CUBE_SIZE; pin++) {
pinMode(PlanePin[pin], OUTPUT);
digitalWrite(PlanePin[pin], HIGH);
}
randomSeed(A0);
}
void loop()
{
int plane = random(CUBE_SIZE);
int cube = random(PLANE_SIZE);
int k = random(100);
digitalWrite(PlanePin[plane], LOW);
if (k < 50) {
digitalWrite(cube, HIGH);
delay(random(LED_TIME));
digitalWrite(cube, LOW);
}
delay(random(PLANE_TIME));
digitalWrite(PlanePin[plane], HIGH);
}
來看一下影片
相關連結
謝謝大大 很抱歉上次提出了那麼無理的要求 真的非常謝謝你 這篇對我來說受益良多 謝謝你 (跪
回覆刪除不客氣
刪除