Is there a way to toggle Pressure Sensor to control Servo Motor

Hi, newbie here
I want to toggle my pressure sensor so that if pressure is applied to the sensor and released, the servo motor rotates to 0 degrees. If the same action is done again, the servo motor rotates back to 90 degrees.
Here is my code as of now

#include <Servo.h>
Servo servo;
int reading; 

void setup(void) {
 Serial.begin(9600);   
 servo.attach(9); 
 servo.write(90); 

}
 
void loop(void) {
  reading = analogRead(A0); 
  int value = map(reading, 0, 1023, 0, 180);
  if (value < 10){
    servo.write(90);
    delay(20);
  } else {
    servo.write(0);  
    delay(20);
  }
  Serial.print("Sensor value = ");
  Serial.println(reading);
  delay(100); 
}

Welcome to the forum

You need to detect when the input becomes HIGH or LOW rather than when it is HIGH or LOW
See the StateChangeDetection example in the IDE

Alright thank you, I'll check it out

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.