Hi everyone,
For Blog #4 I am attaching the code for the steering wheel touch sensor. Like I mentioned this is for the Arduino and it will be made to run on the Nucleo (When I get the Nucleo working). I am also blogging about the other parts of my project I have been working on.
First the code:
#include <CapacitiveSensor.h>
CapacitiveSensor cs_4_8 = CapacitiveSensor(4,8); // 2M resistor between pins 4 & 8, pin 8 is also the sensor pin with the wire to the steering wheel
void setup()
{
cs_4_8.set_CS_AutocaL_Millis(0xFFFFFFFF);
Serial.begin(9600);
pinMode(7,OUTPUT);
pinMode(9,OUTPUT);
}
void loop()
{
long sensor1 = cs_4_8.capacitiveSensor(50);
Serial.println(sensor1);
if(sensor1 >= 300) // Sensor being touched - No alerts
{
digitalWrite(7,LOW);
}
else{ // Sensor not touched - Alert activated
digitalWrite(7,HIGH); // LED lit and alarm activated until sensor touched
tone(9,500);
delay(100);
tone(9,600);
delay(100);
tone(9,700);
delay(100);
tone(9,800);
delay(100);
tone(9,900);
delay(100);
tone(9,1000);
delay(100);
tone(9,900);
delay(100);
tone(9,800);
delay(100);
tone(9,700);
delay(100);
tone(9,600);
delay(100);
tone(9,500);
delay(100);
noTone(9);
}
}
The other sensors I am working on are as follows:
Tilt sensor: This suggestion is from DAB (Thank you for this!!). This sensor will be attached to a head band and will sense if your head tips forward due to nodding off. This is a serious indication that you must stop driving. Attached is the code.
int ledPin = 13;
int switcher = 3;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(switcher, INPUT);
}
void loop() {
if (digitalRead(switcher) == HIGH)
digitalWrite(ledPin, HIGH); // Turn on LED and sound alert when the sensor is tilted
tone(9,500);
delay(100);
tone(9,600);
delay(100);
tone(9,700);
delay(100);
tone(9,800);
delay(100);
tone(9,900);
delay(100);
tone(9,1000);
delay(100);
tone(9,900);
delay(100);
tone(9,800);
delay(100);
tone(9,700);
delay(100);
tone(9,600);
delay(100);
tone(9,500);
delay(100);
noTone(9);
else
digitalWrite(ledPin, LOW); // Turn off LED and sound alert when the sensor is not triggered
}
Loud sound sensor:
This is to alert you if someone is honking their horn at you or any other loud noises. This could be from falling asleep at a traffic light and other outside warnings. This sensor will be placed in the trunk of the vehicle.
int soundPin = 10;
int soundVal = HIGH;
boolean bAlarm = false;
unsigned long lastDetectTime;
int soundTime = 500; // Number of milli seconds to keep the sound alarm high
void setup ()
{
Serial.begin(9600);
pinMode (soundPin, INPUT) ; // input from sensor
}
void loop ()
{
soundVal = digitalRead (soundPin) ; // read the sound alarm time
if (soundVal == LOW) // If we hear a sound
{
lastDetectTime = millis(); // record the time of the sound alarm
if (!bAlarm){
tone(9,500);
delay(100);
tone(9,600);
delay(100);
tone(9,700);
delay(100);
tone(9,800);
delay(100);
tone(9,900);
delay(100);
tone(9,1000);
delay(100);
tone(9,900);
delay(100);
tone(9,800);
delay(100);
tone(9,700);
delay(100);
tone(9,600);
delay(100);
tone(9,500);
delay(100);
noTone(9);
bAlarm = true;
}
}
else
{
if( (millis()-lastDetectTime) > soundTime && bAlarm){
bAlarm = false;
}
}
}
Next will be creating the vehicle vibration sensor and the vibrating wrist band alert. I am wanting to do the heart rate monitor but I don't think I will be receiving the sensor before the project is to be completed. Hope you enjoyed this update on my project.
Thank you
Dale W
Note: This system can be also used for distracted driving as well (Another very dangerous driving issue)
Top Comments