Select Button for Controller (Servo)

Hi. So I am having a bit of programmers block and I cannot figure out how to use the select button twice. So this is for a mechanical arm. The servo motor is meant to grab and ungrab so I had the idea of pressing the select button once to grab and press it again to ungrab as we are using the same controller to also move and rotate the mechanical arm so i thought this way was easier. This is my code so far for just the servo motor (just so you can see what I have done for that part already). I am planning on doing a sweep motion:

#include <ezButton.h>
#include <Servo.h>

Servo myservo;
#define VRX_PIN A1
#define VRY_PIN A0
#define SW_PIN A2
#define DIR_PIN_1 2
#define STEP_PIN_1 3
#define DIR_PIN_2 4
#define STEP_PIN_2 5

#define SELECT_THRESHOLD 485

int xValue = 0;
int yValue = 0;
int bValue = 0;
int pos = 0;

void setup() {
  pinMode(SW_PIN, OUTPUT);
  Serial.begin(9600);
  myservo.attach(9);
}

void loop() {
  bValue = analogRead(SW_PIN);
  //button.loop();


if (bValue == SELECT_THRESHOLD) {
  digitalWrite(SW_PIN, HIGH);
  for (pos = 0; pos <= 45; pos += 1) { 
  myservo.write(pos); 
  delay(5); 
  }
  digitalWrite(SW_PIN, LOW);
  }
  
}

UPDATE
Thanks to camysysca I was able to understand where the approach was meant to be for which I appreciate so much x after so much research and understanding how state machines were meant to work I was able to do double click which would do a sweep motion in one way before doing the next. Here is the updated code (without the rest of the controller because that is a lot of lines of code xd):

#include "Arduino.h"
#include <Servo.h>
#define BUTTON_PIN 3
#define DELAY 20000


Servo myservo;
static unsigned long time;
static unsigned int state;
static unsigned int button;
int pos = 0;

void setup()
{
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  Serial.begin(9600);
  myservo.attach(9);
  
  time = 0;
  state = 1;
}

void loop()
{
  button = digitalRead(BUTTON_PIN);
  switch (state)
  {
    case 1:
    if(button == LOW) {
      state = 2;
      time = millis();
      for (pos = 0; pos <= 45; pos += 1) {
        myservo.write(pos);
        delay(7);
      }
    }
      break;
    case 2:
    if (button == LOW) {
      time = millis();
       if(time - millis() > DELAY) {
  state = 1;
  for (pos = 45; pos >= 0; pos -= 1) {
    myservo.write(pos);
    delay(7);
    }
  }
    break;
  }
    }
 
}

What might help is to review the tutorials for state machines. Start in state 0, button press moves you to state 1, another button press moves you back to state 0.

  • With analogRead, very unlikely you will get consistent functionality with ==.
    Maybe you wanted >=

why is the button configured as an output?

look this over

# include <Servo.h>

Servo myservo;

const byte SW_PIN   = A2;

const int PosClosed = 0;
const int PosOpen   = 45;
int targPos         = PosClosed;
int pos             = 0;

byte butState;

// -----------------------------------------------------------------------------
void loop ()
{
    // monitor button press
    byte but = digitalRead (SW_PIN);
    if (butState != but)  {         // state change
        butState  = but;
        delay (20);                 // debounce

        if (LOW == but)  {
            if (targPos == PosClosed)
                targPos = PosOpen;
            else
                targPos = PosClosed;
            Serial.println (targPos);
        }
    }

    // update servo
    if (pos < targPos)
        myservo.write (++pos);
    else if (pos > targPos)
        myservo.write (--pos);

    delay (5);
}

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

    pinMode (SW_PIN, INPUT_PULLUP); // sw wired between pin and ground
    butState = digitalRead (SW_PIN);

    myservo.attach (9);
}

The output is just to turn the pin on.

So the number would be bigger or equal to? Would that also help me to do the select button twice that way?

That does sound like something that may work.
So if for an example if state zero is ordinary state (closed) and state one is it opening would using the same number still help it to work? I will do more research about this anyway! but just double checking that it would help.

the pin is always on, as either an INPUT or OUTPUT. but it can't be read using digitalRead() if it is contfigured as an OUTPUT.

More like you will continue using irrelevant words, hoping the answer will be given, without you answering any questions. Here is your answer for your research: Every library has examples, as with ezButton. In fact it has a button-count example.

