Arduino - 光敏電阻 + 矩陣LED (photocell + matrix LED)


目的:

以光敏電阻去感應亮度,讓矩陣LED呈現的樹有三段變化且有落葉效果(以光合作用強不強烈作為構想)

圖的變化:


成果:


Arduino Code:

#include < SPI.h >
int LDR = 0; //光敏電阻輸入,接在anallog pin 0
int readLDR = 0; //光敏電阻感應到的光源產生的電阻值

unsigned char i;
unsigned char j;
/*port definition*/
int Max7219_pinCLK = 10;
int Max7219_pinCS = 9;
int Max7219_pinDIN = 8;

unsigned char symbol[12][8] = {
    {
        0xFF,
        0x3C,
        0x3C,
        0x3C,
        0x54,
        0x92,
        0x29,
        0x44
    }, // 接收光最少的樹

    {
        0xFF,
        0x3C,
        0x3C,
        0x3C,
        0x3C,
        0x3C,
        0x7E,
        0xFF
    }, // 接受光尚可的樹
    {
        0xFF,
        0x3C,
        0x3C,
        0x3C,
        0x3C,
        0x7E,
        0xFF,
        0xFF
    },
    {
        0xFF,
        0x3C,
        0x3C,
        0x3C,
        0x7E,
        0xBD,
        0x7E,
        0xFF
    },
    {
        0xFF,
        0x3C,
        0x3C,
        0x7E,
        0xBD,
        0x3C,
        0x7E,
        0xFF
    },
    {
        0xFF,
        0x3C,
        0x7E,
        0xBD,
        0x3C,
        0x3C,
        0x7E,
        0xFF
    },
    {
        0xFF,
        0x7E,
        0xBD,
        0x3C,
        0x3C,
        0x3C,
        0x7E,
        0xFF
    },
    {
        0xFF,
        0xBD,
        0x3C,
        0x3C,
        0x3C,
        0x3C,
        0x7E,
        0xFF
    },

    {
        0xFF,
        0x3C,
        0x3C,
        0x3C,
        0xFF,
        0xFF,
        0xFF,
        0xFF
    }, // 接收光最多的樹
    {
        0xFF,
        0x3C,
        0x7E,
        0xBD,
        0xFF,
        0xFF,
        0xFF,
        0xFF
    },
    {
        0xFF,
        0x7E,
        0xBD,
        0x3C,
        0xFF,
        0xFF,
        0xFF,
        0xFF
    },
    {
        0xFF,
        0xBD,
        0x3C,
        0x3C,
        0xFF,
        0xFF,
        0xFF,
        0xFF
    }
};

//下面的function是因為點矩陣是吃byte,先轉成byte再寫入
void Write_Max7219_byte(unsigned char DATA) {
    unsigned char i;
    digitalWrite(Max7219_pinCS, LOW);
    for (i = 8; i >= 1; i--) {
        digitalWrite(Max7219_pinCLK, LOW);
        //Obtain the MSB of the data
        digitalWrite(Max7219_pinDIN, DATA & 0x80);
        DATA = DATA << 1;
        digitalWrite(Max7219_pinCLK, HIGH);
    }
}

//腳位確定
void Write_Max7219(unsigned char address, unsigned char dat) {
    digitalWrite(Max7219_pinCS, LOW);
    Write_Max7219_byte(address); //
    Write_Max7219_byte(dat); //
    digitalWrite(Max7219_pinCS, HIGH);
}

void Init_MAX7219(void) {
    Write_Max7219(0x09, 0x00); //decode: BCD
    Write_Max7219(0x0a, 0x03); //Brightness
    Write_Max7219(0x0b, 0x07); //
    Write_Max7219(0x0c, 0x01); //
    Write_Max7219(0x0f, 0x00); //
    for (byte i = 0; i < 8; i++) {
        Write_Max7219(i + 1, 0);
    }
}

//下面是做初始設定
void setup() {
    pinMode(Max7219_pinCLK, OUTPUT);
    pinMode(Max7219_pinCS, OUTPUT);
    pinMode(Max7219_pinDIN, OUTPUT);
    delay(50);
    Init_MAX7219();
    Serial.begin(9600);
}

//下面是做持續的光源大小判斷,進而控制矩陣LED
void loop() {
    readLDR = analogRead(LDR); //存取光敏電阻的電阻值
    Serial.println(readLDR);
    if (readLDR <= 100) //光線最亮的時候
    {
        for (byte i = 0; i < 8; i++)
            Write_Max7219(i + 1, symbol[8][i]); //樹和落葉動畫效果
        delay(500);
        for (byte i = 0; i < 8; i++)
            Write_Max7219(i + 1, symbol[9][i]); //樹和落葉動畫效果 
        delay(500);
        for (byte i = 0; i < 8; i++)
            Write_Max7219(i + 1, symbol[10][i]); //樹和落葉動畫效果
        delay(500);
        for (byte i = 0; i < 8; i++)
            Write_Max7219(i + 1, symbol[11][i]); //樹和落葉動畫效果
        delay(500);

    }
    else if (readLDR > 100 && readLDR <= 200) //光線尚可的時候
    {
        for (byte i = 0; i < 8; i++)
            Write_Max7219(i + 1, symbol[1][i]); //樹和落葉動畫效果  
        delay(500);
        for (byte i = 0; i < 8; i++)
            Write_Max7219(i + 1, symbol[2][i]); //樹和落葉動畫效果 
        delay(500);
        for (byte i = 0; i < 8; i++)
            Write_Max7219(i + 1, symbol[3][i]); //樹和落葉動畫效果 
        delay(500);
        for (byte i = 0; i < 8; i++)
            Write_Max7219(i + 1, symbol[4][i]); //樹和落葉動畫效果 
        delay(500);
        for (byte i = 0; i < 8; i++)
            Write_Max7219(i + 1, symbol[5][i]); //樹和落葉動畫效果 
        delay(500);
        for (byte i = 0; i < 8; i++)
            Write_Max7219(i + 1, symbol[6][i]); //樹和落葉動畫效果  
        delay(500);
        for (byte i = 0; i < 8; i++)
            Write_Max7219(i + 1, symbol[7][i]); //樹和落葉動畫效果 
        delay(500);
    }
    else //光線不足的時候
    {
        for (byte i = 0; i < 8; i++) {
            Write_Max7219(i + 1, symbol[0][i]); //顯示只有樹枝的樹 
        }
        delay(500);
    }
}

留言