Hi,
Thank-you for your help before on the topic of making things for dogs, I am currently struggling with trying to only take the reading if it is detected for a certain amount of time (similar to debouncing in switches).
I have tried to code this in but it does not seem to be taking the time I set. Is there a way of checking that a code is within range for a certain period before taking the reading? Am I doing this totally wrong?
While I am aware sharp can mean the distance in the library fiddling with this too much seems to cause more false readings.
Thank-you for any guidance you can give,
#include <SharpIR.h> // Sharp Libary
#include <Keyboard.h> // Keyboard
#define ir A0
#define model 20150
#define LEDPin 13 // Onboard LED
#define ir1 A1
//unsigned long minSample, maxSample;
const int maximumRange = 100; // Maximum range needed in cm
const int minimumRange = 0; // Minimum range needed in cm
boolean shouldPress;
SharpIR sharp(ir, 5, 93, model);
SharpIR sharp1(ir1, 5, 93, model);
// new bit to average out
#define NBSAMPLES 100
unsigned long lastMeasures[NBSAMPLES];
unsigned long lastMeasures1[NBSAMPLES];
uint8_t currentSample;
uint8_t currentSample1;
uint8_t lastMinIndex, lastMaxIndex;
unsigned long minSample, maxSample;
void setup()
{
Serial.begin(9600);
pinMode (ir, INPUT);
pinMode (ir1, INPUT);
pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)
currentSample = 0;
currentSample1 = 0;
minSample = 0xFFFFFFFF;
maxSample = 0UL;
lastMinIndex = 0;
lastMaxIndex = 0;
}
void loop()
{
unsigned long pepe1 = millis(); // takes the time before the loop on the library begins
int dis = sharp.distance(); // this returns the distance to the object you're measuring
int dis1 = sharp1.distance(); // Returns the distance of the second sensor (pin A1)
unsigned long pepe2 = millis() - pepe1; // the following gives you the time taken to get the measurement
Serial.print("Time taken (ms): ");
Serial.println(pepe2);
Serial.print("Mean distance sensor 1: "); // returns it to the serial monitor
Serial.println(dis);
Serial.print("Mean distance sensor 2: "); // returns it to the serial monitor
Serial.println(dis1);
lastMeasures[currentSample] = dis;
lastMeasures1[currentSample1] = dis1;
if ((dis <= maximumRange && dis >= minimumRange) || (dis1 <= maximumRange && dis1 >= minimumRange )){
Serial.println("Dog detected in range");
if(shouldPress == true) // checks if keypress or LED needs to happen
{
digitalWrite(LEDPin, HIGH);
Keyboard.press('d');
shouldPress = false; // sets to false so that it has happened
}
}
else
{
Serial.println("out of range");
digitalWrite(LEDPin, LOW);
Keyboard.releaseAll();
shouldPress = true;
}
}