Added skeleton for PIR sensor; non-functional and omitting for the time being
This commit is contained in:
parent
92c827ecb5
commit
ecb8cbc345
|
@ -25,6 +25,10 @@ In the code you'll find the following variables which may need tweaking
|
|||
* SLEEP_INTERVAL : The amount of time (in miliseconds) that the device sleeps for between checks
|
||||
* ALERT_LEVEL : The battery level that generates an alert. This must be set between 1 and 32%, inclusive
|
||||
|
||||
Known Issues
|
||||
=
|
||||
Please see the TODO.md document for current, known issues.
|
||||
|
||||
Code License
|
||||
=
|
||||
All code is licensed under the Apache 2 License (http://www.apache.org/licenses/LICENSE-2.0) unless otherwise specified.
|
||||
|
@ -35,6 +39,7 @@ The following external libraries are included and may not be licensed under the
|
|||
* SparkFun Lipo Fuel Gauge Library / Example code
|
||||
* "Sleepy" class from https://github.com/jcw/jeelib/
|
||||
* Metro timing lib from https://github.com/thomasfredericks/Metro-Arduino-Wiring
|
||||
* SharpIR lib from http://playground.arduino.cc/Main/SharpIR
|
||||
|
||||
Non-Code License
|
||||
=
|
||||
|
|
1
TODO.md
1
TODO.md
|
@ -3,3 +3,4 @@
|
|||
* Add note about adjusting values for distance
|
||||
* Finish Fritzing documentation (breadboard / schematic)
|
||||
* Update BoM so it's complete
|
||||
* Fix PIR distance sensor code
|
||||
|
|
128
src/SharpIR.cpp
Normal file
128
src/SharpIR.cpp
Normal file
|
@ -0,0 +1,128 @@
|
|||
/************************************************************************************************************
|
||||
* SharpIR.h - Arduino library for retrieving distance (in cm) from the analog GP2Y0A21Y and GP2Y0A02YK *
|
||||
* Distance sensors *
|
||||
* Copyright 2014 Dr. Marcal Casas-Cartagena (marcal.casas@gmail.com) *
|
||||
* Last update: 07.01.2014 *
|
||||
************************************************************************************************************
|
||||
|
||||
************************************************************************************************************
|
||||
* This library is free software; you can redistribute it and/or *
|
||||
* modify it under the terms of the GNU Lesser General Public *
|
||||
* License as published by the Free Software Foundation; either *
|
||||
* version 2.1 of the License, or (at your option) any later version. *
|
||||
* *
|
||||
* This library 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 *
|
||||
* Lesser General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU Lesser General Public *
|
||||
* License along with this library; if not, write to the Free Software *
|
||||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
|
||||
***********************************************************************************************************/
|
||||
|
||||
|
||||
// The Sahrp IR sensors are cheap but somehow unreliable. I've found that when doing continous readings to a
|
||||
// fix object, the distance given oscilates quite a bit from time to time. For example I had an object at
|
||||
// 31 cm. The readings from the sensor were mainly steady at the correct distance but eventually the distance
|
||||
// given dropped down to 25 cm or even 16 cm. That's quite a bit and for some applications it is quite
|
||||
// unacceptable. I checked the library http://code.google.com/p/gp2y0a21yk-library/ by Jeroen Doggen
|
||||
// (jeroendoggen@gmail.com) and what the author was doing is to take a bunch of readings and give an average of them
|
||||
|
||||
// The present library works similary. It reads a bunch of readings (avg), it checks if the current reading
|
||||
// differs a lot from the previous one (tolerance) and if it doesn't differ a lot, it takes it into account
|
||||
// for the mean distance.
|
||||
// The distance is calculated from a formula extracted from the graphs on the sensors datasheets
|
||||
// After some tests, I think that a set of 20 to 25 readings is more than enough to get an accurate distance
|
||||
// Reading 25 times and return a mean distance takes 53 ms. For my application of the sensor is fast enough.
|
||||
// This library has the formulas to work with the GP2Y0A21Y and the GP2Y0A02YK sensors but exanding it for
|
||||
// other sensors is easy enough.
|
||||
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "SharpIR.h"
|
||||
|
||||
|
||||
|
||||
SharpIR::SharpIR(int irPin, int avg, int tolerance, int sensorModel) {
|
||||
|
||||
_irPin=irPin;
|
||||
_avg=avg;
|
||||
_tol=tolerance/100;
|
||||
_model=sensorModel;
|
||||
|
||||
analogReference(DEFAULT);
|
||||
|
||||
}
|
||||
|
||||
|
||||
// When you initialize the library object on your sketch you have to pass all the above parameters:
|
||||
|
||||
// irPin is obviously the pin where the IR sensor is attached
|
||||
// avg is the number of readings the library does
|
||||
// tolerance indicates how similar a value has to be from the last value to be taken as valid. It should be a
|
||||
// value between 0 and 100, like a %. A value of 93 would mean that one value has to be, at least, 93% to the
|
||||
// previous value to be considered as valid.
|
||||
// sensorModel is a int to differentiate the two sensor models this library currently supports:
|
||||
// 1080 is the int for the GP2Y0A21Y and 20150 is the int for GP2Y0A02YK. The numbers reflect the
|
||||
// distance range they are designed for (in cm)
|
||||
|
||||
|
||||
|
||||
|
||||
int SharpIR::cm() {
|
||||
|
||||
int raw=analogRead(_irPin);
|
||||
float voltFromRaw=map(raw, 0, 1023, 0, 5000);
|
||||
|
||||
int puntualDistance;
|
||||
|
||||
if (_model==1080) {
|
||||
|
||||
puntualDistance=27.728*pow(voltFromRaw/1000, -1.2045);
|
||||
|
||||
}else if (_model==20150){
|
||||
|
||||
puntualDistance=61.573*pow(voltFromRaw/1000, -1.1068);
|
||||
|
||||
}
|
||||
|
||||
|
||||
return puntualDistance;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
int SharpIR::distance() {
|
||||
|
||||
_p=0;
|
||||
_sum=0;
|
||||
|
||||
|
||||
for (int i=0; i<_avg; i++){
|
||||
|
||||
int foo=cm();
|
||||
|
||||
if (foo>=(_tol*_previousDistance)){
|
||||
|
||||
_previousDistance=foo;
|
||||
_sum=_sum+foo;
|
||||
_p++;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
int accurateDistance=_sum/_p;
|
||||
|
||||
return accurateDistance;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
49
src/SharpIR.h
Normal file
49
src/SharpIR.h
Normal file
|
@ -0,0 +1,49 @@
|
|||
/************************************************************************************************************
|
||||
* SharpIR.h - Arduino library for retrieving distance (in cm) from the analog GP2Y0A21Y and GP2Y0A02YK *
|
||||
* Distance sensors *
|
||||
* Copyright 2014 Dr. Marcal Casas-Cartagena (marcal.casas@gmail.com) *
|
||||
************************************************************************************************************
|
||||
|
||||
************************************************************************************************************
|
||||
* This library is free software; you can redistribute it and/or *
|
||||
* modify it under the terms of the GNU Lesser General Public *
|
||||
* License as published by the Free Software Foundation; either *
|
||||
* version 2.1 of the License, or (at your option) any later version. *
|
||||
* *
|
||||
* This library 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 *
|
||||
* Lesser General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU Lesser General Public *
|
||||
* License along with this library; if not, write to the Free Software *
|
||||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
|
||||
***********************************************************************************************************/
|
||||
|
||||
#ifndef SharpIR_h
|
||||
#define SharpIR_h
|
||||
|
||||
#include "Arduino.h"
|
||||
|
||||
class SharpIR
|
||||
{
|
||||
public:
|
||||
SharpIR (int irPin, int avg, int tolerance, int sensorModel);
|
||||
int distance();
|
||||
|
||||
|
||||
private:
|
||||
|
||||
int cm();
|
||||
|
||||
int _irPin;
|
||||
int _model;
|
||||
int _avg;
|
||||
int _p;
|
||||
int _sum;
|
||||
int _previousDistance;
|
||||
int _tol;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
17
src/src.ino
17
src/src.ino
|
@ -6,6 +6,7 @@
|
|||
#include "MAX1704.h"
|
||||
#include "Sleepy.h"
|
||||
#include "Metro.h"
|
||||
#include "SharpIR.h"
|
||||
#include "Adafruit_NeoPixel.h"
|
||||
#ifdef __AVR__
|
||||
#include <avr/power.h>
|
||||
|
@ -15,9 +16,10 @@
|
|||
// Various defines / pins / etc
|
||||
// TWEAK THESE ACCORDINGLY
|
||||
// //////////
|
||||
#define DEBUG false
|
||||
#define DEBUG true
|
||||
#define INTERACTIVE_DEBUG false
|
||||
#define USE_SLEEP true
|
||||
#define USE_PIR false
|
||||
#define SLEEP_INTERVAL 500 // miliseconds
|
||||
#define ALERT_LEVEL 15 // battery alert level (%) [1-32% are valid values here]
|
||||
#define POT_BRIGHT A0
|
||||
|
@ -25,6 +27,7 @@
|
|||
#define PIN_ALWAYS_ON 9 // Switch that enables always on/off of pixels
|
||||
#define PIN_WHITE 12 // Switch that switches between color wiper and white color
|
||||
#define PIN_DOOR 11 // Door sensor pin
|
||||
#define PIN_PIR A2 // Pin with PIR distance sensor
|
||||
#define PIN_NEO 6 // Pin with the pixel comm line
|
||||
#define NEO_PIX_NUM 2 // FIXME: this should be 5 for final build for safe lighting
|
||||
|
||||
|
@ -45,6 +48,10 @@ bool white = false;
|
|||
bool max_found = false;
|
||||
bool door_open = false;
|
||||
bool lights_on = false;
|
||||
#if USE_PIR
|
||||
SharpIR sharp(PIN_PIR, 25, 93, 1080);
|
||||
int distance = 0;
|
||||
#endif
|
||||
|
||||
// //////////
|
||||
// Various function definitions
|
||||
|
@ -76,6 +83,7 @@ void setup() {
|
|||
pinMode(PIN_ALWAYS_ON, INPUT_PULLUP);
|
||||
pinMode(PIN_WHITE, INPUT_PULLUP);
|
||||
pinMode(PIN_DOOR, INPUT_PULLUP);
|
||||
pinMode(PIN_PIR, INPUT);
|
||||
pinMode(POT_BRIGHT, INPUT);
|
||||
pinMode(POT_COLOR, INPUT);
|
||||
|
||||
|
@ -135,6 +143,9 @@ void read_values() {
|
|||
always_on = (digitalRead(PIN_ALWAYS_ON) == LOW); // Toggle button pressed == ALWAYS ON
|
||||
white = (digitalRead(PIN_WHITE) == LOW); // Shorted pin to enable white color for LEDs
|
||||
door_open = (digitalRead(PIN_DOOR) == HIGH); // SparkFun door sensor is indicating door is open (NO for door closed state, set as input_pullup so the logic is inverted)
|
||||
#if USE_PIR
|
||||
distance = sharp.distance();
|
||||
#endif
|
||||
lights_on = always_on || (door_open); // Take a look at all appropriate values and set lights on/off as appropriate
|
||||
}
|
||||
|
||||
|
@ -253,6 +264,10 @@ void print_debug() {
|
|||
Serial.println(always_on);
|
||||
Serial.print("Door sensor: ");
|
||||
Serial.println(door_open);
|
||||
#if USE_PIR
|
||||
Serial.print("PIR Sensor: ");
|
||||
Serial.println(distance);
|
||||
#endif
|
||||
Serial.println("----------");
|
||||
delay(250);
|
||||
}
|
||||
|
|
Reference in a new issue