Posted by : Unknown วันอาทิตย์ที่ 27 กรกฎาคม พ.ศ. 2557


ยำรวมกันโปรเจ็กที่เท่าไหร่ก็ไม่ทราบแล้ว

ข้อควรทราบเมื่อใช้งาน Ethernet Shield WZ5100 คือ จุดต่อที่ห้ามใช้คือ  pins A0, A1, D4, and D10-D13


ดังนั้น pin ที่เราจะใช้ได้คือ pin อื่น นอกเหนือจากที่แจ้งไว้ 

โปรเจ็กนี้

MQ2 gas sensor
DHT11 ใช้ pin 3
PIR ใช้ pin2
MQ2 ใช้ A3

จุดสังเกตุ เมื่อ PIR ตรวจเจออะไร ค่า MQ2 ที่แสดง จะแกว่งแป๊บนึง ก่อนกลับมาสู่จุดปกติ 













 
หน้าตาใน command จะเป็นแบบนี้




CODE

#include <dht.h>
dht DHT;
#define DHT11_PIN 3   //
#define DHTTYPE DHT11

const int analogInPin = A3;  // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9;                // choose the pin for the LED

int sensorValue = 0;        // value read from the pot

int inputPin2 = 2;               // choose the input pin (for PIR sensor)
int ledPin2 = 13;
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;                    // variable for reading the pin status
int pinSpeaker2 = 10;           //Set up a speaker on a PWM pin (digital 9, 10, or 11)

void setup() {
  Serial.begin(9600);
  delay(300);//Let system settle
  Serial.println("Humidity and temperature\n\n");
  delay(700);//Wait rest of 1000ms recommended delay before
  //accessing sensor

  pinMode(analogOutPin, OUTPUT);
  pinMode(ledPin2, OUTPUT);      // declare LED as output
  pinMode(inputPin2, INPUT);     // declare sensor as input
  pinMode(pinSpeaker2, OUTPUT);
  Serial.begin(9600);
}

void loop(){
  DHT.read11(DHT11_PIN);
  Serial.print("Current humidity = ");
  Serial.print(DHT.humidity);
  Serial.print("% ");
  Serial.print("temperature = ");
  Serial.print(DHT.temperature);
  Serial.println("C ");
  delay(800);

  val = digitalRead(inputPin2);  // read input value
  if (val == HIGH) {            // check if the input is HIGH
    digitalWrite(ledPin2, HIGH);  // turn LED ON
    playTone(300, 160); // เดิม 300, 160)
    delay(100); // เดิม 150

   
    if (pirState == LOW) {
      // we have just turned on
      Serial.println("Motion detected!");
      // We only want to print on the output change, not state
      pirState = HIGH;
    }
  } else {
      digitalWrite(ledPin2, LOW); // turn LED OFF
      playTone(0, 0);
      delay(300);   
      if (pirState == HIGH){
      // we have just turned of
      Serial.println("Motion ended!");
      // We only want to print on the output change, not state
      pirState = LOW;
    }
  }

  // read the analog in value:
  sensorValue = analogRead(analogInPin);           
  // determine alarm status
  if (sensorValue >=750)
  {
    digitalWrite(analogOutPin, HIGH);   // sets the LED on
  }
  else
  {
  digitalWrite(analogOutPin, LOW);    // sets the LED off
  }

  // print the results to the serial monitor:
  Serial.print("sensor = " );                      
  Serial.println(sensorValue);    

  // wait 10 milliseconds before the next loop
  // for the analog-to-digital converter to settle
  // after the last reading:
  delay(10);
  }


// duration in mSecs, frequency in hertz
void playTone(long duration, int freq) {
    duration *= 1000;
    int period = (1.0 / freq) * 1000000;
    long elapsed_time = 500; // เดิม 0
    while (elapsed_time < duration) {
        digitalWrite(pinSpeaker2,HIGH);
        delayMicroseconds(period / 2);
        digitalWrite(pinSpeaker2, LOW);
        delayMicroseconds(period / 2);
        elapsed_time += (period);
    }
}

Leave a Reply

Subscribe to Posts | Subscribe to Comments

- Copyright © arduino Thai for fun - Skyblue - Powered by Blogger - Designed by Johanes Djogan -