I want to modify my code to move a servo motor only for the duration of time that the sensor is touched and stop moving immediately when the sensor is no longer touched. The code I have moves the sensor from 0 to 90 and back. That motion is completed even when you remove your finger. I want it to be such that if you remove your finger before the servo reaches 90 degrees the servo stops. And the servo starts up again toward 90 when touch resumes. This is the code I currently have:

//when signal output is high the sensor is being pressed

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
int pos = 0;    // variable to store the servo position
int sensor=11; //initialises component to arduino digital pins

void setup() {

void loop() {

        int value=digitalRead(sensor);
        if(value == HIGH){
                digitalWrite(led, HIGH);
                for (pos = 0; pos <= 90; pos += 1) {
                        myservo.write(pos);              // tell servo to go to position in 
                        variable 'pos'
                                delay(15);                       // waits 15ms for the servo to reach the 
                for (pos = 90; pos >= 0; pos -= 1) { // goes from 90 degrees to 0 degrees
                        myservo.write(pos);              // tell servo to go to position in 
                        variable 'pos'
                Serial.println("TOUCHED ");
                digitalWrite(led, LOW);

                Serial.println("NOT TOUCHED ");
  • 1
    You could test the button in the for loop, while the servo is moving. Bail out of the for loop with 'break' if the button is released. When you do that and add more things to the sketch, the code becomes harder to maintain. Another option is rewriting your sketch and use millis, start with the example: arduino.cc/en/Tutorial/BlinkWithoutDelay – Jot Oct 20 '18 at 10:13
  • Leonardo, what I recommend you do is non-blocking "cooperative multitasking". Start with the "Blink Without Delay" tutorial @Jot pointed you to. I've written a few more-advanced examples here: stackoverflow.com/questions/50028821/… and here: arduino.stackexchange.com/questions/75932/…. You can study my code to see how I do it. Side note: this is the start of building a scheduler, which results eventually in a basic operating system. – Gabriel Staples Aug 10 '20 at 23:39

To solve your problem, you need to take a tiny step into real time programming. Most Arduino programs deal with one issue or action at a time. Your simple twist to interrupt and action "i want it to be such that if you remove your finger before the servo reaches 90 degrees that's where the servo stops" requires you to continuously monitor your input (touch switch) while changing your output (next servo position).

You can go about this 2 ways. You can use interrupts to inform you when the state of the touch switch changes. This is challenging. Many find it difficult to conceive of what might happen if their program were to be interrupted at any and all possible points in its execution. A mistake here can lead to unexpected results.

Or you could take turns polling the state of the touch switch then making a small adjustment in the position of the servo. When the state of the touch switch changes from active to not-active, exit the loop you are using to change the servo position.

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.