姓名:巫守原 李柏陞
功能:防小偷 和整小偷
程式:
#include <Ultrasonic.h>
#include <Servo.h>
Servo myservo;
const int buttonPin = 2;
const int ledPin = 4;
const int ledPin2 = 3;
int val;
int potpin = 0;
int buttonState = 0;
Ultrasonic ultrasonic(12,13); // (Trig PIN,Echo PIN)
void setup() {
myservo.attach(9);
pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
Serial.begin(9600);
pinMode(buttonPin, INPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
Serial.print(ultrasonic.Ranging(CM)); // CM or INC
Serial.println(" cm" );
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
digitalWrite(ledPin2, HIGH);
delay(10000);
int val = ultrasonic.Ranging(CM);
if(val<=30){
myservo.write(180);
}
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
digitalWrite(ledPin2, LOW);
}
}
沒有留言:
張貼留言