(六)arduino入門:LED流水燈
一、本節介紹
在本課中,您將進行一個簡單但有趣的實驗 - 使用LED來創建流動的LED燈。顧名思義,這些流動的燈由連續八個LED連續點亮並逐漸變暗,就像流動的水一樣。
二、材料準備
1 * Arduino Uno板
1 *麵包板
若干杜邦線
8 * LED
8 *電阻(220Ω)
1 * USB數據線
三、原理講解
本實驗的原理只是依次打開八個LED。
四、實驗步驟
步驟1:建立電路
原理圖
步驟2:程序
步驟3:編譯代碼
步驟4:將草圖上傳到Arduino Uno板
現在,您應該看到八個LED從左到右依次亮起,然後從右到左依次變暗。之後,LED將從右到左點亮,從左到右變暗。這個整個過程將重複,直到電路斷電。
本節課代碼:
//*******老鼠的眼睛******** //LED流水燈 /* 八個LED將從左到右逐個點亮,然後從右到左逐個出去。 之後,LED從右到左依次點亮,然後從左到右逐個出去。 這個過程將無限期重複。* / /**************************************/ const int lowestPin = 2;//最低的一個連接到引腳2 const int highestPin = 9;//最高的一個連接到引腳9 /**************************************/ void setup() { //s設置引腳2到9作為輸出 for (int thisPin = lowestPin; thisPin <= highestPin; thisPin++) { pinMode(thisPin, OUTPUT); ///初始化thisPin作為輸出 } } /****************************************/ void loop() { //在引腳上迭代 //將引導從最低到最高 for (int thisPin = lowestPin; thisPin <= highestPin; thisPin++) { digitalWrite(thisPin, HIGH); //LED亮 delay(100);//等待100 ms } //從最高到最低 for (int thisPin = highestPin; thisPin >= lowestPin; thisPin--) { digitalWrite(thisPin, LOW); //轉動引導 delay(100);//等待100 ms } for (int thisPin = highestPin; thisPin >= lowestPin; thisPin--) { digitalWrite(thisPin, HIGH); delay(100);//等待100 ms } for (int thisPin = lowestPin; thisPin <= highestPin; thisPin++) { digitalWrite(thisPin, LOW); delay(100);//等待100 ms } }