---
# System prepended metadata

title: Arduino課堂學習

---

一顆LED ch4_1
使燈泡閃爍 中間延遲1秒
電路圖
![arduino-led-電路圖-1](https://hackmd.io/_uploads/SyPocJye0.png)
實作圖片
![LED線路連接實體圖](https://hackmd.io/_uploads/rkvjck1xR.jpg)
程式碼
int led = 13;
void setup() 
{                
  pinMode(led, OUTPUT); 
  digitalWrite(led,LOW);    
}

void loop() {
  digitalWrite(led, HIGH);    
  delay(100);               
  digitalWrite(led, LOW);   
  delay(100);               
}
使燈泡越來越亮
電路圖
![1](https://hackmd.io/_uploads/HJj1hJylA.png)
實作圖片
![upload_001aa8e5a453335bac0ca099b5981161](https://hackmd.io/_uploads/Byxmn1ygA.jpg)
程式碼 ch4_4
int led = 6;           
int brightness = 0;    
int lighten = 5;    
void setup()  
{ 
} 
void loop()  
{ 
    analogWrite(led,brightness);    
    if(brightness<250)     
      brightness = brightness + lighten;
    else
      brightness=0;
    delay(50);      
}
一個按鍵開關控制一個LED亮與暗
用按鍵開關控制燈泡亮暗
電路圖
![upload_e2a32b3117de467071fcb51e16faf201](https://hackmd.io/_uploads/HJ3inJJeC.png)
實作圖片
![Screenshot_20240406_234232](https://hackmd.io/_uploads/SyRFTy1x0.jpg)
程式碼
const int sw=2;		
const int led=13;	
const int debounceDelay=20;	
int ledStatus=LOW;	
int val;				
void setup() 
{   
  pinMode(sw,INPUT_PULLUP);  	
  pinMode(led,OUTPUT);  	
}
void loop() 
{
  val=digitalRead(sw);	
  if(val==LOW)		
  {
    delay(debounceDelay);	
    while(digitalRead(sw)==LOW)	
      ;  				
    ledStatus=!ledStatus;  	
    digitalWrite(led,ledStatus);        			
  }    
}
串列埠實驗
Arduino 傳送訊息給電腦
電路圖
![20240312_135241](https://hackmd.io/_uploads/H1DdygJeR.jpg)
程式碼ch6_1
byte val=65;
void setup() 
{ 
  Serial.begin(9600);
}
void loop() 
{
  for(int i=0;i<26;i++)
  {
     Serial.write(val+i);
     Serial.print('=');
     Serial.println(val+i);
     delay(1000); 
  }  
}  
操作畫面截圖
![Screenshot_20240406_235529](https://hackmd.io/_uploads/Syscxg1eR.jpg)
程式碼 ch6_3
int num = 0;   
void setup()
{
  Serial.begin(9600);   
}
void loop() 
{
 if (Serial.available() > 0) 
 {
    num = Serial.read();
    Serial.print("received:");
    Serial.println(num,DEC);
    Serial.write(num);
    Serial.println();
  }
}
程式碼
int i;
int key;
int led[]={8,9,10,11,12,13};
int status[]={0,0,0,0,0,0};
void setup()
{
  Serial.begin(9600); 
  for(i=0;i<6;i++)
  {
    pinMode(led[i],OUTPUT); 
    digitalWrite(led[i],LOW);
  }
}
void loop() 
{
  if (Serial.available()>0) 
  {
      key = Serial.read();
      key=key-'0';
      if(key>=0 && key<=5)
      {
        status[key]=!status[key];
        digitalWrite(led[key],status[key]); 
        Serial.print("LED status:");    
        for(i=0;i<6;i++)
        {
          if(status[i]==1)
            Serial.print("1");
          else
            Serial.print("0");
        }    
        Serial.println(); 
      }  
  }    
}
心得感想
我對這次的課程還不熟悉
不太知道怎麼操作
會持續加油努力學習





