Help with moving servos by pressing a button!

Hello everyone, I'm hoping for some feedback on my code that doesnt seem to want to work for me, though that may be down to my circuitry work as well. My goal is to have a pushbutton function with multiple different outcomes depending on how many times the button has been pressed and how long it has been since the button has been pushed. The first push turns three servo motors, begins a time counter, and begins a pushbutton counter. That sets the pushbutton counter to "1." Pushing the button again has two different conditions;
Condition 1: less than 2 minutes has elapsed since the previous push, thus causing two more servos to turn and the pushbutton counter to go up by two counts, thus now at "3;"
Condition 2: over 2 minutes has elapsed since the previous push and the pushbutton counter increases by one without the button being pushed, setting it now at "2," and the next push (however long after the timer passes the 2 minute mark) causes the two aforementioned servos to turn, the pushbutton counter to increase by one, and have it set to "3."
Then, a final button push will increase the pushbutton counter by one to "4," all five servos move back into original position in sequence from last to first, and the code resets the pushbutton counter, timer, and loop back to the beginning.
Here is my code as follows:

#include <Servo.h>

//These constants won't change:
const int buttonPin = 2;  // the number of the push button pin
const int eyeplatePin = 3;
const int crplatePin = 5;
const int clplatePin = 6;
const int jrplatePin = 9;
const int jlplatePin = 10;

//Variables will change:
Servo eyeplate; //initialize the various individuals servos and set beginning state to zero:
int eyepos = 0;
Servo cheekright;
int crpos = 0;
Servo cheekleft;
int clpos = 0;
Servo jawright;
int jrpos = 0;
Servo jawleft;
int jlpos = 0;

int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;

long lastPressedTime = 0;
long sweepDelay = 120000;

void setup() {

  //initialize button pin as input:
  pinMode(buttonPin, INPUT);

  //initialize servo pins as outputs:
  pinMode(eyeplatePin, OUTPUT);
  pinMode(crplatePin, OUTPUT);
  pinMode(clplatePin, OUTPUT);
  pinMode(jrplatePin, OUTPUT);
  pinMode(jlplatePin, OUTPUT);

  eyeplate.attach(3); //attach the servos to their pins
  cheekright.attach(5);
  cheekleft.attach(6);
  jawright.attach(9);
  jawleft.attach(10);
}

void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if state has changed, increment the counter
    if (buttonState == HIGH) {
      // if current state is HIGH then the button went from off to on:
      buttonPushCounter++;
      lastPressedTime = millis();
      // also sweep the eyeplate open to a set angle position:
      eyeplate.write(45);
      delay(300);
      cheekright.write(45);
      cheekleft.write(-45);
    }
    lastButtonState = buttonState;
    delay(50);
  }
    if (buttonState != lastButtonState) {
      if (buttonState == HIGH) {
        if ((millis() - lastPressedTime) < sweepDelay)  {
          buttonPushCounter;
          buttonPushCounter;
          jawright.write(45);
          jawleft.write(45);
        }
        else ((millis() - lastPressedTime) > sweepDelay);  {
          buttonPushCounter++;
            if (buttonState != lastButtonState) {
              if (buttonState == HIGH)  {
                buttonPushCounter++;
                jawright.write(45);
                jawleft.write(45);
              }
              lastButtonState = buttonState;
              delay(50);
            }
        }
      }
    }
    if (buttonState != lastButtonState) {
      if (buttonState == HIGH)  {
        buttonPushCounter++;
        jawright.write(0);
        jawleft.write(0);
        delay(300);
        cheekright.write(0);
        cheekleft.write(0);
        delay(300);
        eyeplate.write(0);
}
    }
      }

I hope to hear back from somebody soon, thank you!
P.S. I am a super beginner so actual lines of code to swap into mine rather than verbal instructions is much appreciated :).

Once you do this part, the two following:
if (buttonState != lastButtonState) {
will never be true. Why are you checking the same button three times?

Saying "buttonPushCounter" twice does nothing. If you expected it to do something then your expectations are wrong and you should reexamine them.

So how would I go about fixing it?

Start over?

You will need an input pin and a counter for each of the four buttons. You already have "state change detection" code:
if (buttonState != lastButtonState) {

Do that for each of the four buttons. You should probably add debouncing if you have mechanical buttons. That keeps contact bounce from getting counted as multiple presses:

  if (buttonState != lastButtonState && millis() - lastButtonPressTime > 10) {
    lastButtonPressTime = millis();

Sorry, I'm not sure where you're getting four buttons but I only have one button that controls five servos, just depending on the number of presses.

Oops. I had your problem mixed up with someone else's. Ignore my last reply.

difficult to read. obviously a state machine with 2 inputs: button and timer, that can use a switch statement to keep track of states. inputs (stimuli) trigger an action that can advance the state by 1 or 2, as well as change the position of servos

consider. makes it easier to understand the code
you can add code to move you servos appropriate for each state under the condition when a button is pressed

const byte PinBut = 2;
byte butLst;

unsigned long msecPeriod = 0;
unsigned long msecLst;
unsigned long msec;
#define TwoMin   5000           // just 5 sec for testing

int state = 0;

char s [80];

// -----------------------------------------------------------------------------
bool
butPress (void)
{
    byte but = digitalRead (PinBut);
    if (butLst != but)  {
        butLst = but;
        delay (20);     // debounce
        if (LOW == but)  {
         // Serial.println ("butPress");
            return true;
        }
    }
    return false;
}

// -----------------------------------------------------------------------------
bool
tmr (void)
{
    if (msecPeriod && msec - msecLst >= msecPeriod) {
        msecPeriod = 0;
        return true;
    }
    return false;
}

// -----------------------------------------------------------------------------
void
setState (
    int   st )
{
    state = st;

    sprintf (s, "state %d", state);
    Serial.println (s);
}

// -----------------------------------------------------------------------------
void
loop ()
{
    msec = millis ();

    switch (state) {
    case 0:
        if (butPress ()) {
            msecLst    = msec;
            msecPeriod = TwoMin;
            setState (1);
        }
        break;

    case 1:
        if (tmr ())
            setState (2);

        if (butPress ())  {
            msecPeriod = 0;
            setState (3);
        }
        break;

    case 2:
        if (butPress ())
            setState (3);
        break;

    case 3:
        if (butPress ())
            setState (0);
        break;

    default:
        sprintf (s, " state %d", state);
        Serial.println (s);
    }
}

void
setup ()
{
    Serial.begin (9600);

    pinMode (PinBut, INPUT_PULLUP);
    butLst = digitalRead (PinBut);

    setState (0);
}ui

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