---
# System prepended metadata

title: Arduino 課堂學習

---

# 感測器
電位器控制LED光線亮度

實驗文字說明

實驗照片
![Screenshot_20240521_002544](https://hackmd.io/_uploads/SkcOqxFm0.jpg)
程式碼ch8_3
```
const int led=10;
int val;
float volts;
void setup() 
{ 
  pinMode(led,OUTPUT);
  digitalWrite(led,LOW);
}
void loop() 
{
  val=analogRead(0);
  val=map(val,0,1023,1000,100);
  digitalWrite(led,HIGH);
  delay(val);
  digitalWrite(led,LOW);
  delay(val);
}  
```
光線偵測實驗

實驗文字說明

實驗照片
![upload_7d3e8120bdb08107af26a9b5d3097f28](https://hackmd.io/_uploads/H1C_TgF7C.png)
程式碼ch8_5
```const int cds=0;
const int led=10;
int val;
void setup() 
{ 
  pinMode(led,OUTPUT);
  digitalWrite(led,LOW);
}
void loop() 
{
  val=analogRead(cds);
  if(val>512)
     digitalWrite(led,HIGH);
  else
     digitalWrite(led,LOW); 
}
```
移動偵測

實驗文字說明

實驗照片
![upload_169c281436f9db8e7d5a0520138fbfb2](https://hackmd.io/_uploads/BJgbV-YXC.png)
程式碼ch8_6
```const int led=13;		
const int PIRout=2;		
void setup() 
{ 
  pinMode(led,OUTPUT);		
  pinMode(PIRout,INPUT);		
}
void loop() 
{
  int val=digitalRead(PIRout);		
  if(val==HIGH)		
  {
  	digitalWrite(led,HIGH);		
  	delay(50);
  	digitalWrite(led,LOW);
  	delay(50);    
  }
  else					
  {
  	digitalWrite(led,LOW);		
  }  
}
```
超音波測距儀（自定版：紅綠黃燈）

實驗文字說明

實驗照片
![Screenshot_20240521_010504](https://hackmd.io/_uploads/B1MCMbt7A.jpg)
程式碼自製
```const int TRIG_PIN = 6; 
const int ECHO_PIN = 7; 
const int LED_PIN  = 3; 
const int DISTANCE_THRESHOLD = 10; 

// variables will change:
float duration_us, distance_cm;

void setup() {
  Serial.begin (9600);
  pinMode(TRIG_PIN, OUTPUT); 
  pinMode(ECHO_PIN, INPUT);
  pinMode(LED_PIN, OUTPUT);
  pinMode(4,OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(12,OUTPUT);
}

void loop() {
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  duration_us = pulseIn(ECHO_PIN, HIGH);
  distance_cm = 0.017 * duration_us;

  if(distance_cm < DISTANCE_THRESHOLD){
    digitalWrite(LED_PIN, HIGH); 
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(12, LOW);
  }
  else if (distance_cm<=30){
    digitalWrite(LED_PIN, LOW);
    digitalWrite(4, HIGH);
    digitalWrite(5, LOW);
    digitalWrite(12, LOW);
  }
  else if (distance_cm<=50){
    digitalWrite(LED_PIN, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, HIGH);
    digitalWrite(12, LOW);
  }
  else{
    digitalWrite(LED_PIN, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(12, HIGH);
  }

  Serial.print("distance: ");
  Serial.print(distance_cm);
  Serial.println(" cm");

  delay(500);
}
```
伺服馬達和電腦鍵盤控制

實驗文字說明

實驗照片
![Screenshot_20240521_010853](https://hackmd.io/_uploads/r1S3Q-YXR.jpg)
程式碼 ch13_2
```include <Servo.h> 
Servo servo;
void setup() 
{ 
  Serial.begin(9600);
  servo.attach(2);
} 
void loop() 
{
  if(Serial.available())
  {
     char ch=Serial.read();
     Serial.println(ch);
     if(ch=='A' || ch=='a')
        servo.write(0);
     else if(ch=='B' || ch=='b')
        servo.write(90);
     else if(ch=='C' || ch=='c')
        servo.write(180);  
  }      
}
```
伺服馬達和電位器（可變電阻）

實驗文字說明

實驗照片
![Screenshot_20240521_011805](https://hackmd.io/_uploads/ByyxIWK7R.jpg)
程式碼 自製
```#include <Servo.h>
Servo myServo; 
void setup() 
{
  Serial.begin(9600);
  myServo.attach(9);
}
void loop() 
{
  int analogValue = analogRead(A0);
  int angle = map(analogValue, 0, 1023, 0, 180);
  myServo.write(angle); 
  Serial.print("Analog: ");
  Serial.print(analogValue);
  Serial.print(", Angle: ");
  Serial.println(angle);
  delay(100);
}
```
步進馬達

實驗文字說明

實驗照片
![upload_ff25b4d18a1f5394977bc28f1c9ce23c](https://hackmd.io/_uploads/r1T78ZYQC.jpg)
程式碼 自製
```#include <Stepper.h> 
#define steps 200
Stepper stepper(steps,2,3,4,5);
int n=0;
char key;
void setup() 
{ 
  stepper.setSpeed(1);
  Serial.begin(9600);
  Serial.println("press 0~9:setting steps/speed"); 
  Serial.println("press S:setting speed(default 1 RPM)");    
  Serial.println("press R:right turn");
  Serial.println("press L:left turn");  
} 
void loop()
{  
  if(Serial.available())
  {
     key=Serial.read();    
     if(key>='0' && key<='9')
        n=n*10+key-'0';   
     else if(key=='R' || key=='r')
     {
        stepper.step(n);  
        n=0;       
     }
     else if(key=='L' || key=='l')
     {     
        stepper.step(-1*n);  
        n=0;        
     }  
     else if(key=='S' || key=='s')
     {  
        stepper.setSpeed(n);
        n=0;         
     }      
  }
}
```
# 心得感想
最近覺得Arduino的操作越來越難，可能和程式的不熟有一點關係，希望之後能夠跟上老師的腳步！