Code for making a Car Driver Sleep Detection System using an Accelerometer

This post is related(is a follow-up of) to: Jerk Sensor? - Frequently-Asked Questions - Arduino Forum

Hello Arduino Explorers,

Some of you guys may have already seen my earlier(first) post(Suggesstions For An Innovative Science Project - Project Guidance - Arduino Forum) and many may also have been utter fed up after that post but I sincerely apologize.

Finally I have found a proper topic for my school project. A "Driver Sleep Detection System" using an arduino mini, a 9V battery, a piezzo buzzer and an accelerometer, quite a simple setup...(I hope!)

So you might be wondering, why I am here...
As I am a beginner, I know nothing of code. Absolute nothing. So I am shamefully asking the generous volunteers of this forum to please give me the code (hope its just a few lines) for my project.

I am not a lazy noob, its just that I don't have time to study the arduino code library and then do trial and error.

So Please Please Help Me Out One Last Time. I don't want to trouble anybody, I am just requesting an experienced coder to just help me out if he's free for an hour.

Requirements:

The setup is such that it would be worn by the driver on his/her head especially at night time. The arduino has to be configured such that it will trigger a beep alarm when it senses jerky moments(that's when the driver is drousy and is about to sleep) and the alarm will alert the driver as well as the other passengers aboard. There should be a tweak-able parameter to change the sensitivity of the accelerometer as well as an option to change the beepers beeping.

I would be happier if I would get a brief description of the wiring of a piezzo buzzer and the accelerometer.

Suggestions for any extra equipment such as LEDs would be highly encouraged.

Hope I will be able to finish this project within a few days.

Thanks in Advance.

Prashanth J.K.

Has you research shown how many minutes a 9 volt battery will operate your project?

Paul

especially at night time

I hope you realize that that is not necessarily the most likely time for drivers to fall asleep.

I rarely get sleepy going to work in the middle of the night. I often get sleepy going home from work in broad daylight.

There are several accelerometers that work well with the Arduino. Each of them comes with a library that includes examples.

I am not a lazy noob, its just that I don't have time to study the arduino code library and then do trial and error.

You mean that you procrastinated for too long.

I would be happier if I would get a brief description of the wiring of a piezzo buzzer and the accelerometer.

Take a look at how many wires the buzzer has. Isn't it rather obvious that power goes in one, and out the other? Where do you suppose the power is going to come from? Where do you suppose it is going to go to?

How to wire the accelerometer just might possibly depend on which freaking accelerometer you are talking about, wouldn't you think?

The arduino has to be configured such that it will trigger a beep alarm when it senses jerky moments

The Arduino isn't going to sense anything. It is going to get facts and data - something you don't seem to be too familiar with. It will analyze the data, and see if it fits some pattern (i.e. represents "jerky motions").

Suggestions for any extra equipment such as LEDs would be highly encouraged.

How about a cattle prod up the wearer's ... Nah, never mind.

Hope I will be able to finish this project within a few days.

Sure you will.

Can somebody help me finish it...

My finish of choice is a mix of mineral oil and bees wax. Pretty easy to apply, and buffs to a high gloss.

If you had something else in mind, you might give a hint as to how that code is unfinished.

I suppose the output code must be fed in here:

I have no idea what "output", "code" or "fed" mean to you. Obviously not the same things they mean to me.

None of my code ever needs to be fed.

pjkrs60:
Nevermind! @PaulS , I managed to fetch some code (from here: https://www.arduino.cc/en/Tutorial/Memsic2125?from=Tutorial.AccelerometerMemsic2125 ) myself... Can somebody help me finish it...

/*

Memsic2125

Read the Memsic 2125 two-axis accelerometer. Converts the pulses output by the
  2125 into milli-g's (1/1000 of Earth's gravity) and prints them over the
  serial connection to the computer.

The circuit:
    - X output of accelerometer to digital pin 2
    - Y output of accelerometer to digital pin 3
    - +V of accelerometer to +5V
    - GND of accelerometer to ground

created 6 Nov 2008
  by David A. Mellis
  modified 30 Aug 2011
  by Tom Igoe

This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/Memsic2125
*/

// these constants won't change:
const int xPin = 2;    // X output of the accelerometer
const int yPin = 3;    // Y output of the accelerometer

void setup() {
  // initialize serial communications:
  Serial.begin(9600);
  // initialize the pins connected to the accelerometer as inputs:
  pinMode(xPin, INPUT);
  pinMode(yPin, INPUT);
}

void loop() {
  // variables to read the pulse widths:
  int pulseX, pulseY;
  // variables to contain the resulting accelerations
  int accelerationX, accelerationY;

// read pulse from x- and y-axes:
  pulseX = pulseIn(xPin, HIGH);
  pulseY = pulseIn(yPin, HIGH);

// convert the pulse width into acceleration
  // accelerationX and accelerationY are in milli-g's:
  // Earth's gravity is 1000 milli-g's, or 1 g.
  accelerationX = ((pulseX / 10) - 500) * 8;
  accelerationY = ((pulseY / 10) - 500) * 8;

// print the acceleration
  Serial.print(accelerationX);
  // print a tab character:
  Serial.print("\t");
  Serial.print(accelerationY);
  Serial.println();

delay(100);
}




I suppose the output code must be fed in here:


// print the acceleration
  Serial.print(accelerationX);
  // print a tab character:
  Serial.print("\t");
  Serial.print(accelerationY);
  Serial.println();

C'mon man.
You state you cant be bothered to learn.... (no time?... thats non-sense... you could have gathered some basic facts in the hours wasted begging for help)

Trying and not understanding is NOT the same as not giving an effort at all.

Most people here will HELP you when you posted an honest attempt.. not just do the work for you for free. If you want that, post in the gigs & collabs section and pay someone.

1.) Never use a 9v battery.. they suck (I know you said you want to use car charger in end.. but until then I hope you get odd results for lack of current..etc)
2.) You dont even bother to tell us or give us a link to the accelerator/module you are using... why? (how are we supposed to know this... and then provide help?)
3.) In general terms.. you have the hardware correct.. (Arduino, Accelerometer, buzzer for feedback)..
But have you even thought how this is applied in the real world?
How is this mounted?
Do the drivers need to wear a hat?
a headband? visor? How is this used by the driver?

