Senior Design Project Help

Thread Starter

ripcus10

Joined Apr 13, 2012
56
Hey everyone I'm new to this site and new to the world of microcontrollers. My group is doing a senior design project in which we are using an accelerameter to measure the g-forces during a crash and when they exceed a certain limit will trip a relay. I talked to the customer support staff at robot shop and they told me the hardware that i would need to buy. I bought the following components:

http://www.robotshop.com/dfrobot-tri...-mma7260q.html
http://www.robotshop.com/productinfo...-36&lang=en-US
http://www.robotshop.com/dfrobot-single-relay.html

I have received them and hooked them up but am running into a bit of trouble when it comes to the programming. I have taken some programming courses but not enough to program the microcontroller to do what we need it to. I was wondering if someone could point me in a direction where I might be able to find a sample program to use. Thanks in advance to the help!
 

MrChips

Joined Oct 2, 2009
30,824
What is the school level of this course?

Here is my sincere advice.

If you would like to become an expert embedded systems designer one of these days, there is no short cut.

Rather than use someone else's code, learn to write your own.

If you are starting out with microcontrollers, eventually you may choose to program entirely in a high level language such as C. But do yourself a favour and learn ASM first.
 

Thread Starter

ripcus10

Joined Apr 13, 2012
56
This is a 400 level course. The thing is that I am a MET student and haven't had many courses that deal with things like this. My group and I drew up the idea and after doing some research we purchased the hardware that I listed above. I don't have a lot of time to learn everything there is to programming these controllers and am trying to find some help. I'm not trying to short cut things I'm trying to make sure I'm heading in the right direction.
 

chrisw1990

Joined Oct 22, 2011
551
hi sorry, your first two links are broken.. for me anyway
can you tell more about this? what processor? do you have schematic?
do you have aims and objectives?
pictures? youve clearly got a set up, let us see it..:D
 

Thread Starter

ripcus10

Joined Apr 13, 2012
56
Sorry about that I didn't realize they didn't work. As of right now I have the three plugs from the accelerameter plugged into slots 1, 2 and 3 on the analog side. I believe that I have the accelerameter set up to be reading 6g which is it's highest setting. I then have the relay plugged into slot 0 on the digital side. Here are the working links for the accelerameter and microcontroller.

http://www.robotshop.com/dfrobot-triple-axis-accelerometer-breakout-board-mma7260q.html
http://www.robotshop.com/productinfo.aspx?pc=RB-Dfr-36&lang=en-US

I'm wanting the microcontroller to trip the relay if the accelerameter exceeds say 4g's. I'm guessing that there is going to have to be a if statement embedded inside of a loop? The biggest part that I'm unsure of is how to read in the analog signal for the accelerameter and how to trip the relay when the accelerameter exceeds the set level. Also i have attached a picture of my set up as well.

http://www.flickr.com/photos/79051833@N03/6930681392/

Thanks for all your help!
 

chrisw1990

Joined Oct 22, 2011
551
youll have to enable the ADC.. analogue to digital converter.. and then keep polling the input.. is there a particular axis you had in mind? like when it exceeds 4g in the Y axis?
if nothing like that is needed.. then effectively,
you need to configure it for 4, or 6g.. (see datasheet) and then you need to compare your adc result to your expected.. the outputs hover at 1.65V typ., and for 4g are 300mV/g, so at 4g you should expect approx 2.85V.. convert that into digital.. (10bit adc at 3.3v is 0.03mV/bit) then just do an if(ADRESULT=> 2.85V) then do some stuff because its over 4g..
make sense?
sorta rambled a bit lol
 

Thread Starter

ripcus10

Joined Apr 13, 2012
56
Yeah that makes sense. I don't need it to read in any particular axis I would actually prefer it to read all axis at the same time.

Here is a sample code that was provided with the microcontroller:
/*
ADXL3xx

Reads an Analog Devices ADXL3xx accelerometer and communicates the
acceleration to the computer. The pins used are designed to be easily
compatible with the breakout boards from Sparkfun, available from:
http://www.sparkfun.com/commerce/categories.php?c=80

http://www.arduino.cc/en/Tutorial/ADXL3xx

The circuit:
analog 0: accelerometer self test
analog 1: z-axis
analog 2: y-axis
analog 3: x-axis
analog 4: ground
analog 5: vcc

created 2 Jul 2008
by David A. Mellis
modified 30 Aug 2011
by Tom Igoe

This example code is in the public domain.

*/

