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.
At the moment, I have gotten so far as to get this to trigger the sending of the signal within a specific distance, the problem now being that after the delay and the device beings to read again, if it continues to sense a person in proximity, it triggers the signal again. I can see why this is occurring because of the way the code runs at present, and I'm guessing there needs to be something written into the programming to account for state changes, but I'm just not sure where to begin as most tutorials I can find are for button pushes, which are a bit simpler. Also, ideally, if there is a state change where the person moves closer, then the signal should not be not sent either. And if the person were to go backwards and outside the range, to trigger it again and turn it off. Thanks in advance.
#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 after being activated for the first time.
index = 0;
total = 0;
}
}