accelerometer ADXL335

I have interfaced an ADXL335 3-axis accelerometer with an atmega8 microcontroller.. what i would like to know is how can i use the analog output of the accelerometer to interrupt the microcontroller whenever the outputs deviate from their mean value? any kind of help will be greatly appreciated.... thanks :slight_smile:

(no electrical guru)

If you want an interrupt you must add some hardware as analog ports cannot generate an IRQ on some threshold value.

Do you have a real time constraint in your application?
Maybe it is a good Idea to post your code so we can have a look at it how it can be done.

Do you use delay() in your code? Code can be more reactive if it is written without delays'...

yea... there is a real time constraint.... basically we are building a window washer.... and using the accelerometer as an anemometer so that whenever the bot which is suspended from the top starts swaying, it needs to be interrupted...

here is the code for the accelerometer alone, i have been polling the status of the outputs of accelerometer to get the desired result...

int x, y, z;

void setup()
{
Serial.begin(9600); // sets the serial port to 9600
}
void loop()
{
x = analogRead(0); // read analog input pin 0
y = analogRead(1); // read analog input pin 1
z = analogRead(2); // read analog input pin 2
Serial.print("accelerations are x, y, z: ");
Serial.print(x, DEC); // print the acceleration in the X axis
Serial.print(" "); // prints a space between the numbers
Serial.print(y, DEC); // print the acceleration in the Y axis
Serial.print(" "); // prints a space between the numbers
Serial.println(z, DEC); // print the acceleration in the Z axis
delay(1000);// wait 100ms for next reading
if (x<375||x>390 && y<375||y>390 && z<310||z>325)
{
digitalWrite(13,HIGH);
}
else
{
digitalWrite(13,LOW);
}

}

PLease use the # button to get code tags , makes at least the look better.

some observations:

  • why not use higher baudrate 115200 is 12 times faster

  • if code and comments differ the code wins ==> check the delay(1000) in your code

  • you need to add extra ()'s in the boolean statement to enforce what you really want (I guess)
    see my change in the code below.
    => google for short circuit boolean evaluation why you should do that. (operator precedence is also a good reader)

int x, y, z;

void setup()
{
	Serial.begin(115200);    <<<<<<<<<<<<<<<<<<<<
}
void loop()
{
	x = analogRead(0);       // read analog input pin 0
	y = analogRead(1);       // read analog input pin 1
	z = analogRead(2);       // read analog input pin 2
	
	Serial.print("accelerations are x, y, z: ");
	Serial.print(x, DEC);    // print the acceleration in the X axis
	Serial.print(" ");       // prints a space between the numbers
	Serial.print(y, DEC);    // print the acceleration in the Y axis
	Serial.print(" ");       // prints a space between the numbers
	Serial.println(z, DEC);  // print the acceleration in the Z axis
	
	delay(1000);// wait 100ms for next reading <<<<<<<<<<<<<<<<<<
	
	
	if ((x<375||x>390) && (y<375||y>390) && (z<310||z>325))  <<<<<<<<<<<<<<<<
	{
		digitalWrite(13,HIGH);
	}
	else
	{
		digitalWrite(13,LOW);
	}
}

well actually the code was originally written by a friend... i just tweaked it a little and forgot to change the comments :stuck_out_tongue: and as far as using the brackets... the code works perfectly fine without using them also