Can Someone Help Me With The Programming?

I am New With Arduino Uno and Learned Some Basic Things About it!
I want To Run a Servo Motor by Pressing a Push Button but The Challenge is that I want to push the button only 1 time and the Motor Should Rotate continously!!
And There’s One More thing i want to add (if its possible?) I want the Servo Motor To Rotate in such a way Like It rotates 180 degrees And after a delay of 1 second it comes back to its original position(anticlockwose 180 degrees)

show a link to the hardware in use.

And what have you tried ?

Show us a good schematic of your circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.

In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.

Use the </> icon from the ‘reply menu’ to attach the copied sketch.

Do you have s servo or a continuous rotation “servo” ?

Please provide a link to where you got it

Here’s an example of a way to do it.

#include <mechButton.h>
#include <Servo.h>
#include <squareWave.h>
#include <blinker.h>

#define  SERVO_PIN   6           // Servo attaches here.
#define  BTN_PIN     5           // Button attaches here & ground.
#define  LED_PIN     2           // Optional blinking lED when running.

mechButton  button(BTN_PIN);     // Create mechButton object.
Servo       theServo;            // Create a servo object.
squareWave  ourWave(2000,1000);  // 2 second wave. One for fwd, one for rev.
blinker     aBlinker(LED_PIN);   // May be nice to see when the silly thing is on or not.
bool        lastWave;            // For deciding "Should we change directions or not?"

void setup() {
   theServo.attach(SERVO_PIN);      // Do the attach.
   button.setCallback(btnClicked);  // Set the button's callback.
   lastWave = false;                // No wave runnig so the last wave is false.

// The button was clicked, lets take action!
void btnClicked(void) {
   if (!button.trueFalse()) {                // If teh button was grounded (pushed)..
      ourWave.setOnOff(!ourWave.running());  // Toggle the tiing wave on/off;
      aBlinker.setOnOff(ourWave.running());  // Set the on/off of the blinking LED to match.

void loop() {
   idle();                                                     // idle() runs bacground stuff.
   if (ourWave.running()&&ourWave.pulseHiLow()!=lastWave) {    // If the wave is running and the wave state has changed..
      lastWave = !lastWave;                                    // Toggle our saved wave state.
      if (lastWave) {                                          // If the wave state is now true..
         theServo.write(180);                                  // Tell servo to go to 180 deg.
      } else {                                                 // Else, its false..
         theServo.write(0);                                    // Tell servo to go to 0 deg.

To get this to compile you’ll need to instal LC_baseTools from the Arduino library manager.
If this is for homework? Good luck explaining how it works to you teacher.

-jim lee

1 Like
// I want to push the button only 1 time and the Motor Should:
// Rotate 180 degrees,
// delay for 1 second,
// rotate back to its original position (anticlockwose 180 degrees)
// and repeat forever.

#include <Servo.h>
const byte ServoPin = 2;
const byte ButtonPin = 3;

Servo Motor;

void setup()

  pinMode(ButtonPin, INPUT_PULLUP);  // Button between pin and Ground

  while (digitalRead(ButtonPin) == HIGH)
    // Do nothing, waiting for button to be pressed

void loop()
  delay(200);  // Give the servo a little time to get back to 0
1 Like

Delays?!!? @johnwasser I expected more from you!

-jim lee

There is no glory in making a sketch more complicated than it has to be to meet the specification. :slight_smile:

I´m not a friend of the delay() funktion, too. But the sketch using the delay() functions fits the requirements of the the project sheet.

Opioids will make you feel better if that’s all that is called for. (on the sheet)

-jim lee

I Will Surely Try It!!
Nope , This Is not a Homework But a Model i Want To Make :sweat_smile:
Taking New Challenges and Learning New things!!
Thanks You Rock Sir!

Why Can’t We Use For loop to Make it rotate Continuously , for example
I have done a little bit coding while i was in school so why can’t we apply this ?
Is is legal?
Btw thanks For The Code :smile: I appreciate it!!

Its The Basic Circuit Of A Push Button and a Servo Motor with Arduino(Nothing Special about that) :slight_smile:
I was Stuck in The Coding part!!
If U want to know components then here u go:
Arduino Uno
SG90 Servo Motor
Jumper Wires
pull-up resistor (here 2.2 KOhms)

That would, in theory, execute the semicolon 999 times (not forever). Since the ‘null statement’ (:wink: does nothing, the compiler will probably optimize the whole loop away and substitute this statement which has the same result in less time:
i = 999;
(Edited. I had accidentally put 1000 instead of 999.)

I know there’s some tongue-in-cheek here, but actually I think using delay() is fine under some circumstances. In fact, better than forcing a finite state machine onto every solution. There is a lot of this kind of thing on this forum:

“You should be using a finite state machine for this! Uhmmm… what was the question, again?”

There is nothing wrong at all in blocking the processor for the duration of a delay() if there is nothing else that needs doing during that period. And for some applications that is, indeed, the case.

The best thing about delay() is that it is a single line: incredibly easy to use, keeps your source code simple, reduces the risk of bugs. FSMs are inherently more complicated and therefore more likely to contain bugs.

So, if you want to trigger an action, wait for a second, and then trigger another action, then delay(1000) is a great solution. From an engineering point of view, it is never good to make something more complicated than it needs to be.

Haha, more than a little! I think John is one of the most competent Arduino guys out there. He is forever coming up with solutions that I can’t even fathom!

As for delay(). I’ve used it too. It’s just such the crutch that cripples in the long run. And we all fight against it.

-jim lee

Sir Your Quote Is 99% correct, But there’s a Small Mistake There Probably!
When i Tried Your Code the Motor Was Doing The job perfectly but I didnt even pressed the button!!!
Then i Changed The Above Mentioned "HIGH "to “LOW”
When i Press The Push button then Only the Motor should Start Rotating thats why it should be Low!!
BTW thankss a Lot Sir!

That’s what happens when you don’t say how your button is wired. We don’t always guess correctly.

1 Like

But why fight against it? We are supposed to be engineers, aren’t we? Reasonable, rational, logical, etc.

The rational position is surely: “delay() is the correct solution when there is nothing else needs doing during that time interval; FSM is the correct solution when other activities cannot wait.”

It is never a good thing to build a suspension bridge when all you need is a truss bridge. It is never a good thing to “over-engineer” a solution, because you are adding failure points, costs, material usage, etc.

As good engineers we must avoid dogmatism - it can have no place in our thinking. “Never use delay(); it is always bad” is pure dogmatism. Instead, we should advocate the most appropriate solution for the problem. Sometimes that is delay(), sometimes it’s an FSM. Sometimes it’s a nail, sometimes it’s a screw. Sometimes it’s moulded plastic, sometimes it’s die cast aluminium.

So… You’re more upset about my treatment of delay() than my teasing of John? Huh, I totally misunderstood your comment. Sorry about that.

No, I don’t like delay(). I don’t like the thinking process it tends to foster. And, do what little I can to show others ways around using it.

-jim lee