姓名:巫守原 李柏陞
功能:防小偷 和整小偷
程式:
#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);
}
}
2016年5月21日 星期六
2016年5月20日 星期五
開關+燈(不用一直按)
按鈕
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2按鈕的位子; // the number of the pushbutton pin
const int ledPin = 5燈的位子; // the number of the LED pin
// Variables will change:
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
// set initial LED state
digitalWrite(ledPin, ledState);
}
void loop() {
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;
// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH) {
ledState = !ledState;
}
}
}
// set the LED:
digitalWrite(ledPin, ledState);
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastButtonState = reading;
}
Arrays 紅綠燈
int timer = 100; // The higher the number, the slower the timing.
int ledPins[] = {
3, 4, 5, LED燈放的接腳
}; // an array of pin numbers to which LEDs are attached
int pinCount = 3; // the number of pins (i.e. the length of the array)
void setup() {
// the array elements are numbered from 0 to (pinCount - 1).
// use a for loop to initialize each pin as an output:
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
pinMode(ledPins[thisPin], OUTPUT);
}
}
void loop() {
// loop from the lowest pin to the highest:
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
// turn the pin on:
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
// turn the pin off:
digitalWrite(ledPins[thisPin], LOW);
} // loop from the highest pin to the lowest:
for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
// turn the pin on:
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
// turn the pin off:
digitalWrite(ledPins[thisPin], LOW);
}
int timerr[] ={ 10000,3500,9500}停的秒數(綠黃紅); // The higher the number, the slower the timing.
int ledPins[] = {
3,4,5
}; // an array of pin numbers to which LEDs are attached
int pinCount = 3; // the number of pins (i.e. the length of the array)
void setup() {
// the array elements are numbered from 0 to (pinCount - 1).
// use a for loop to initialize each pin as an output:
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
pinMode(ledPins[thisPin], OUTPUT);
}
}
void loop() {
// loop from the lowest pin to the highest:
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
// turn the pin on:
digitalWrite(ledPins[thisPin], HIGH);
delay(timerr[thisPin]);
// turn the pin off:
digitalWrite(ledPins[thisPin], LOW);
}
// loop from the highest pin to the lowest:
//for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
// turn the pin on:
//digitalWrite(ledPins[thisPin], HIGH);
// delay(timer);
// turn the pin off:
// digitalWrite(ledPins[thisPin], LOW);
}
Arduino聲音 MELODY
*/
#include "pitches.h"
// notes in the melody:
int melody[] = {
NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};聲音(音階)C(DO) D(RE)E(MI)F(FA)G(SO)A(LA)B(SI)
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
4, 8, 8, 4, 4, 4, 4, 4
};節拍
void setup() {
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 8; thisNote++)為上方聲音的順序 {
// to calculate the note duration, take one second
// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8時間(1000單位為1秒), etc.
int noteDuration = 1000 / noteDurations[thisNote];
tone(8, melody[thisNote], noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(8);
}
}
void loop() {
// no need to repeat the melody.不會重覆
LED Analoglnput
以搖桿控制LED燈
int sensorPin = A0; // select the input pin for the potentiometer控制器(搖桿)int ledPin = 3; // select the pin for the LED接腳為3
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
// turn the ledPin on
digitalWrite(ledPin, HIGH);
// stop the program for <sensorValue> milliseconds:
delay(sensorValue);
// turn the ledPin off:
digitalWrite(ledPin, LOW);
// stop the program for for <sensorValue> milliseconds:
delay(sensorValue);
}
Arduino 閃爍燈
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin 13 as an output.
pinMode(13, OUTPUT);
}
接腳13
// the loop function runs over and over again forever
void loop() {
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
延遲1秒 最高電壓
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
延遲1秒最低電壓
void setup() {
// initialize digital pin 13 as an output.
pinMode(13, OUTPUT);
}
接腳13
// the loop function runs over and over again forever
void loop() {
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
延遲1秒 最高電壓
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
延遲1秒最低電壓
訂閱:
文章 (Atom)