I NEED HELP FOR FLEX SENSOR READ

Hi everyone, Im using Arduino Pro and I need help with codes, specifically for the calibration of Flex Sensors.

I have connected one end of the flex sensor with a 5Kohm resistor to Analog Input 1 which is connected to GND

and the other end of the flex sensor to VCC. (refer to diagram - http://arduino.cc/en/uploads/Tutorial/c … on_sch.png (in this case, they used a Photocell)

I intend to Serial.print Logic 1 when I bend the Flex Sensor

and Logic 0 when it’s not bend.

Thing is, I always get a display of 0, even when I bend it.

Please help me, I need help.

HELP HELP HELP.

Here’s the code, you’re welcome to give me any feedback.

THANKYOU!

const int sensorPin = A1;

int sensorValue = 0;
int sensorTreshold = 160;
int sensorMax = 0;
int sensorLogic = LOW;

void setup() {
 
  Serial.begin(9600);
  pinMode(sensorLogic, INPUT);
  
  while(millis() < 1000) {
    sensorValue = analogRead(sensorPin);
    
    if (sensorValue > sensorTreshold) {
      sensorLogic = LOW; // bend, logic 1
    }
    
    if (sensorValue < sensorTreshold) {
      sensorLogic = HIGH; // not bend, logic 0
    }
  }
}
void loop() {
  // read the sensor:
  sensorValue = analogRead(sensorPin);
  
  //apply the calibration to the sensor reading
  sensorValue = map(sensorValue, sensorTreshold, sensorMax, 0 , 255);
  
  // in case the sensor value is outside the range seen during calibration
  sensorValue = constrain(sensorValue, 0, 255);
  
  // print the results: 
  Serial.println(sensorLogic, DEC);
  delay(1000);
}

Kayla,

As I often do, I suggest a divide and conquer approach.

First, I suggest you make sure your analog circuit is working the way you expect. I suggest you connect, in series: a 5 Vdc supply, the flex sensor, your 4 kΩ resistor and ground. (That is, the end of the resistor that is not connected to the sensor gets connected to the ground of the 5Vdc power supply.)

Connect a voltmeter between that same ground and the connection between the sensor and the resistor. Measure the voltage with various amounts of flexing. That should tell you what the analog input (AI) pin of your Arduino will see. I don’t know what the flexed and unflexed resistances of your sensor are, so I can’t predict the values.

Once you have that circuit giving you reasonable results, go back to getting the Arduino to read it for you.

I haven’t tried to run or debug your code, but here are a few things I noticed:

  • - You assign “A1” as the input pin number, but “A1” isn’t a number. I believe you need “1” (without the quotation marks).
  • - Your pinMode command appears (assuming that the LOW is interpretted to be zero) to be setting pin 0, not your sensor pin.
  • - I don't understand why you have a loop reading your sensor in "setup".
  • - From where did you get "sensorTreshold = 160"?
  • - The value you send to the display is one that is determined in "setup". I don't see anything in "loop" that can change it.
  • - I don't follow the logic of your map(). You're mapping the AI value, on a range of 160 to 0 into the range of 0 to 255. Why?
  • I suggest you start with a much simpler approach. Have the Arduino read the sensor and report the raw value from the analog to digital (AtoD) conversion. Once that works, you should find getting data manipulation working to be much less frustrating.

    Have Fun,

    Eric

    Hi Eric,

    Thankyou for the reply. (Y)

    I’ve solved the entire problem by placing the if statement into the loop.

    Thanks again :smiley: