วันอาทิตย์ที่ 24 กุมภาพันธ์ พ.ศ. 2562

"Embedded Space"


/////////////////////////////////////////////////////////////////////
// Author: RSP @ ESL (Embedded System Lab), KMUTNB
// Date: 2014-APR-03
// Target Board: Arduino (ATmega328P, 5V, 16MHz)
// Arduino IDE: version 1.0.5
// Description:
//   This Arduino sketch demonstrates how to implement a simple human
//   motion detection system based on the AMN34112 sensor module.
//   When the motion is detected, the system triggers an alarm 
//   (LED blinking and buzzer sound).

////////////////////////////////////////////////////////////////////////

/*
  Board          INT0 INT1 INT2 INT3 INT4 INT5
  Uno, Ethernet   2 3        
  Mega2560        2 3 21 20 19 18
  Leonardo        3 2 0 1 7
 */


#define ALARM_PIN       (3)
#define BLINK_TIME_MS (1000)
#define WAIT_TIME_MS  (3000)

typedef enum {ST_CHECK=0, ST_ALARM, ST_WAIT} state_type;
state_type state;
uint32_t ts;

volatile uint16_t event_count = 0;
void eint_isr() {
  event_count++;
}

void setup() {
  pinMode( ALARM_PIN, OUTPUT );
  digitalWrite( ALARM_PIN, LOW );
  delay( 3000 );
  ts = millis();
  event_count = 0;
  state = ST_CHECK;
  attachInterrupt( 0, eint_isr, RISING ); // use INT0
}

void loop() {
  static uint8_t led_state = 0;
  
  switch (state) {
    case ST_CHECK:
      if ( event_count > 1 ) {
        state = ST_ALARM; // change state to ST_ALARM
        ts = millis(); // update timestamp
      }
      break;
    case ST_ALARM:
      if ( millis() - ts >= BLINK_TIME_MS ) {
        state = ST_WAIT; // change state to ST_WAIT
        ts = millis();
        led_state = 0;
        digitalWrite( ALARM_PIN, led_state ); // turn off alarm
      } 
      else {
        led_state ^= 1;
        digitalWrite( ALARM_PIN, led_state ); // toggle alarm
        delay(50);
      }
      break;
    case ST_WAIT:
      if ( millis() - ts >= WAIT_TIME_MS ) {
         event_count = 0; // reset event counter
         state = ST_CHECK;
      }
      break;
    default: 
      state = ST_CHECK; 
      break;
  }
}

////////////////////////////////////////////////////////////////////////

ไม่มีความคิดเห็น:

แสดงความคิดเห็น

มินิโปรเจค Arduino Calculator (แก้ไข)

 https://www.electronicshub.org/arduino-calculator/ #include <LiquidCrystal.h> ...