Stop servo with a push button

Hello,

I’ve got a program that uses three push buttons, every push button does a different thing to control a servo motor.

When the first button is pressed, the servo motor moves from position 0 to 180 and goes back to 0 every two seconds.
When the second button is pressed, I can control the servo with a potentiometer and when the third button is pressed, I control it with a force sensitive resistor.

Everything works pretty much ok, but when I’m on the first mode, I need to keep pressing another button until it changes to another mode,I know that’s because there’s a delay of 2 seconds. However is there anyway to keep always checking the state of the buttons to change the mode at any moment?

I’m going to show you just the code of the servo motor going from 0 to 180 since everything else works ok.

/*
     simple debounce using delay, will block for 5mSec when state changes
*/

#include <Servo.h>

Servo myservo1;  //Servomotor


const uint32_t debounceTime = 20;  // 5 mSec, enough for most switches


const uint8_t switchCPM     = 5;  // with n.o. momentary pb switch to ground
const uint8_t switchPot     = 6;  // with n.o. momentary pb switch to ground
const uint8_t switchFSR     = 7;  // with n.o. momentary pb switch to ground

const uint8_t ledCPM        = 2; // led
const uint8_t ledPot        = 3; // led
const uint8_t ledFSR        = 4; // led


const uint8_t FSR           = 1; // Force Sensitive Resistor
const uint8_t Pot           = 0; // Potentiometer


int force;     // the analog reading from the FSR resistor divider

const bool switchOn = false;     // using INPUT_PULLUP
const bool switchOff = true;



bool lastStateCPM   = switchOff;
bool newStateCPM    = switchOff;


bool lastStatePot   = switchOff;
bool newStatePot   = switchOff;

bool lastStateFSR   = switchOff;
bool newStateFSR   = switchOff;

bool toggleStateCPM = false;
bool toggleStatePot = false;
bool toggleStateFSR = false;



void setup()
{

  Serial.begin ( 9600 );

  myservo1.attach(10);  // pin the servo is connected to
  myservo1.write(0);

  pinMode ( switchCPM, INPUT_PULLUP );
  pinMode ( ledCPM, OUTPUT );
  pinMode ( switchPot, INPUT_PULLUP );
  pinMode ( ledPot, OUTPUT );
  pinMode ( switchFSR, INPUT_PULLUP );
  pinMode ( ledFSR, OUTPUT );

} // setup

void loop ()
{

  newStateCPM = digitalRead( switchCPM );
  newStatePot = digitalRead( switchPot);
  newStateFSR = digitalRead( switchFSR);

  if ( lastStateCPM != newStateCPM ) // state changed
  {
    delay( debounceTime );
    lastStateCPM = newStateCPM;

    // push on, push off
    if ( newStateCPM == switchOn && toggleStateCPM == false )
    {
      toggleStateCPM = true;
      digitalWrite( ledCPM, HIGH );
      Serial.println( F ( "Switched ON CPM" ) );

      toggleStatePot = false;
      digitalWrite( ledPot, LOW );
      Serial.println( F ( "Switched OFF Pot" ) );

      toggleStateFSR = false;
      digitalWrite( ledFSR, LOW );
      Serial.println( F ( "Switched OFF FSR" ) );

      while (toggleStateCPM == true)
      {
        myservo1.write(0);
        newStatePot = digitalRead( switchPot);
        newStateFSR = digitalRead( switchFSR);
        newStateCPM = digitalRead( switchCPM );
        if ( newStateFSR == switchOn && toggleStateFSR == false || newStatePot == switchOn && toggleStatePot == false ) {
          toggleStateCPM = false;
          digitalWrite( ledCPM, LOW );
          Serial.println( F ( "Switched OFF CPM" ) );
        }
        delay(2000);

        newStatePot = digitalRead( switchPot);
        newStateFSR = digitalRead( switchFSR);
        newStateCPM = digitalRead( switchCPM );
       if ( newStateFSR == switchOn && toggleStateFSR == false || newStatePot == switchOn && toggleStatePot == false ) {
          toggleStateCPM = false;
          digitalWrite( ledCPM, LOW );
          Serial.println( F ( "Switched OFF CPM" ) );
        }
        myservo1.write(179);
        newStatePot = digitalRead( switchPot);
        newStateFSR = digitalRead( switchFSR);
        newStateCPM = digitalRead( switchCPM );

         if ( newStateFSR == switchOn && toggleStateFSR == false || newStatePot == switchOn && toggleStatePot == false ) {
          toggleStateCPM = false;
          digitalWrite( ledCPM, LOW );
          Serial.println( F ( "Switched OFF CPM" ) );
        }
        delay(2000);
        newStatePot = digitalRead( switchPot);
        newStateFSR = digitalRead( switchFSR);
        newStateCPM = digitalRead( switchCPM );

        if ( newStateFSR == switchOn && toggleStateFSR == false || newStatePot == switchOn && toggleStatePot == false ) {
          toggleStateCPM = false;
          digitalWrite( ledCPM, LOW );
          Serial.println( F ( "Switched OFF CPM" ) );
        }

      }

    }
    else if ( newStateCPM == switchOn && toggleStateCPM == true )
    {
      toggleStateCPM = false;
      digitalWrite( ledCPM, LOW );
      Serial.println( F ( "Switched OFF CPM" ) );
    }

  }  // if state CPM changed

The statement

delay(2000);

will keep anything useful from happening for two seconds (2000 milliseconds). You need to very much shorten your delays, or ditch them and learn to use millis().

Cool! so if i use millis() and for example an interval of 2 seconds, it would check the state of the buttons until that interval is reached and then the servo would move to the next position but keep checking the states?

edit: It worked! thank you very much!!

try 2000 loops with 1ms delay

portales: try 2000 loops with 1ms delay

Jeez ....

Have a look at the demo Several Things at a Time which illustrates how to use millis() to manage timing without blocking.

...R

I am glad that something worked, but that is NOT what I said. During delay(2000), NOTHING happens for two whole seconds: no button checking, no state checking, no servo updating, NOTHING! Not so cool.

I ditched the delays and I started using millis(). This is what I did.

unsigned long currentMillis = millis();

        if(currentMillis - previousMillis > interval){
          previousMillis = currentMillis;

          if(positionservo == 0){
            positionservo = 175;
            myservo1.write(positionservo);
          }
          else{
            positionservo = 0;
            myservo1.write(positionservo);
          }
        }

And it worked! Thank you very much for your help.