Three position servo

I trying to write a code so that I can use three push buttons to make a servo move to three positions one for each button.
Thanks, Wally.

You didn't actually ask a question :wink: but presumably something's not working. Share your code and circuit diagram, and explain where you need help.

Sorry, I don’t know how to write the code. I would like someone to write the code for me.

Thanks, Wally.

did the internet search engines break...?

just one of MANY!

That tutorial shows the servo powered from the Arduino, which is a no-no even for a micro servo; don't do that.

edit... oops ... misread it that there are 3 servos.. but it's only 1 and 3 buttons:

new pic...

edit- Arduino power not shown :wink:

1 Like

Hello Greatnorthern
Try and test this example sketch.

/* BLOCK COMMENT
  ATTENTION: This Sketch contains elements of C++.
  https://www.learncpp.com/cpp-tutorial/
  Many thanks to LarryD
  https://europe1.discourse-cdn.com/arduino/original/4X/7/e/0/7e0ee1e51f1df32e30893550c85f0dd33244fb0e.jpeg
  https://forum.arduino.cc/t/three-position-servo/1000588
  Tested with Arduino: Mega[x] - UNO [ ] - Nano [ ]
*/
#define ProjectName "Three position servo"
#include <Servo.h>
Servo myservo;
// HARDWARE AND TIMER SETTINGS
// YOU MAY NEED TO CHANGE THESE CONSTANTS TO YOUR HARDWARE AND NEEDS
constexpr byte ServoPin {8};
constexpr int ServoStart {90};
// CONSTANT DEFINITION
enum Buttonstatus {Released, Pressed, Idle};
enum Timer {Stopp, Start, Redo};
// VARIABLE DECLARATION AND DEFINITION
unsigned long currentTime;
// -- objects -----------------------------------------
struct TIMER {              // has the following members
  unsigned long time2wait;  // memory for interval time
  unsigned long atPresent;  // memory for actual time
  int conduct;                        // control for stop/start/redo
};
struct BUTTON {              // has the following members
  byte pin;                         // port pin
  bool statusQuo;            // current state
  TIMER scan;                   // see TIMER struct
};
struct BUTTONSERVO  {        // has the following members
  int angle;                               // insert desired angle value 
  BUTTON knop;                    // see BUTTON struct
};
BUTTONSERVO buttonServos[] {
  {0, A0, false, 20, 0, Redo},
  {180, A1, false, 20, 0, Redo},
};
// -- services  -----------------------------------------
bool timerEvent (TIMER &timer) {
  bool reTurn = currentTime - timer.atPresent >= timer.time2wait && timer.conduct;
  if (reTurn) timer.conduct == Timer::Redo ? timer.atPresent = currentTime : timer.conduct = Timer::Stopp;
  return reTurn;
}
// ------------------------------
int getKnop( BUTTON &knop) {
  if (!timerEvent(knop.scan)) return Buttonstatus::Idle;
  int stateNew = !digitalRead(knop.pin);
  if (knop.statusQuo == stateNew) return Buttonstatus::Idle;
  return (knop.statusQuo=stateNew);
}
// -------------------------------------------------------------------
void setup() {
  Serial.begin(9600);
  Serial.println(F("."));
  Serial.print(F("File   : ")), Serial.println(__FILE__);
  Serial.print(F("Date   : ")), Serial.println(__DATE__);
  Serial.print(F("Project: ")), Serial.println(ProjectName);
  pinMode (LED_BUILTIN, OUTPUT);  // used as heartbeat indicator
  //  https://www.learncpp.com/cpp-tutorial/for-each-loops/
  for (auto &buttonServo : buttonServos) pinMode(buttonServo.knop.pin, INPUT_PULLUP);
  myservo.attach(ServoPin);
  myservo.write(ServoStart);
}
void loop () {
  currentTime = millis();
  digitalWrite(LED_BUILTIN, (currentTime / 500) % 2);
  for (auto &buttonServo : buttonServos) {
    if (getKnop(buttonServo.knop) == Pressed) myservo.write(buttonServo.angle);
  }
}

Have a nice day and enjoy coding in C++.
Дайте миру шанс!

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