Dat code :-{

This code is really messing with my head!

//Transmitter (NANO)

#include <VirtualWire.h>

/*
 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 xpin = A3;                  // x-axis of the accelerometer
const int ypin = A2;                  // y-axis
const int zpin = A1;                  // z-axis (only on 3-axis models)
int previous = 1160;
int now = 0;
char c = 1;

void setup()
{
  // initialize the serial communications:
  Serial.begin(9600);
  
  //Virtual Wires
  vw_setup(3000);
  vw_set_tx_pin(7);
  
 //Make sure the readings aren't too big
  int now = xpin + ypin + zpin;
  int threshold = now/2;
}

void loop()
{
  //Read pins and set variables
  int xpin = (analogRead(A0));
  int ypin = (analogRead(A1));
  int zpin = (analogRead(A2));
//Declare the variable in this scope?
  int threshold;
  
  //Addition
  int now = xpin + ypin + zpin;
  
//If Pin3 is high, show debug info
  if(digitalRead(3) == HIGH);
  {
      Serial.println(now);
  }
  
  //The maths
  if(now > (previous - threshold) || now < (previous - threshold));
  {
     vw_send((uint8_t *)c, 1);
     //Send if to me
     Serial.println("Bump");
  }
  
//Set the previous total
  int previous = now;
  delay(200);
  
}

I have an accelerometer and want to check if I it senses an impact. It will then broadcast it via RF waves for my control panel to pick up.

I just get BUMP BUMP BUMP BUMP BUMP etc…

Please help!

Nick

  //The maths
  if(now > (previous - threshold) || now < (previous - threshold));

The only way the if statement can be false is if now == (previous-threshold).
The other problem is the semicolon at the end of the line. Get rid of it.

Pete

Want I want to do is as follows.

If my current reading is different to my previous one by the amount of my my threshold

The difference is:

current-previous

This may be positive or negative depending whether the value is increasing or decreasing.

The absolute difference is:

abs(current-previous)

This exceeds the threshold when:

abs(current-previous) > threshold

Thanks!

Also, my combined sensor range from x + y + z is over 10000 How would I shrink this down to around 1000?

Also, is this no right:

//Transmitter (NANO)

#include <VirtualWire.h>

/*
 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 xpin = A3;                  // x-axis of the accelerometer
const int ypin = A2;                  // y-axis
const int zpin = A1;                  // z-axis (only on 3-axis models)
int previous = 1160;
int now = 0;
char c = 1;

void setup()
{
  // initialize the serial communications:
  Serial.begin(9600);
  
  //Stuff
  vw_setup(3000);
  vw_set_tx_pin(7);

}

void loop()
{
  //Read pins and set variables
  int xpin = (analogRead(A0));
  int ypin = (analogRead(A1));
  int zpin = (analogRead(A2));
  
  //Addition
  int threshold = 400;
  int now = (xpin + ypin + zpin) - 1000;
  
  delay(10);

  //The maths
  if((now-previous) > threshold)
  {
     vw_send((uint8_t *)c, 1);
     Serial.println("Bump");
     Serial.println(abs(now-previous));
  }
  
  
  int previous = now;
  delay(200);
  
}

Also, my combined sensor range from x + y + z is over 10000 How would I shrink this down to around 1000?

÷10?

Good point. I was thinking about floats and all that complicated stuff!