# Interfacing Flex sensors and accelerometer ADXL335 with ATMEGA328P

I am interfacing two flex sensors and an accelerometer (ADXL335) to an ATMEGA328P microcontroller. I’m using arduino code on Arduino Uno to program the ATMEGA328P. I will include parts of the code i am having problems understanding (sensor readings).
so in my setup() function, i check the input value of the sensors by analogRead() at stability.

``````#include<SoftwareSerial.h>
void setup()
{
x_val = checkstabiliy(A2);
y_val = checkstabiliy(A3);
FLEX_VAL = checkstabiliy(A0);
FLEX_VAL1 = checkstabiliy(A1); // this function returns value reading of analogRead(pin) when stable.
}// end of setup()

void loop()
{
x = acc_x - x_val;
y = acc_y - y_val;
f = f_val - FLEX_VAL;
f1 = f_val1 - FLEX_VAL1;

if (((f1 < -40 ) || (f1 > 40)) && ((f < -40 ) || (f > 40)))
{
send_data += '1'; //if both flex sensors are bent it should add 1 to this string variable
}
while ( (x < -30) || (x > 30) ||  (y < -30) || (y > 30))
{
x = acc_x - x_val;
y = acc_y - y_val;
}
} // end of loop()
``````

So what I understand is that we should find the difference in readings from stability and the value when the sensor is changing position during loop() function.
what i dont understand is the condition of the if statement.
-why should we use negative and positive numbers?
-why are we using 30 for accelerometer and 40 for flex sensors?

Your help would be greatly appreciated!

Well, I guess it is like this. This in setup() determines the analog value of the sensor when it is in the unflexed position:

``````FLEX_VAL = checkstabiliy(A0);
``````

say it has the value 500.

Now in the loop() you keep checking the new analog value from the flex sensor. If the reading is now 600, the sensor has moved 100 units in the positive direction (600 - 500 = 100). If however, the sensor reading is now 400, it means the sensor has moved 100 units in the opposite direction (400 - 500 = -100). Hence the negative numbers.

40 and 30 are simply threshold values and small movement below these values are discarded. Different types of sensors have different threshold values.

The if statements could have been a little simpler if the abs() function was used.

So since they are flex sensors, and since the starting position (stable value) is when they are straight, and then the reading is taken again when the finger is bent (flex is now at 90 degrees)...
since the voltage when stable is larger than when the flex is bent, should the only situation be is that the difference between the two values negative?
in which situation would it be that the difference is positive if its only finding the change from stability to bending position?

Post a link to the flex sensors you are using.
It may be that once a “stable” value has been determined during setup, that the sensors drift below that value even if they are not physically moved, and hence the need to handle negative values.

It is the Flex Sensor by Spectra Symbol 4.5 inches

@li16, do not cross-post. Thread locked.