I am trying to write a code to read the current position of a flex sensor when a button is pushed and use that data to pulse a piezo. I set it up to read the flex sensor and to alarm when it gets 100 or below, that works! So what I want to do is integrate a button where you push it to set the current state of the flex sensor and then when its moved and is less than the number logged when the button was pressed then it alarms. I used to write in Basic and quick basic and am now (many years later in life) trying to learn C and am struggling. I'm using a NANO ATmega328p - here is my code so far:

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
void loop() {
  int val = 0;
  val = digitalRead(inPin);
  int flexSensorReading = analogRead(flexSensorPin);
  if (flexSensorReading < 100) {
    Serial.println("Alarm! Set Angle Exceeded");
    digitalWrite(Piezo, HIGH);
    digitalWrite(Piezo, LOW);

  • 1
    And what is your specific problem in writing the code? You already read the state of the button pin and the analog voltage of the flex sensor. Use an if statement, where you set a variable to the analog value, if the button was pressed. Then replace the 100 in your first if statement with this variable.
    – chrisl
    May 27 '18 at 11:33
  • 1
    And two other things for when you have done this: For the button to be responsive you should ditch those delay() calls and use non-blocking code (refer to the BlinkWithoutDelay example of the Arduino IDE). And then it might be necessary to debounce your button. For this you can find plenty tutorials on the web.
    – chrisl
    May 27 '18 at 11:35
  • Also depending on your wireup you might want to use INPUT_PULLUP instead of INPUT if you don't have an external pull-up resistor attached to your button input. (arduino.cc/en/Tutorial/InputPullupSerial) May 27 '18 at 12:00
  • Also the TimedAction library (playground.arduino.cc/Code/TimedAction) will help you to periodically flip the piezo output (if an alarm needs to be triggered) every 100ms. You thus get rid of the delay() calls and your button will become more responsive. For button debouncing, you can also use the Button library (playground.arduino.cc/Code/Button). May 27 '18 at 12:04

You will need to add this at the start of your code.

int LoggedPosition=100;

Change this line

if (flexSensorReading < 100) {

To this

if (flexSensorReading < LoggedPosition) {

and add this at just before the delay, at the end of the loop() function

  • I entered that code but it appears to reset the value to a new value every time its flexed so you keep flexiing until the value exceeds the flex sensor. it's like what I want sort of. but not.. 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. May 27 '18 at 23:38
  • I think your button gives a low voltage to the pin, when pressed. It's a bit counter-intuitive, but that is the only thing that could cause this behaviour.You will need to change if(digitalRead(inPin)) to if(digitalRead(inPin)==0). Check if this works.
    – Nitro
    May 28 '18 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.