Reading the position of a flex sensor to set a limit

Ok, I want to read the current position of the flex sensor when the button is pressed and use the coordinates as the limit point. So say you physically bend the flex sensor where you want the limit to be and hit the button. It remembers that position and if it reaches that position or beyond then it beeps the buzzer. I almost have it I just don’t know how to assign the coordinates as a string/array?

See Attached diagram (batt. Is 9v and res. Is 10k)

int flexSensorPin = A0;//analog pin 7= tiny2=A1
const int Piezo = 3; //digital pin 3 = tiny0
const int inPin = 2; // Push Button to set current Bend Status
void setup() {
pinMode (Piezo, OUTPUT);
pinMode(inPin, INPUT); // declare pushbutton as input
Serial.begin(9600);
}
void loop() {
int val = 0;
val = digitalRead(inPin)
; int flexSensorReading = analogRead(flexSensorPin);
Serial.println(flexSensorReading);
if (flexSensorReading < 100) {
Serial.println(“Alarm! Set Angle Exceeded”);
digitalWrite(Piezo, HIGH);
delay(100);
digitalWrite(Piezo, LOW);

}
delay(250);
}

The logic sounds simple

start of loop()
  if the button is pressed
    read the flex sensor and save the value to a variable
  end if

  read the flex sensor
  if the value is greater than the saved value
    execute the code here
  end if
end of loop()

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Why do you need an array?
Does your code work?

What is the application?

Thanks… Tom… :slight_smile:

What’s with all the underlining ?

Did you intend for some of the text to be links ?

It's a spammer UKHeliBob. For some reason the forum doesn't show links in the normal blue color when they are in underlined text so it makes it not so obvious that they have snuck a bunch of spam links in with the rest of the text that's harvested from posts on this forum.

Ah, I see. We can normally spot a link because it's underlined and blue. In order to hide the spam links they underlined all the text and used color tags to override the normal link blue color.

pert:
It's a spammer UKHeliBob. For some reason the forum doesn't show links in the normal blue color when they are in underlined text so it makes it not so obvious that they have snuck a bunch of spam links in with the rest of the text that's harvested from posts on this forum.

Actually you can still see the linked text because it is double underlined, but I have no intention of clicking on them.

const int flexSensorPin = A0;//analog pin 7= tiny2=A1
const int PiezoPin = 3; //digital pin 3 = tiny0
const int inPin = 2; // Push Button to set current Bend Status


void setup() {
  pinMode (PiezoPin, OUTPUT);
  pinMode(inPin, INPUT);    // declare pushbutton as input
  Serial.begin(9600);
}
int Limit = 100;  // Made this Global so the value persists.
void loop() {
  int val = digitalRead(inPin);
  int flexSensorReading = analogRead(flexSensorPin);


  if (val)
  {
    // Button is pressed
    Limit = flexSensorReading;
  }


  Serial.println(flexSensorReading);
  if (flexSensorReading < Limit)
  {
    Serial.println("Alarm! Set Angle Exceeded");
    digitalWrite(PiezoPin, HIGH);
    delay(100);
    digitalWrite(PiezoPin, LOW);
  }
}

Been Hacking trying to make this work, I’m hung up on the context of assigning and using integers. At startup I want to read the value of the current position of the flex sensor and use that as a start point. After that the flex sensor will be moved straight again and if flexed to that value or beyond then the buzzer should start. If you want to set a new position then set the flex sensor where you want the limit to be and then press the button and the new parameter is set. This code is hacked as you can see I know I’m just missing something simple… The value in this hacked code is hard set to 100 but thats what I want to make the stored integer.

int flexSensorPin = A0;//analog pin 7= tiny2=A1
// int StartValue = flexSensorPin;
const int Piezo = 3; //digital pin 3 = tiny0
const int inPin = 2; // Push Button to set current Bend Status
void setup() {
  pinMode (Piezo, OUTPUT);
  pinMode(inPin, INPUT);    // declare pushbutton as input
  Serial.begin(9600);
}
void loop() {
  int val = 0;
  val = digitalRead(inPin)
        ;  int flexSensorReading = analogRead(flexSensorPin);
  Serial.println(flexSensorReading);
//  if inPin( HIGH goto Start; 
  if (flexSensorReading < 100) {
    Serial.println("Alarm! Set Angle Exceeded");
    digitalWrite(Piezo, HIGH);
    if(digitalRead(inPin)){
//    LoggedPosition=flexSensorReading;
}
    delay(100);
    digitalWrite(Piezo, LOW);

  }
  delay(250);
}