// these constants describe the pins. They won't change:
const int groundpin = 18; // analog input pin 4 -- ground
const int powerpin = 19; // analog input pin 5 -- voltage
const int xpin = A3; // x-axis of the accelerometer
const int ypin = A2; // y-axis
const int zpin = A1; // z-axis (only on 3-axis models)

void setup()
{
// initialize the serial communications:
Serial.begin(9600);

// Provide ground and power by using the analog inputs as normal
// digital pins. This makes it possible to directly connect the
// breakout board to the Arduino. If you use the normal 5V and
// GND pins on the Arduino, you can remove these lines.
pinMode(groundpin, OUTPUT);
pinMode(powerpin, OUTPUT);
digitalWrite(groundpin, LOW);
digitalWrite(powerpin, HIGH);
}

void loop()
{
// print the sensor values:
Serial.print(analogRead(xpin));
// print a tab between values:
Serial.print("\t");
Serial.print(analogRead(ypin));
// print a tab between values:
Serial.print("\t");
Serial.print(analogRead(zpin));
Serial.println();
// delay before next reading:
delay(100);
}

I know that it is reading the inputs of the accelerameter and printing them out. So if i understand what you were saying above I should replace the printing command with the IF() statement for each axis and then have it trigger the relay if it goes over 4g's, right? How do I point the microcontroller to the correct slot to send the digital signal to?
 

Thread Starter

ripcus10

Joined Apr 13, 2012
56
I apologize if these questions are beginner level or seem stupid. I believe the microcontroller is programmed in Java which I only took one semester of and we didn't come close to doing stuff like this. Thanks again for all your help!!
 

chrisw1990

Joined Oct 22, 2011
551
ok, first thing, use code tags, makes it so much more readable, thats the little # at the top of the reply box..

yeh.. replace this:
Rich (BB code):
// print the sensor values:
Serial.print(analogRead(xpin));
// print a tab between values:
Serial.print("\t");
Serial.print(analogRead(ypin));
// print a tab between values:
Serial.print("\t");
Serial.print(analogRead(zpin));
Serial.println();
with
Rich (BB code):
if(analogRead(xpin)=>0bxxxxxxxx) //whatever 2.85V is in binary, depends if your using 8or10-bit adc.
{ do your desired code/function etc; }
else if(analogRead(xpin)<=0bxxxxxxxx) //2.85V but minus (can go 4g in either direction)
{do its business; }
if(analogRead(ypin)=>... etc.
oh you dont need that serial data stuff either.. unless you want to include that too?
by the way, 4g is a lot.. do you actually need 4g? or is 2g a more realistic trigger point?
 

Thread Starter

ripcus10

Joined Apr 13, 2012
56
Ok if i understood what you were saying correctly i believe this should be correct?

Rich (BB code):
/*
 
 The circuit:
 analog 0: accelerometer self test
 analog 1: z-axis
 analog 2: y-axis
 analog 3: x-axis
 analog 4: ground
 analog 5: vcc
*/

// these constants describe the pins. They won't change:
const int groundpin = 18;             // analog input pin 4 -- ground
const int powerpin = 19;              // analog input pin 5 -- voltage
const int xpin = A3;                  // x-axis of the accelerometer
const int ypin = A2;                  // y-axis
const int zpin = A1;                  // z-axis (only on 3-axis models)
const int relay = D0;                 // Output for the relay

void loop()
{
  if(analogRead(xpin)=>00110010 00101110 00111000 00110101); 
  { do relay = 1 };
  else if(analogRead(xpin)<=-00110010 00101110 00111000 00110101); 
  {do relay = 1 };
  if(analogRead(ypin)=>00110010 00101110 00111000 00110101);
  (do relay = 1);
  else if(analogRead(ypin)<=-00110010 00101110 00111000 00110101);
  (do relay = 1);
  if (analogRead(zpin)=>00110010 00101110 00111000 00110101);
  (do relay = 1);
  else if(analogRead(zpin)<=00110010 00101110 00111000 00110101);
  (do relay = 1);
  // delay before next reading:
  delay(100);
  relay = 0;
}
As far as the 4g's go I need to do a little bit more research. We want something that will trigger in the event of a motorcycle crash but wont trigger if the rider takes off real hard or stops real fast. Thanks again for all your help. My head was spinning trying to figure all this stuff out
 

chrisw1990

Joined Oct 22, 2011
551
Rich (BB code):
else if(analogRead(xpin)<=-00110010 00101110 00111000 00110101); 
  {do relay = 1 };
almost, but i bet it uses 2's complement which means itll be 10110010 etc. also .... its a 32bit ADC????
yeh no worries, thats what were here for.. im developing something sorta similar as we speak.. slightly more of a nuisance though! lot more inputs and comms! uses micro sd card n usb.. and gps.. anyway.. point is, your binary comparisons wont be liked because of the spaces, i dont know why you have a word though? like i say, are you using 32bit adc?
in addition, your last zpin isnt - like the rest, perhaps a different way would be using a decimal value? say you want below 1v, and this is represented by 30decimal.. you could just <=30) depending on what your compiler accepts..
ALSO after thought, you wouldnt be having negative values!!!!!
your voltage would always be positive!! just be above or below the idle 1.65V!:D
again.. hope rambling makes sense here
 

