前言
本次的小目標,希望能以伺服馬達當手、光敏電阻當眼睛,做出Google離線小恐龍的簡易自動破解器
成果照
Code
#include <Servo.h> //載入函式庫,這是內建的,不用安裝
Servo myservo; // 建立SERVO物件
int r = A0 ; //假設一個字母R代替A0的孔洞
int sensorValue;
void setup() {
Serial.begin(9600);
myservo.attach(9); // 設定要將伺服馬達接到哪一個PIN腳
myservo.write(0); //歸零
// put your setup code here, to run once:
}
void loop() {
sensorValue = analogRead(r);
Serial.println(sensorValue);
if(sensorValue <= 0){ //如果光敏電阻的數值小於或大於某一數值時,則伺服馬達轉動。
myservo.write(30); //旋轉到30度
delay(100); //延遲0.5秒
myservo.write(0); //旋轉到0度
delay(100 ); //延遲0.5秒 可自行更改
}
}