SRF05 Motion Sensing

Hi,

I’m attempting to use my Arduino like a TV remote, with a motion sensor to trigger the on/off function. It’s for an art installation, so the viewer is required to enter within a certain proximity to the television, and subsequently the Arduino and sensor, in order for the TV to turn on and be playing a video. Specifically, I’m using the SRF05 Range Finder in order to achieve this, specifically for its ability to program in a range of distances. I’ve gotten it to work insofar as that motion within a certain distance triggers the output of the remote codes via the IR LED. The problem I’m now having is that every time the loop runs, as long as a person is sensed within the given distance, the signal is sent again and again, causing it to continually turn the TV on and off. I suspect that this is all in the coding, but I’m not sure where to start.

I’m using Ken Shirriff’s IRremote library to send the signals, and this code so far, credited to Lucky Larry (http://luckylarry.co.uk/arduino-projects/arduino-redefining-the-tv-remote/), with adaptations written in by me, which controls the function of the SRF05:

#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 is less than a given value (indicating proximity to the sensor)…
if (average <= 190) {
Serial.println(“Power”);
// 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(1000);

}
if (index >= numReadings) { //to ensure that the signal does not continue to be sent without further distance readings, after being activated for the first time.
index = 0;
total = 0;
}
}

Any help would be massively appreciated.

Any help would be massively appreciated.

Every time the average is less than 190, you send the code to toggle the TV. You need to read, understand, and embrace the state change example's philosophy.

Only send the IR code when there is a significant change.

NOTHING ever follows a } on the same line.

Two }} NEVER go on the same line.

Hi, I do understand that this is why it's happening based on how the code currently works (it's a distance I've set myself etc.), perhaps not well communicated initially. So my question overall would certainly be: how do I go about writing that the IR code is only sent with a significant change in reading? Or, where else can I look to learn to do so?

As for your other notes, I'm aware of those, it's an issue of the copying and pasting which I was too lazy to rectify on here before uploading, but thanks.