Creating a buzzer alarm when accelometer ( ADXL 335 )is moved by someone.

Hi, I am trying to make an alarm by buzzer whenever accelometer ADXL335 moves. I am using arduino UNO … connections are done, but the problem is in code as I have no background… . i tried to merge the buzzer code and the ADXL 335 code and put a condition if ( Xval >= 50 || Yval <= 50 | Zval <= 50 ) , buzzer shall alarm on … there is an error in the code but i dot know what is it… please help :slight_smile: :

/*
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

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);

// For Buzzer.
pinMode(buzzer, OUTPUT); // Set buzzer - pin 9 as an output
}

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);

// For Buzzer
if ( Xval >= 50 || Yval <= 50 | Zval <= 50 ) {
tone(buzzer, 1000); // Send 1KHz sound signal…
delay(100); // …for 1 sec
noTone(buzzer); // Stop sound…
delay(100); // …for 1sec
}
}

Welcome to the forums. You can start by reading the sticky post at the top of this forum about how to properly post your code using code tags. It helps people help you. The easier you make it, the more likely help will be. Please read it, go back and edit your original post and include code tags around your code.

It would also help if you posted the error you are receiving. There is a button on the IDE that copies the entire error message, ready for posting in the forums. Go do that also.