Hi,
I am a newbie here and not expert into programming thing. Appreciate if I can have valuable advice and guidance from experts in this forum.
My intention/idea is to de-energised vehicle ignition relay when the ultrasonic sensor is not detecting driver presence in the vehicle when the vehicle engine is still running.
I managed to have source code online to for "ultrasonic sensor" but I have no idea about code to de-energise the relay and how to do wire connection from Arduino to the ignition relay.
Additional concerns: I also wonder whether using the ultrasonic sensor to detect human presence is practical? In fact, recently I also found there is a grid eye evaluation breakout board to detect human presence. I really appreciate your help. May God Bless You all.
Code below for ultrasonic sensor(Credit to owner Oleesa)
...
#include <Servo.h>
#include <LiquidCrystal.h>
Servo robotNeck; //servo variable with name robotNeck
LiquidCrystal lcd(1, 2, 4, 5, 6, 7); // Creates an LCD object with parameters: (rs, enable, d4, d5, d6, d7)
//assign pin numbers
//const int trigPin = 9; //Pin 9 where ultrasonic distance sensor trig is connected
//const int echoPin = 10; //Pin 10 where ultrasonic distance sensor echo is connected
const int sigPin = 13;
int distanceCm, distanceIn;
long timeVal;
const int speedOfSound = 0.0343; //sets the speed of sound to a constant value
void setup() {
robotNeck.attach(3); //Servo is connected to arduino pin ~3
//pinMode(echoPin, INPUT); //sets echoPin as input
//pinMode(trigPin, OUTPUT); //sets trigPin as output
lcd.begin(16, 2); //Initializes the lcd and specifies the dimension (width and height, respectively) of the display
lcd.setCursor(3,0);
lcd.print("Welcome to");
delay(1000);
lcd.clear();
lcd.setCursor(4,0);
lcd.print("OLEESA'S");
lcd.setCursor(1,1);
lcd.print("Distance Meter");
delay(2500);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Booting.");
delay(500);
lcd.clear();
lcd.print("Booting..");
delay(1000);
lcd.clear();
lcd.print("Booting...");
delay(1500);
lcd.clear();
}
void loop() {
//Rotate robotNeck from 15 to 165 because of limits which might draw too much current
for(int j=15; j<=165; j++){
robotNeck.write(j);
delay(10);
distanceCm = distCalc();
distanceIn = distCalc();
//To display value on the lcd
lcd.setCursor(0,0); // Sets the location at which subsequent text written to the LCD will be displayed
lcd.print("Distance: "); // Prints string "Distance: " on the LCD
lcd.print(distanceCm); // Prints the distance value from the sensor
lcd.print("cm"); // Prints the unit of measurement
delay(10); //waits for 10 milliseconds
lcd.setCursor(0,1); // Sets the location at which subsequent text written to the LCD will be displayed
lcd.print("Distance: "); // Prints string "Distance: " on the LCD
lcd.print(distanceIn); // Prints the distance value from the sensor
lcd.print("in"); // Prints the unit of measurement
delay(100); //waits for 100 milliseconds
}
for(int j=165; j>=15; j--){
robotNeck.write(j);
delay(10);
distanceCm = distCalc();
distanceIn = distCalc();
//To display value on the lcd
lcd.setCursor(0,0); // Sets the location at which subsequent text written to the LCD will be displayed
lcd.print("Distance: "); // Prints string "Distance: " on the LCD
lcd.print(distanceCm); // Prints the distance value from the sensor
lcd.print("cm"); // Prints the unit of measurement
delay(100); //waits for 100 milliseconds
lcd.setCursor(0,1); // Sets the location at which subsequent text written to the LCD will be displayed
lcd.print("Distance: "); // Prints string "Distance: " on the LCD
lcd.print(distanceIn); // Prints the distance value from the sensor
lcd.print("in"); // Prints the unit of measurement
delay(100); //waits for 100 milliseconds
}
}
int distCalc(){
pinMode(sigPin, OUTPUT);
digitalWrite(sigPin, LOW);
delayMicroseconds(2);
digitalWrite(sigPin, HIGH);
delayMicroseconds(10);
digitalWrite(sigPin, LOW);
// The same pin is used to read the signal from the PING))): a HIGH pulse
// whose duration is the time (in microseconds) from the sending of the ping
// to the reception of its echo off of an object.
pinMode(sigPin, INPUT);
timeVal = pulseIn(sigPin, HIGH);
//converts time to distance
distanceCm = timeVal / 29 / 2;
distanceIn = timeVal / 74 / 2;
return distanceCm, distanceIn;
}