SparkFun Forums 

Where electronics enthusiasts find answers.

For the discussion of Arduino related topics.
By elsewhereartist
#194301
Hi everyone. Please excuse any ignorance on my part, I'm fairly new to Arduino and trying to do something very specific with it (just for now, I intend to continue learning after my project), in a short space of time. I'm hoping that what I ask is possible.

I'm using an SRF05 as a way of triggering an IR signal to be sent to a TV to turn it on when someone comes within a given distance of the sensor. Through a lot of trial and error (and using Ken Shirriff's IR library and an adapted code thanks to Lucky Larry), I've finally managed to get it to work so that the signal is sent when the measured value crosses the threshold for the first time, to ensure that it does not continue to be triggered every time the loop runs again so long as someone is standing in front of the sensor. All I have left to achieve is for it to do the opposite as well, so that when a person moves away and out of the given distance, it causes the signal to be sent again only once, causing the TV to turn off as they leave. Any ideas how to do this?

Here's everything I have so far:

#include <IRremote.h>
IRsend irsend;

const int numReadings = 1; // set a variable for the number of readings to take
int index = 0; // the index of the current reading
int total = 0; // the total of all readings
int average = 0; // the average
int oldAverage = 0; // the old average
int echoPin = 2; // the SRF05's echo pin
int initPin = 4; // the SRF05's init pin
unsigned long pulseTime = 0; // variable for reading the pulse
unsigned long distance = 0; // variable for storing distance

// setup my arrays for each signal I want to send,corresponding to commands usually performed by the remote control
unsigned int powerOn[67] = {8900,4400, 600,550, 550,550, 600,550, 500,600, 550,550, 550,550, 550,600, 550,550, 550,1650, 550,1650, 600,1650, 550,1700, 550,1650, 550,1650, 600,1650, 550,600, 550,550, 550,1650, 600,550, 550,1650, 550,600, 500,600, 550,550, 550,550, 550,1650, 600,550, 550,1650, 600,550, 550,1650, 600,1600, 600,1650, 550,1650, 600};

void setup() {
// make the init pin an output:
pinMode(initPin, OUTPUT);
// make the echo pin an input:
pinMode(echoPin, INPUT);
// initialize the serial port:
Serial.begin(9600);
}

void loop() {

// loop for a number of readings on the SRF-05 to get an average to smooth the results.
for (index = 0; index<=numReadings;index++) {
digitalWrite(initPin, LOW);
delayMicroseconds(50);
digitalWrite(initPin, HIGH);
delayMicroseconds(50);
digitalWrite(initPin, LOW);
pulseTime = pulseIn(echoPin, HIGH);
distance = pulseTime/58;
total = total + distance;
delay(10);
}
// store the previous reading
oldAverage = average;
// store the current reading
average = total/numReadings;
// debug to check for spikes in the sensor etc..
Serial.println(average);


// if my distance from the sensor is less than a given value (indicating proximity to the sensor)... AND if my distance remains within the range - TURN ON and REMAIN ON
if (average <190) {
if (oldAverage >=190) {
Serial.println("Power On");
// use the IR library to send my signal (array, number of items in array, Khz)
irsend.sendRaw(powerOn,67,38);
// these delays depend on how long it takes the device to recognise the signal sent and to act.
delay(5000);
}
}
if (index >= numReadings)
{ //to ensure that the signal does not continue to be sent after being activated for the first time.
index = 0;
total = 0;
}
}

Thanks in advance, really hope someone can help! :)
By jbike
#194491
It could be that in using delay, you prevent things like the sonar from continually operating. If the sonar doesn't see the person at close range, then it should send the off IR command. Use a unsigned long variable to hold the start time, set it equal to millis(); when the person comes close. Also set a flag to prevent the resetting of the start time if the person is still in close range the next time through the loop. If the sonar still detects the person and the flag is still set, compare the current millis(); minus the variable and see if it is 5 seconds or more. Then reset the flag, then check the sonar distance, and if the sonar distance indicates the person is gone, send the off IR command.