Want to write a code which answers the following qn:
Read the value (name the variable flexValue) of a “flex sensor” that is
connected to analog input AN0 of the Arduino. The value will be a 10-bit
integer number having a value between 0 and 1023 inclusive, although flexing
the sensor strip might not give values that cover the full range.
MY CODE:
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int flexValue = analogRead(A0);
// print out the value you read:
Serial.print(flexValue);
delay(1); // delay in between reads for stability
}
Is this correct?
Thanks.
delay (1) = 1 millisecond
try 250-1000 for a 1/4 - 1 second delay
else it looks ok
you could add a vairable for the analog read, to make easier changes to the program
oh i see.
Thank you!
Does this look better and answer the qn better?
// assign variable flexValue value of analog input AN0
flexValue = analogRead(A0);
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int flexValue = analogRead(A0);
// print out the value you read:
Serial.print(flexValue);
delay(1000); // delay in between reads for stability
}
If what u need is reading an int number as u said the code should be ok.
But if u want to estimate the bend angle and the resistance, this code should help u
/******************************************************************************
Create a voltage divider circuit combining a flex sensor with a 47k resistor.
- The resistor should connect from A0 to GND.
- The flex sensor should connect from A0 to 3.3V
As the resistance of the flex sensor increases (meaning it's being bent), the
voltage at A0 should decrease.
Development environment specifics:
Arduino 1.6.7
******************************************************************************/
const int FLEX_PIN = A0; // Pin connected to voltage divider output
// Measure the voltage at 5V and the actual resistance of your
// 47k resistor, and enter them below:
const float VCC = 4.98; // Measured voltage of Ardunio 5V line
const float R_DIV = 47500.0; // Measured resistance of 3.3k resistor
// Upload the code, then try to adjust these values to more
// accurately calculate bend degree.
const float STRAIGHT_RESISTANCE = 37300.0; // resistance when straight
const float BEND_RESISTANCE = 90000.0; // resistance at 90 deg
void setup()
{
Serial.begin(9600);
pinMode(FLEX_PIN, INPUT);
}
void loop()
{
// Read the ADC, and calculate voltage and resistance from it
int flexADC = analogRead(FLEX_PIN);
float flexV = flexADC * VCC / 1023.0;
float flexR = R_DIV * (VCC / flexV - 1.0);
Serial.println("Resistance: " + String(flexR) + " ohms");
// Use the calculated resistance to estimate the sensor's
// bend angle:
float angle = map(flexR, STRAIGHT_RESISTANCE, BEND_RESISTANCE,
0, 90.0);
Serial.println("Bend: " + String(angle) + " degrees");
Serial.println();
delay(500);
}
Note:
The code is taken from sparkfun website as it says, u could look it up under : flex sensor hookup guide.
try this if you just want to read the values from the pin
int flexPin = A0;
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
pinmode (flexPin, INPUT);
}
void loop() {
// read the input on analog pin and print out the value
Serial.print( analogRead(flexPin) );
delay(1000); // delay in between reads for stability
}