Ok! That makes sense! Definitely will change that!

I was just double checking that that is what the states would do before I research because if it wasn't then I wouldn't need to research it. I have never done states before so I have no idea. I am not expecting an answer to be given. I am literally doing research now xd it is not my fault my teachers never really taught us programming.

Do your research and practice what you learn. I am certain your teachers did teach the information, but it was you who failed to listen. Pay attention to the teacher.

Our lectures weren't even lectures xd it was literally pdf files which as I am looking at at this moment (teacher would not teach us anything he literally told us to figure it out on our own) this is the ONLY things we learnt: arrays, strings, if, while and for loops, functions (all on MATLAB) then we learnt SIMULINK which I probably will never need, and C++. We never learned anything about States. Anything about controllers. All I have done on this project is research and learn and I have done my state machine research now and I know what I am meant to be doing. But you are being unreasonable and assuming people's education. You have no idea how much effort and hours I have put into this project. All I did was ask a question and thanks to camsysca who actually recommended me something useful instead of what you gave me which wasn't even what I needed since I knew that stuff already. Majority of this controller has been completed. I was just struggling with how to do a double click since I tried to research and couldn't much hence this forum. If I knew what I was doing I wouldn't need to post this forum :slight_smile:

Excellent.

I have "taught" so many people like you in many subjects and different theaters on the need for paying attention. You want 1-on-1, spoon fed,kid gloves without putting in effort. I know that does not work. I know what works. You do not even see where you were not paying attention. I do, using your own words.

You will when you pay attention in class.

Which is it? You know or you don't? Why would you withhold helpful information?

A good carpenter does not blame the tools.

You will understand the value of libraries, and eventually, how to code without (some) libraries.

Pay attention in school. Someone is paying for your education.

Like I said i understand the website you gave me because I have done research. I have done EVERYTHING ELSE for this controller the only issue I had was CLICKING THE SAME BUTTON TWICE. If you are a "so-called teacher" you are probably not a good one with this attitude. No wonder no one listens to your lectures. I am not blaming anything. I just understand some stuff and I WANT to learn more to get a good grade. If you READ what i sent you you would understand what I learnt. So not really a good teacher are you? I don't want to go to your level anymore so this is all I am going to say before I edit this post later to show what I did BECAUSE I DID MY RESEARCH. With the website you gave me, I went through everything and went by line-by-line to understand everything because that is all I have done for this course. It is not the students fault if they need help because they didn't understand something or never approached a certain topic like state machines. Not all lecturers can go through every type of programming because sadly they don't have the time depending on the university. Unluckily mine was one of them but I have had great help from my mentor for this project because I wanted to do something more complex so I could LEARN. But due to Easter holidays I couldn't reach out to him and tried to figure it out on my own by looking at different websites to see how I could do it as well as have so much trial and error codes, like the one I showed above. You really need to stop judging people without knowing their story. I am a dyslexic and dyspraxic student who struggles a lot therefore I am required to get more help than an average student. Here is my tip for you my guy. You need to understand student needs if you are a teacher as well as to learn to change your personality because clearly there is a reason if no one is listening to you because my lecturers are great people and they work so hard. I am lucky you aren't one of them :slight_smile:

It is easy to verify this immediately with practical work. We all enjoy the results from the interaction (yes, teachers are happy when students learn). I make them overcome their weaknesses. You know, the "you can't teach me" attitude. I break that down and build up a productive person. You will a different form of this in the work force where no-one cares about excuses. You learn and produce or leave. I like to influence them to do the right thing (and sometimes, leaving is the right thing).

Then how do you imagine so many students learn "every type of programming?" The teachers know their subject matter, or they would not hold their position. Poor students are just poor students. Excuses are excuses. Correct the pattern, or be left behind.

Why, when getting good results has been my job? You will understand this some day. Best thing to do is pay attention to your teacher, not blame it on the teacher.

That could be good, but learning anything is about repetition of good habits to form a foundation, without which, complexity complicates learning. Some people can remember a procedure with one example. I would say most need the exercise of repetition. That is homework... reinforcing the day's information.

Why? Should I be like you?

I am glad you acknowledge that, I am certain they are. Show how much you appreciate them by being a good student.

We finally agree. Always good to end on an up note. I wish you well.