Hi there,
My current Project is a sensor which uses a relay to open a door when something/-one approaches. The sensor reacts every time but sometimes it's too slow but I can't really Change much on the hardware-side anymore so i want to make my program faster.
I'd really appreciate all advice and every piece of useful information you have to offer.
So here's what it looks like:
The sensors I use are a HC-SR501 Motion sensor (Infrared/PIR) and a HC-SR04 Ultrasonic Sensor (US). The relay is a standard one I got in a sensor kit for arduino.
My Code:
#define trigPinA 11 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define echoPinA 12 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define pirA 8
#define trigPinB 9
#define echoPinB 10
#define pirB 7
#define relais 6
float duration, distance; //Variablen fuer den US
int pirState; //Variable fuer den Zustand des PIR-Pins
void setup()
{
Serial.begin(9600);
pinMode(relais, OUTPUT);
pinMode(trigPinA, OUTPUT);
pinMode(echoPinA, INPUT);
pinMode(pirA, INPUT);
pinMode(trigPinB, OUTPUT);
pinMode(echoPinB, INPUT);
pinMode(pirB, INPUT);
}
bool sideA()
{
digitalWrite(trigPinA, LOW); // Senden des Trigger-Signals
delayMicroseconds(2);
digitalWrite(trigPinA, HIGH);
delayMicroseconds(10);
digitalWrite(trigPinA, LOW);
duration = pulseIn(echoPinA, HIGH); // Empfangen und Umrechnen des Echo-Signals
distance = (duration*.0343)/2;
pirState = digitalRead(pirA); // Auslesn des Zustandes des PIRs
Serial.print("DistanceA: "); //Debugging ueber seriellen Monitor
Serial.println(distance);
Serial.print("pirA: ");
Serial.println(pirState);
if(distance < 250 || pirState == 1)
{
return true;
}
else
{
return false;
}
}
bool sideB()
{
digitalWrite(trigPinB, LOW); // Senden des Trigger-Signals
delayMicroseconds(2);
digitalWrite(trigPinB, HIGH);
delayMicroseconds(10);
digitalWrite(trigPinB, LOW);
duration = pulseIn(echoPinB, HIGH); // Empfangen und Umrechnen des Echo-Signals
distance = (duration*.0343)/2;
pirState = digitalRead(pirB); // Auslesen des Zustandes des PIRs
Serial.print("DistanceB: "); //Debugging ueber seriellen Monitor
Serial.println(distance);
Serial.print("pirB: ");
Serial.println(pirState);
if(distance < 250 || pirState == 1)
{
return true;
}
else
{
return false;
}
}
void loop()
{
sideA();
sideB();
if(sideA() == true || sideB() == true)
{
digitalWrite(relais, HIGH);
delay(100);
digitalWrite(relais, LOW);
delay(1000);
}
Serial.println("----------------");
}
My ideas are the following:
- Leave away the calculation from µs to cm
- Comment out the Debugging messages
- Send the Ultrasonic triggers simultaneously (crosstalk isn't possible)
My doubts are the following:
Will commenting out the debugging messages really do much?
Can I measure the duration for both sensors simultaneously?
I was also wondering wether or not it's possible to use the functions (sideA & sideB) in my if-condition if I haven't "executed" them before in the loop?
And could I somehow stop the measurement of travel time of the ultrasonic wave midway? Like making it stop after 400µs of measuring because then the object would be too far away anyway? That would drastically decrease the time the program needs for one loop in my opinion.
Thank you in advance for your answers,
Smagel