Thread Starter

ripcus10

Joined Apr 13, 2012
56
I tried using a site that converted from decimal to binary and that was what it gave me. I have since then converted decimal to binary correctly. Here is what i have so far:

Rich (BB code):
void loop()
{
  if(analogRead(xpin)=>00000011); 
  { do relay = 1 };
  else if(analogRead(xpin)<=00000000); 
  {do relay = 1 };
  if(analogRead(ypin)=>00000011);
  (do relay = 1);
  else if(analogRead(ypin)<=00000000);
  (do relay = 1);
  if (analogRead(zpin)=>00000011);
  (do relay = 1);
  else if(analogRead(zpin)<=00000000);
  (do relay = 1);
  // delay before next reading:
  delay(100);
  relay = 0;
Are my signals to the relay correct or should i be doing something different to trigger it?

Holy cow your project sounds a lot more difficult then what I'm doing. I would have to say that I have a lot more respect for all of this now after taking on this part of the project.
 

chrisw1990

Joined Oct 22, 2011
551
haha its easier the more you do! less looking stuff up, and if you want to get into it, make code portable.. i.e. functions which do more general things (say you set up an spi channel, you pass it which channel and the speed and it does the rest in the function..) anyway i digress..
the main problem i can see is this offset, what you will find, when you plug the device in, is that your voltage when its not moving will be 1.65v, ish anyway.. and your device will add or subtract the relevant mv/g (300mv/g when at 4g sensitivity). so a movement in the +axis direction will increase the voltage, and the opposite, decrease..
therefore all your thresholds will have to be say 127(decimal) +/- your selected values...
make sense?
 

Thread Starter

ripcus10

Joined Apr 13, 2012
56
Where did you get the 127(decimal) from? Is that the value of 1.65V? If that is correct and I am understanding everything i would then have 1327(decimal) as my high limit and -1073(decimal) as my low limit?
 

chrisw1990

Joined Oct 22, 2011
551
no no, im assuming, because its 8bits, the maximum value is 255, and the lowest is 0, therefore, your halfway idle voltage is in themiddle, hence 127 decimal.. so yeh the approximate value of 1.65v
 

Thread Starter

ripcus10

Joined Apr 13, 2012
56
Ok that makes sence. So i would set my middle point at say 127(decimal) and if i use 2.85V as my high limit it would be a difference of 105(decimal) resulting in 233(decimal) for the high limit and 22(decimal) for the low limit. Does that sound about right?
 

chrisw1990

Joined Oct 22, 2011
551
well 1 bit is equivalent to 0.012mV (Vsupply/2^8) so i make that 100, and therefore 227, but yeh you got it:) and then in the opposite direction, 27, so yes, you got it!:D
so your thing would be comparing the 27(or 22)<adcresult(xaxis)<227(or 233).
but in the if statements discussed earlier.
ALSO as a thought, dont know what your controlling with the relays, but it might be a good idea to put else statements in, then, for whatever reason, if your adresult doesnt fit into your two compares.. have an else {relay=0;} that way, you know it will be on with your criteria or have a default off if for whatever reason it isnt. :)
 

Thread Starter

ripcus10

Joined Apr 13, 2012
56
Well I didn't realize that they converter I was using was in 8-bit instead of 10-bit. The manual for the microcontroller that i have says that the analog is 10-bit. With that being said does the same still apply? Would the middle point be 512 instead of 127 and would it still be +/- 100 for the high and low limit? Sorry i didn't catch that before i know it messing everything we just talked about up.
 

chrisw1990

Joined Oct 22, 2011
551
yeh 512 'cos full 10bit is 1024 or 1023 and dont worry about it...
it would be different (the +/-100 bit) because 3.3v/1024 is 0.0032mV/bit.. therefore 1.65>2.85V is 1.2/0.0032 = 375 so youd basically want +/-375
 
Top