Hi,
Does you code work as in the post above, to sound buzzer on exceeding threshold?

What do you want to put in the array?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
How have you got your button wired?

Thanks.. Tom.. :slight_smile:

Are you SURE you only want to look at the button when the alarm is sounding?!? You will only be able to set the limit to a value lower than the current limit.

  if (flexSensorReading < 100) {
    Serial.println("Alarm! Set Angle Exceeded");
    digitalWrite(Piezo, HIGH);
    if (digitalRead(inPin)) {
      //    LoggedPosition=flexSensorReading;
    }
    delay(100);
    digitalWrite(Piezo, LOW);
  }

Look at the sketch I posted to see one way to correct this programming error.

TomGeorge:
Hi,
Does you code work as in the post above, to sound buzzer on exceeding threshold?

What do you want to put in the array?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
How have you got your button wired?

Thanks… Tom… :slight_smile:

I want it to set the value of the current position when the button is pressed or when powered up then use that value as the “boundary” point where if that’s met or exceeded then the alarm sounds and continues to sound until the button is pressed to reset the value or the flex sensor is moved to the “safe” position.

I want it to set the value of the current position when the button is pressed or when powered up then use that value as the “boundary” point where if that’s met or exceeded then the alarm sounds and continues to sound until the button is pressed to reset the value or the flex sensor is moved to the “safe” position.

My code is kinda hacked now, not sure things are necessary or even in the right places. Please bear with me, I’m still thinking in basic and q basic. Am new to C++ and as you can see i’m lost. Anyways I have it set to go off greater than 250. That’s the spot where I want the remembered “string” to be the boundary (replaces the 250 with an integer or string). It doesn’t seem like the button even registers at this point.

int flexSensorPin = A0;//analog pin 7= tiny2=A1
int LoggedPosition = flexSensorPin;
const int Piezo = 3; //digital pin 3 = tiny0
const int inPin = 2; // Push Button to set current Bend Status
int val = 0;


void setup() {
  pinMode (Piezo, OUTPUT);
  pinMode(inPin, INPUT);    // declare pushbutton as input
  Serial.begin(9600);
val = digitalRead(inPin);

  
}
void loop() {
  

  int flexSensorReading = analogRead(flexSensorPin);
        ;  
  Serial.println(flexSensorReading);
  if (flexSensorReading > 250) {
    Serial.println("Alarm! Set Angle Exceeded");
    digitalWrite(Piezo, HIGH);
//    if(digitalRead(inPin)){
    LoggedPosition=flexSensorReading;
    delay(100);
    digitalWrite(Piezo, LOW);

  }
  delay(250);
}

Another approach is to use some sort of mode condition.

loop() {
  if we are in limit setting mode
    do the limit setting loop
  else
    do the normal operation loop
}

limit_setting_loop() {
  if the "end of mode" button is pressed (released, whatever)
    set mode to normal operation
  else
    if the 'set upper limit' switch is pressed
      record the upper limit

  etc etc
}

normal_operation_loop() {
  if the "set limits mode" button is pressed (released, whatever)
    set mode to setting limits
  else
    do the normal stuff
}

johnwasser:
Are you SURE you only want to look at the button when the alarm is sounding?!? You will only be able to set the limit to a value lower than the current limit.

  if (flexSensorReading < 100) {

Serial.println(“Alarm! Set Angle Exceeded”);
    digitalWrite(Piezo, HIGH);
    if (digitalRead(inPin)) {
      //    LoggedPosition=flexSensorReading;
    }
    delay(100);
    digitalWrite(Piezo, LOW);
  }





The alarm works using the code above but I changed it to "if (flexSensorReading.> 250)" because the sensor works better in the other direction. What I want is to make the 250 a string or integer thats the current position of the sensor on power up or pressing the button. So, say you bend it 45 degrees and press the button, the flex sensor is released and it monitors for the flex sensor to reach 45 or beyond and starts alarming.. Then you bend it at say 80 degrees and press the button and it does the same but for 80 degrees. 

Look at the sketch I posted to see one way to correct this programming error.

sierratech:
I want it to set the value of the current position when the button is pressed or when powered up then use that value as the "boundary" point where if that's met or exceeded then the alarm sounds and continues to sound until the button is pressed to reset the value or the flex sensor is moved to the "safe" position.

Hi, you don't have to store your threshold value in an array.
If it is a single number then store it as a single variable.
Tom..