Other important thing we use in this example is the sleeping feature in SquidBee, if you see the code, you'll see arduino is most of the time sleeping (saving power). For waking up SquidBee we use one of the interrupts sources in Arduino, the one on pin 2 (where we connect the sensor). When a vibration is detected the voltage value on pin 2 goes to 0 V and the interrupt is triggered, once the interrupts happens arduino is waked up, it wakes up the XBee, sends the message and later all the system goes back to sleep.
/* * Copyright (C) 2008 Libelium Comunicaciones Distribuidas S.L. * * This file is part of SquidBee code examples * squidbee_vibration.pde is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * *You should have received a copy of the GNU General Public License *along with Foobar. If not, see <http://www.gnu.org/licenses/>. */ /* Name: squidbee_vibration.pde * Version 0.0.1 * Desc: A vibration sensor is attached to digital input 2, an interrupt is set in this pin * in SquidBee. When the sensor detects vibration, the interrupt is activated and Squidbee * wakes up and send a message * Author: Marcos Yarza <m.yarza"at/removethis"libelium.com> */#include <avr/sleep.h> int wakePin = 2; // pin used for waking up int XBee_wake_pin = 7; // pin used for waking up XBee (connected to pin 9 in XBee) void wakeUpNow() // here the interrupt is handled after wakeup { // do nothing } void setup() { pinMode(wakePin, INPUT); pinMode(XBee_wake_pin, INPUT); digitalWrite(XBee_wake_pin, HIGH); Serial.begin(19200); attachInterrupt(0, wakeUpNow, LOW); // use interrupt 0 (pin 2) and run function // wakeUpNow when pin 2 gets LOW } void sleepNow() // here we put the arduino to sleep { set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here sleep_enable(); // enables the sleep bit in the mcucr register // so sleep is possible. just a safety pin attachInterrupt(0,wakeUpNow, LOW); // use interrupt 0 (pin 2) and run function // wakeUpNow when pin 2 gets LOW sleep_mode(); // here the device is actually put to sleep!! // THE PROGRAM CONTINUES FROM HERE AFTER WAKING UP sleep_disable(); // first thing after waking from sleep: // disable sleep... detachInterrupt(0); // disables interrupt 0 on pin 2 so the // wakeUpNow code will not be executed // during normal running time. } void loop() { digitalWrite(XBee_wake_pin, LOW); // after wakes up the micro, wakes up XBee delay(10); Serial.println("Vibration alarm!"); // sends the alarm messge digitalWrite(XBee_wake_pin, HIGH); // sleep XBee again delay(100); sleepNow(); // sleep function called here }
Комментариев нет:
Отправить комментарий