adxl code

I am new to both coding and arduino lessons so i needed a help in the code.

The aim is to take the readings from ADXL335 and after caliberating compare the simultaneous values. according to the tutorials and data from forums i wrote a code but their are still some issues. please guide me.

float zpin = A3;

float zeroz = 367.5;

float scalez =.330;

int greenled = 13;
int redled2 =8;
int redled1 = 9;
int redled3 =10;

int c=0;
int prev;

void setup() {

Serial.begin(9600);
pinMode (zpin, INPUT);// put your setup code here, to run once:
pinMode (redled2, OUTPUT);
pinMode (redled1, OUTPUT);
pinMode (redled3, OUTPUT);
pinMode (greenled, OUTPUT);
}

void loop() {
prev=c;
float z = analogRead(zpin);

c =Serial.print ((z - (zeroz)) / (scalez));
float deltag=c-prev;
Serial.print(“previous”);
Serial.print(prev);
Serial.print(“difference”);
Serial.print(deltag);
Serial.print ("\n");
delay(1000);

}
this is my code
the output comes in this manner as shown in the attachment. i have neglected the space. i just wanted to know why my variable “prev” is not taking the new value of “c”.

Pin numbers should not be floats.

The value returned by a Serial.print() is the number of bytes printed, probably not what you wanted.

Steve

when i mentioned pin number in integer it only gave a whole number since i also wanted decimal so i used float. i also have tried with int but float works better.
but can u please help me with the reason why its not giving a proper difference and why is it not taking the previous values?

Pin numbers are integers. So "float zpin = A3" makes no sense. Also the result of an analogRead() is an integer (0-1023) so assigning that to a float z makes no sense.

You don't explain what the meaning of c and prev are supposed be so I can't really follow your code.

c =Serial.print ((z - (zeroz)) / (scalez));

Can you explain what you think that line is doing? Again it makes no real sense.

Perhaps try assigning the value of the calculation to c first and then, in a separate command, printing it.

Steve

Steve

thanks for replying. for the analog pin statement i will change it thanks for the guidance.

as for c and prev. i am taking set of values which are identified by variable c(how its calculated i will mention at last) prev is a variable i wanted to define for the previous reading of c like if the values are
4
5
6
8
7
then for the first cycle value of c is 4 measured and the value of prev is 0 initially defined, as for second reading c takes a value of 5 meanwhile prev takes a value of 4 and so on.

and for the area of code u mentioned
c =Serial.print ((z - (zeroz)) / (scalez));
its a caliberation formulae i came with, off which zeroz is a variable i measured when the accelerometer was pointing up and down and took the mean of it. As for scalez is a constand from the datasheet to convert voltage into terms of accleration due to gravity. In short its just a caliberation formulae which provides output in mm/sec^2.

SWAP287:
and for the area of code u mentioned
c =Serial.print ((z - (zeroz)) / (scalez));
its a caliberation formulae i came with, off which zeroz is a variable i measured when the accelerometer was pointing up and down and took the mean of it. As for scalez is a constand from the datasheet to convert voltage into terms of accleration due to gravity. In short its just a caliberation formulae which provides output in mm/sec^2.

That is where you are not understanding. That line of code does NOT set c equal to the result of the formula. As I already said, what is returned from a Serial.print is not the value that was printed it is just the number of bytes that were written to the output. See Serial.print reference You can't do the calculation and the printing in one command like that.

Change that line to these two lines

c = (z - zeroz) / scalez;
Serial.print(c);

See if that does what you want. You might still have a problem because you are mixing up float variables and int variables but the results may be near enough for your purposes.

Steve

Steve

i changed the line and thanks to that now its working and thanks for the advice also. Can you also tell me about how to perform fourier transform on this readings.

No. I can’t even imagine why you might want to apply Fourier transforms to that sort of data.

Steve