4.) Error validation... how do you determine a 'sleeping head' dip.. from a normal, awake, purposeful head 'dip'? (or whatever it is you are trying to use to determine 'fallen asleep'?)

5.) Mounting and orientation (to me) would seem to be crucial? (unless you code things correctly)
To get a basic sketch going...
wire up the accelerometer to the Arduino..... and out put the x, y & z values it returns...to the serial monitor
once you have that.. you can start to play with the accelerometer, tilting and turning, and watching the values on the serial monitor... once you have a vale (threshold) that you believe is a 'point' where you feel that means someone is sleeping....
record the value.. and use that as a conditional check/threshold to trigger the buzzer.

Paul_KD7HB:
Has you research shown how many minutes a 9 volt battery will operate your project?

Paul

maybe he can make the Arduino sleep ?
after-all, it's not driving !

acceleratometer ? won't you get a huge spike upon impact ?
maybe TXT the EMS that you might need help ?

if you get a sensor, put it on your driving cap, and then tilt your head, you would want readings.

if they get out of range, beep the beeper thingie.

it is the slow nod that would indicate the problem, not the jerk of waking up at 70 MPH.

You already have and posted the code..

add a conditional if(){} statement in there.. to check the values of:

accelerationX
accelerationY

No clue how it is mounted.. and if you want/need to check more than one axis? (there are algorithms for this stuff out there too ya know!)
example:

if(accelerationX > 150){
    //I'm sleeping
    Serial.println('target has triggered a sleeping state response');
    //do buzzer
}else{
    //not sleeping, carry on
}