---
# System prepended metadata

title: 機器人專題課程

---

# 機器人專題課程

## 1.閃燈
![](https://i.imgur.com/cmZIBPl.png)


```javascript=
void setup()
{
  pinMode( D4 , OUTPUT);
}

void loop()
{
  digitalWrite(D4 , LOW);
  delay( 5000 );
  digitalWrite(D4 , HIGH);
  delay( 5000 );
}
```
![](https://i.imgur.com/AZtN6wo.jpg)

我覺得這東西讓我很新奇，第一次自己動手做成功讓我很有成就感，閃燈是為了測試開發板是否正常。


## 2.蜂鳴器
![](https://i.imgur.com/E3oX61O.png)


```javascript=
void setup()
{
}

void loop()
{
  tone(D6, 400);
  delay( 1000 );
  noTone(D6);
  delay( 1000 );
}
```

![](https://i.imgur.com/Uhmjdke.jpg)

蜂鳴器是用來響的，讓我覺得很新奇，動手做讓我很有成就感。


## 3.超音波感測器
![](https://i.imgur.com/iaHFnIo.png)

```javascript=
int Range = 0 ;
int ardublockUltrasonicSensorCodeAutoGeneratedReturnCM(int trigPin, int echoPin)
{
  long duration;
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(20);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  duration = duration / 59;
  if ((duration < 2) || (duration > 300)) return false;
  return duration;
}

void setup()
{
  digitalWrite( D5 , LOW );

  Serial.begin(9600);
}

void loop()
{
  Range = 	ardublockUltrasonicSensorCodeAutoGeneratedReturnCM( D5 , D6 ) ;
  Serial.print("message");
  Serial.print(Range);
  Serial.print(" ");
  Serial.println();
  delay( 200 );
}
```