---
# System prepended metadata

title: Arduino微控智學創新

---

Arduino微控智學創新
===
# 01_109/03/03 
## 程式一:用函式點亮LED:
```cpp
#define LED1 8
#define LED2 9
#define LED3 10
#define LED4 11

void blink1(int);

void setup() {
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
}

void loop()
{
  blink1(LED1);
  blink1(LED2);
  blink1(LED3);
  blink1(LED4);
}
void blink1(int a)
{
  digitalWrite(a, LOW);
  delay(500);
  digitalWrite(a, HIGH);
  delay(500);
}
```
## 程式二:變數delay控制LED
```cpp
#define LED1 8
#define LED2 9
#define LED3 10
#define LED4 11

void blink1(int,int);

void setup() {
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
}

void loop()
{
  blink1(LED1,300);
  blink1(LED2,600);
  blink1(LED3,900);
  blink1(LED4,1200);
}
void blink1(int a,int b)
{
  digitalWrite(a, LOW);
  delay(b);
  digitalWrite(a, HIGH);
  delay(b);
}

```
# 02_109/03/10
## 程式一:ON/OFF雙按鈕控制繼電器
```cpp
#define RY 11
#define S1 A4
#define S2 A5


void setup() {


  pinMode(RY, OUTPUT);
  digitalWrite(RY, 0);
}

void loop() {

  int i = digitalRead(S2);
  if (!i) {
    digitalWrite(RY, 1);
  }
  else {
    int j = digitalRead(S1);
    if (!j)digitalWrite(RY, 0);
  }
  delay(100);
}
```
## 程式二:單按鈕控制1個LED燈
```cpp
#define S1 A5
#define LED 11
 int a=1;
void setup()
{
  pinMode(LED, OUTPUT);
  digitalWrite(LED, 1);
}

void loop()
{
  int i = digitalRead(S1);
  if (!i) {
    while (!i) {
      i = digitalRead(S1);
    }
    a=!a;
    digitalWrite(LED, a);
  }
  delay(100);
}
```
# 03_109/03/17
## 程式一:按鈕控制LED&蜂鳴器單獨發聲
```cpp
#define S1 A5
#define LED 13
#define BZ 3

int a = 1; //儲存LED目前狀態

void setup() {
  pinMode(LED, OUTPUT);
  pinMode(BZ, OUTPUT);
  digitalWrite(LED, a);
}

void loop() {
  int i = digitalRead(S1);
  if (!i)
  {
    tone(BZ,5000);
    delay(100);
    noTone(BZ);
    while (!i)
    {
      i = digitalRead(S1);
    }
    a = !a;  //LED反相
    digitalWrite(LED, a);
  }
}
```
### 程式二:按鈕控制5個LED依序點亮&蜂鳴器單獨發聲
```cpp
#define S1 A5
#define BZ 3
int LED[5] = {11, 10, 9, 8, 7};
int a = 0;
void setup()
{ pinMode(BZ, OUTPUT);
  for (int i; i < 5; i++)
  {
    pinMode(LED[i], OUTPUT);
    digitalWrite(LED[i], 1);
  }
  digitalWrite(LED[a], 0);
}

void loop()
{

  int i = digitalRead(S1);
  if (!i) {
    tone(BZ, 5000);
    delay(100);
    noTone(BZ);
    while (!i) i = digitalRead(S1);
    digitalWrite(LED[a], i);
    if(++a==5) a=0;
    digitalWrite(LED[a],0);
  }
}
```
# 04_109/03/24 
## 程式一:
![](https://i.imgur.com/HcKnsVd.png)

```cpp
const int DIP[4] = {A2, A3, A4, A5};
const int LED[10] = {11, 10, 9, 8, 7, 6, 5, 4, 3, 2};
int sw[4] = {1, 1, 1, 1};

void setup() {
  for (int i = 0; i < 10; i++)
  {
    pinMode(LED[i], OUTPUT);
    digitalWrite(LED[i], 1);
  }
}
void loop() {
  for (int i = 0; i < 4; i++)
    sw[i] = digitalRead(DIP[i]);
  if (sw[0] == 0 && sw[1] == 1 && sw[2] == 1 && sw[3] == 1)//F1
  {
    F1();
  }
  else if (sw[0] == 1 && sw[1] == 0 && sw[2] == 1 && sw[3] == 1)
  {
    LEDOFF();
    F2();
  }
  else if (sw[0] == 1 && sw[1] == 1 && sw[2] == 0 && sw[3] == 1)
  {
    LEDOFF();
    F3();
  }
  else if (sw[0] == 1 && sw[1] == 1 && sw[2] == 1 && sw[3] == 0)
  {
    F4();
  }
  else
  {
    LEDOFF();
    digitalWrite(LED[4], 0);
  }
}
void F1()
{
  for (int i = 0; i < 10; i++) digitalWrite(LED[i], 0);
  delay(200);
  for (int i = 0; i < 10; i++) digitalWrite(LED[i], 1);
  delay(200);
}
void LEDOFF()
{
  for (int i = 0; i < 10; i++)
    digitalWrite(LED[i], 1);
}
void F2()
{
  for (int i = 0 ; i < 10; i++)
  {
    digitalWrite(LED[i], 0);
    delay(100);
    digitalWrite(LED[i], 1);
  }
}
void F3()
{
  for (int i = 9; i >= 0; i--)
  {
    digitalWrite(LED[i], 0);
    delay(100);
    digitalWrite(LED[i], 1);
  }
}
void F4()
{
    LEDOFF();
    F2();
    F3();
}
```
### 程式二:四個指撥開關控制六種不同燈的表現(多了來回燈)
```cpp
const int DIP[4] = {A2, A3, A4, A5};
const int LED[10] = {11, 10, 9, 8, 7, 6, 5, 4, 3, 2};
int sw[4] = {1, 1, 1, 1};

void setup() {
  for (int i = 0; i < 10; i++)
  {
    pinMode(LED[i], OUTPUT);
    digitalWrite(LED[i], 1);
  }
}
void loop() {
  for (int i = 0; i < 4; i++)
    sw[i] = digitalRead(DIP[i]);
  if (sw[0] == 0 && sw[1] == 1 && sw[2] == 1 && sw[3] == 1)//F1
  {
    F1();
  }
  else if (sw[0] == 1 && sw[1] == 0 && sw[2] == 1 && sw[3] == 1)
  {
    LEDOFF();
    F2();
  }
  else if (sw[0] == 1 && sw[1] == 1 && sw[2] == 0 && sw[3] == 1)
  {
    LEDOFF();
    F3();
  }
  else if (sw[0] == 1 && sw[1] == 1 && sw[2] == 1 && sw[3] == 0)
  {
    F4();
  }
  else if (sw[0] == 0 && sw[1] == 0 && sw[2] == 0 && sw[3] == 0)
  {
    F5();
  }
  else if (sw[0] == 1 && sw[1] == 1 && sw[2] == 1 && sw[3] == 1)
  {
    F6();
  }
}
void F1()
{
  for (int i = 0; i < 10; i++) digitalWrite(LED[i], 0);
  delay(200);
  for (int i = 0; i < 10; i++) digitalWrite(LED[i], 1);
  delay(200);
}
void LEDOFF()
{
  for (int i = 0; i < 10; i++)
    digitalWrite(LED[i], 1);
}
void F2()
{
  for (int i = 0 ; i < 10; i++)
  {
    digitalWrite(LED[i], 0);
    delay(100);
    digitalWrite(LED[i], 1);
  }
}
void F3()
{
  for (int i = 9; i >= 0; i--)
  {
    digitalWrite(LED[i], 0);
    delay(100);
    digitalWrite(LED[i], 1);
  }
}
void F4()
{
    LEDOFF();
    F2();
    F3();
}
void F5()
{
  LEDOFF();
  for(int i=0;i<5;i++)
  {
    digitalWrite(LED[i],0);
    digitalWrite(LED[9-i],0);
    delay(200);
  }
}
void F6()
{
  LEDOFF();
  for(int i=5;i>=0;i--)
  {
    digitalWrite(LED[i],0);
    digitalWrite(LED[9-i],0);
    delay(200);
  }
}

```
# 05_109/04/14
## 程式一:用滑動式電位計控制LED亮度
![](https://i.imgur.com/tZ2NzKQ.png)

```cpp
#define LED 11
#define pot A5
void setup() {
  pinMode(LED, OUTPUT);
  digitalWrite(LED, 1);
  Serial.begin(9600);
}
void loop() {
  int i = analogRead(pot);
  int j = map(i, 0, 1023, 0, 30);
  analogWrite(LED, 255-j);
  Serial.print("pot讀的數值=");
  Serial.print(i);
  Serial.print("      LED顯示的亮度數值=");
  Serial.println(j);
  delay(200);
}
```

# 06_109/04/21
## 程式一:Serial視窗輸入0~9控制LED亮度
```
說明：
 Serial視窗輸入0~9控制LED亮度，其中0為最暗，9為最亮
 Serial輸入格式如下:
 假設輸入為：'1' Serial視窗輸出為：
 鍵盤輸入的是:『1』
 亮度等級為：『1』
```
```cpp=
#define LED 11
void ShowLED(int);
int in = 0;
void setup()
{
  pinMode(LED, OUTPUT);
  digitalWrite(LED, 1);
  Serial.begin(9600);
}
void loop()
{
  if (Serial.available() > 0)
  {
    in = Serial.read();
    Serial.print("鍵盤輸入的是:『 ");
    Serial.print(in - 48);
    Serial.println("』");
    in = in - '0';
    if (in >= 0 && in <= 9 )
    {
      ShowLED(in);
    }
  }
}
void ShowLED(int level)
{
  int i = map(level, 0, 9, 255, 0);

  Serial.print("亮度等級為=『");
  Serial.print(level);
  Serial.println("』");
  analogWrite(LED, i);
}
```
## 程式二:
```
功能:判斷Serial輸入幾個字元，根據輸入1~2個字元回饋字元數
ex.
輸入"72"+"enter" 輸出視窗會顯示:"現在輸入2個字元"
輸入"5"+"enter" 輸出視窗會顯示:"現在輸入1個字元"
```
```cpp
#define LED 11
char recData[6] = {0, 0, 0, 0, 0, 0};
int i = 0 , recN = 0;
void setup()
{
  pinMode(LED, OUTPUT);
  digitalWrite(LED, 1);
  Serial.begin(9600);
}
void loop()
  {
    if (Serial.available() > 0) 
    {
      CLR_recData();
      recN = Serial.readBytesUntil(13, recData, 6);
      if (recN == 1)
      {
        Serial.println("現在輸入一個字元");
      }
      if (recN == 2)
      {
        Serial.println("現在輸入兩個字元");
      }
    }
  }
  void CLR_recData()
  {
    for (int i = 0; i < 6; i++)
      recData[i] = 0;
  }
```
# 07-109/4/28
## 程式一:
```
功能:Serial輸入數字和利用u.d調整亮度，並回饋
ex.輸入:87 
   輸出:
   LED亮度等級為『87』
   再輸入:U 
   輸出:
   目前輸入的key為『U』
   LED亮度等級為『97』
```
```cpp
#define LED 11
char recData[6] = {0, 0, 0, 0, 0, 0};
int i = 0, recN = 0;
void ShowLED(int level);
int LED_LEVEL = 0;

void setup()
{
  pinMode(LED, OUTPUT);
  digitalWrite(LED, 1);
  Serial.begin(9600);
}
void loop()
{
  if (Serial.available() > 0)
  {
    CLR_recData();
    recN = Serial.readBytesUntil(13, recData, 6);
    if (recN == 1)
    {
      if (recData[0] >= '0' && recData[0] <= '9')
      {
        //Serial.println("現在輸入1個字元");
        //        Serial.println(recData[0]);
        LED_LEVEL = recData[0] - '0';
        ShowLED(recData[0] - '0');
      }
      if (recData[0] == 'D'||recData[0] == 'd')
      {
        Serial.print("目前輸入的key為『");
        Serial.print(recData[0]);
        Serial.println("』");
        if (LED_LEVEL >= 10)
        {
          LED_LEVEL -= 10;
          ShowLED(LED_LEVEL);
        }
        else
        {
          LED_LEVEL = 0;
          ShowLED(LED_LEVEL);
        }
      }
      if (recData[0] == 'U'||recData[0] == 'u')
      {
        Serial.print("目前輸入的key為『");
        Serial.print(recData[0]);
        Serial.println("』");
        if (LED_LEVEL <= 89)
        {
          LED_LEVEL += 10;
          ShowLED(LED_LEVEL);
        }
        else
        {
          LED_LEVEL = 99;
          ShowLED(LED_LEVEL);
        }
      }
    }
    if (recN == 2)
    {
      //Serial.println("現在輸入2個字元");
      /*
        Serial.println(recData[0]);
        Serial.println(recData[1]);*/
      int a = (recData[0] - '0') * 10 + (recData[1] - '0');
      //Serial.println(a);
      LED_LEVEL = a;
      ShowLED(a);
    }
  }
}
void CLR_recData()
{
  for (int i = 0; i < 6; i++)
    recData[i] = 0;
}


void ShowLED(int level)
{
  int i = map (level, 0, 99, 255, 0);
  //    Serial.print("LED 亮度= " );
  //    Serial.println(255 - i);
  Serial.print("LED亮度等級=『");
  Serial.print(level);
  Serial.println("』");
  Serial.println("-------------");
  analogWrite(LED, i);
}
```
# 08_109/5/5
## 程式一:
==電路圖==
![](https://i.imgur.com/vpNUxiG.png)
==程式碼==
```cpp
 const int seg7[] = {7, 8, 9, 10, 11, 12, 13};
const int TAB[] = {0x40, 0x79, 0x24, 0x30, 0x19, 0x12, 0x02, 0x58, 0x00, 0x10};
const int scan[]={6,5,4,3};
void setup()
{
  for (int i = 0; i < 7; i++)
  {
    pinMode(seg7[i], OUTPUT);
    digitalWrite(seg7[i], 1 );
    //delay(1000);
  }
  for (int i = 0; i < 4; i++)
  {
    pinMode(scan[i], OUTPUT);
    digitalWrite(scan[i], 1 );
    //delay(1000);
  }
}
void loop()
{
  turn_off_seg7();
  OutPort(TAB[0]);
  digitalWrite(scan[0],0);
  delay(5);
  turn_off_seg7();
  OutPort(TAB[1]);
  digitalWrite(scan[1],0);
  delay(5);
  
//  for (int i = 0 ; i < 10; i++) 
//  {
//    OutPort(TAB[i]);
//    delay(100);
//  }
}
void OutPort(int dat)
{
  for (int i = 0; i < 7; i++)
  {
    if (dat % 2 == 1)
    {
      digitalWrite(seg7[i], 1);
    }
    else
    {
      digitalWrite(seg7[i], 0);
    }
    dat = dat / 2;
  }
}
void turn_off_seg7()
{
  for(int i=0;i<4;i++)
  digitalWrite(scan[i],1);
}

```
# 08_109/5/12
==電路圖==
![](https://i.imgur.com/vpNUxiG.png)
==程式碼==
```cpp
const int seg7[] = {7, 8, 9, 10, 11, 12, 13};
const int TAB[] = {0x40, 0x79, 0x24, 0x30, 0x19, 0x12, 0x02, 0x58, 0x00, 0x10};
const int scan[] = {6, 5, 4, 3};
int Disp[] = {0, 5, 1, 2};
int disp_v = 0000;
unsigned long A = 0, B = 0;
int mm = 0, ss = 0;
void setup()
{
  for (int i = 0; i < 7; i++)
  {
    pinMode(seg7[i], OUTPUT);
    digitalWrite(seg7[i], 1 );
    //delay(1000);
  }
  for (int i = 0; i < 4; i++)
  {
    pinMode(scan[i], OUTPUT);
    digitalWrite(scan[i], 1 );
    //delay(1000);
  }
  A = millis();
}
void loop()
{
 
//  Disp[0] = disp_v / 1000;
//  Disp[1] = (disp_v / 100) % 10;
//  Disp[2] = (disp_v / 10) % 10;
//  Disp[3] = disp_v % 10;
    Disp[0] = mm / 10;
    Disp[1] = mm % 10; 
    Disp[2] = ss/ 10;
    Disp[3] = ss % 10;
  for (int i = 0 ; i < 4; i++)
  {
    turn_off_seg7();
    OutPort(TAB[Disp[i]]);
    digitalWrite(scan[i], 0);
    delay(5);
  }
  B = millis();
  if ((B - A) > 100)
  {
    if(ss<59)
    {
      ss++;
    }
    else
    {
      ss=0;
      mm++;
    }
    if(mm>59) 
    {
       mm=0;
    }
    A=millis();
  }
}
void OutPort(int dat)
{
  for (int i = 0; i < 7; i++)
  {
    if (dat % 2 == 1)
    {
      digitalWrite(seg7[i], 1);
    }
    else
    {
      digitalWrite(seg7[i], 0);
    }
    dat = dat / 2;
  }
}
void turn_off_seg7()
{
  for (int i = 0; i < 4; i++)
    digitalWrite(scan[i], 1);
}
```
# 09_109/5/19
==基本程式碼==
```cpp
/* ========== ch3_4_1.ino ============
* A4腳連接P18-1，SDA
* A5腳連接P18-2，SCL
*/
//========== 整體宣告部分 ============
#include <Wire.h>        //包含Wire.h標頭檔
#include <SparkFun_MMA8452Q.h> //包含SparkFun_MMA8452Q.h標頭檔
MMA8452Q myAcc;       //建構MMA8452Q物件
//========== 初始設定部分 ============
void setup()
{ myAcc.init();     //MMA8452Q初始化，而I2C位址自動為0x1d
    Serial.begin(9600);     //啟用串列埠
  } 
//========== 主程式部分 ============
void loop()
{ if(myAcc.available());    //若MMA8452Q資料備妥
    { myAcc.read();     //讀取MMA8452Q資料
      //===== 顯示X軸加速度值 ======
      Serial.print("Accelerometer X:"); 
      Serial.println(myAcc.x, 10); //十進位數字
      //===== 顯示Y軸加速度值 ======
      Serial.print("Accelerometer Y:");
      Serial.println(myAcc.y, 10); //十進位數字
      //===== 顯示Z軸加速度值 ======
      Serial.print("Accelerometer Z:"); 
      Serial.println(myAcc.z, 10);//十進位數字
      Serial.println(); 
      //===== 顯示重力加速度值(g值) =====
      Serial.println("Rescale:");
      Serial.print(myAcc.cx, 3);
      Serial.print(",  ");
      Serial.print(myAcc.cy, 3);
      Serial.print(",  ");
      Serial.println(myAcc.cz, 3);
      Serial.println();
      //===== 顯示晶片方向 =====
      int dir = myAcc.readPL(); 
      switch (dir)
      { case PORTRAIT_U:
          Serial.println("PORTRAIT UP");
          break;
        case PORTRAIT_D:
          Serial.println("PORTRAIT DOWN");
          break;
        case LANDSCAPE_L:
          Serial.println("LANDSCAPE LEFT");
          break;
        case LANDSCAPE_R:
          Serial.println("LANDSCAPE RIGHT");
          break;
        case LOCKOUT:
          Serial.println("FLAT");
          break;
    }
    delay(5000);     //停滯5秒
  }
}
```
==延伸程式碼==
```cpp
#include <Wire.h>        
#include <SparkFun_MMA8452Q.h> 
MMA8452Q myAcc;       
const int LED[10] = {12, 11, 10, 9, 8, 7, 6, 5, 4, 3};
int x=0;
void setup()
{
  for (int i = 0; i < 10; i++)
  {
    pinMode(LED[i], OUTPUT);
    digitalWrite(LED[i], 1);
    delay(500);
  }
  myAcc.init();
  Serial.begin(9600);
}

void loop()
{
 if(myAcc.available())
 {
  myAcc.read();
  x=myAcc.x;
  x=map(x,-1023,1023,-5,5);
  Serial.println(x);
 }
 switch(x)
 {
   case -5:
   digitalWrite(LED[0],0);
   break;
   case -4:
   digitalWrite(LED[1],0);
   break;
   case -3:
   digitalWrite(LED[2],0);
   break;
   case -2:
   digitalWrite(LED[3],0);
   break;
   case -1:
   digitalWrite(LED[4],0);
   break;
   case 1:
   digitalWrite(LED[5],0);
   break;
   case 2:
   digitalWrite(LED[6],0);
   break;
   case 3:
   digitalWrite(LED[7],0);
   break;
   case 4:
   digitalWrite(LED[8],0);
   break;
   case 5:
   digitalWrite(LED[9],0);
   break;
 }
 delay(50);
 for(int i=0 ;i<10;i++)
{
 digitalWrite(LED[i],1);
}
}
```
# 10_109/06/02
==測試馬達==
```cpp
#define MIA 10 
#define MIB 11
void setup() 
{
  pinMode(MIA,OUTPUT);
  pinMode(MIB,OUTPUT);
  digitalWrite(MIA,0);
  digitalWrite(MIB,0);
}

void loop() 
{
  digitalWrite(MIA,0);
  digitalWrite(MIB,1);
  delay(1000);
  digitalWrite(MIA,0);
  digitalWrite(MIB,0);
  delay(1000);
  digitalWrite(MIA,1);
  digitalWrite(MIB,0);
  delay(1000);
  digitalWrite(MIA,0);
  digitalWrite(MIB,0);
  delay(1000);
}
```
==程式一==
```cpp
#define S1 A5
#define LED 13
#define BZ 3
#define MIA 10
#define MIB 11

int a = 1; //儲存LED目前狀態

void setup() {
  pinMode(LED, OUTPUT);
  pinMode(BZ, OUTPUT);
  digitalWrite(LED, a);
}

void loop() {
  int i = digitalRead(S1);
  if (!i)
  {
    tone(BZ, 5000);
    delay(100);
    noTone(BZ);
    while (!i)
    {
      i = digitalRead(S1);
    }
    a = !a;  //LED反相
    digitalWrite(LED, a);
  }
  if (a == 0)
  {
    digitalWrite(MIA, 0);
    digitalWrite(MIB, 1);
  }
  else
  {
    digitalWrite(MIA, 1);
    digitalWrite(MIB, 0);
  }
}
```
# 11_109/06/09
==用途==
控制馬達的正反轉
==程式碼==
```cpp
#define S1 A5
#define BZ 3
#define MIA 10
#define MIB 11
const int LED[3] = {7,6,5};

int a = 1; //儲存LED目前狀態
int count=0;
int X=0;
void setup() {
  pinMode(LED, OUTPUT);
  pinMode(BZ, OUTPUT);
  digitalWrite(LED, a);
   for (int i = 0; i < 3; i++)
  {
    pinMode(LED[i], OUTPUT);
    digitalWrite(LED[i], 1);
  }
}

void loop() {
  int i = digitalRead(S1);
  if (!i)
  {
    tone(BZ, 5000);
    delay(100);
    noTone(BZ);
    while (!i)
    {
      i = digitalRead(S1);
    }
    count += 1;
    X = count%3;
  }
  if (X == 1)
  {
    LEDOFF();
    digitalWrite(MIA, 0);//反轉
    digitalWrite(MIB, 1);
    digitalWrite(LED[0],0);
  }
  else if (X == 2)
  {
    LEDOFF();
    digitalWrite(MIA, 1);//正轉
    digitalWrite(MIB, 0);
    digitalWrite(LED[1],0);
  }
  else
  {
    LEDOFF();
    digitalWrite(MIA, 0);
    digitalWrite(MIB, 0);
    digitalWrite(LED[2],0);
  }
}
void LEDOFF()
{
  for (int i = 0; i < 3; i++)
    digitalWrite(LED[i], 1);
}
```