The difference is returning a value which is very large

I have an accelerometer which I want to use to see if there was an impact. The way I am doing this is by adding up all the data from all of the sensors and then finding the difference. If the difference is too large since the last recorded one then it sends a wireless message to the control panel. However, the difference is giving a result of around 1046 when it should be giving a difference of about 5 or less as it is on a flat surface.

//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 = 60;
  int now = (xpin + ypin + zpin) / 10;
  Serial.println((previous-now));

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

I really don’t understand why this won’t work and have spent 6 hours trying to figure it out!

Nick

Try:

vw_send((uint8_t *) &c, sizeof(c));

Cheers!

There is a function that will convert your values to a char array (string), this useful function is sprintf(). Look it up and see how it works, then adapt it in your code.

Check to make sure you want it this way:

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)

but then you use:

  int xpin = (analogRead(A0));
  int ypin = (analogRead(A1));
  int zpin = (analogRead(A2));

in your code. You’re reading different pins than you’ve defined. Even though you have globals that don’t have the same names, the pin definitions inside of loop() take precedence over the global definitions. Is this what you really wanted to do? Also, does vw_send() really expect a pointer to be sent to it as a parameter?

  delay(10);
  int previous = now;
  delay(50);

Why are you redeclaring "previous" and then throwing it a way when you reach the end of loop! Drop the int

Mark

Mark - you solved it!

Karma coming your way!

Nick