May Arduino execute sketch on power supply?

Hi mongol,

right now you are in a state of asking for fish again and again each day new.

How about learning fishing yourself ?

Take a look into this tutorial:

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

best regards Stefan

Hello,
Thank you, but I'm not a coder and I don't want to be. I just have 1 single project I need to do without being interested in becoming a developer. I got this kit as a birthday present and I want to use the servo engine to click a button, that's it. But thank you, the course is great indeed for anyone who wants to become an Arduino developer.

This code is great but If I could make it run just once after I plug in that would be perfect. Because now it runs the servo motor motion in a loop without stoping.

#include <Servo.h>

Servo Myservo;  // create servo object to control a servo
const byte ServoPin = 3; //~


int angle = 45;   // initial angle  for servo (beteen 1 and 179)
int angleStep = 10;
const int minAngle = 45;
const int maxAngle = 50;

void setup()
{
  // Servo button demo by Robojax.com
  Serial.begin(9600);          //  setup serial
  Myservo.attach(ServoPin);  // attaches the servo on pin 3 to the servo object

  Serial.println("Robojax Servo Button ");
  Myservo.write(angle);//initial position
}

void loop()
{
    // change the angle for next time through the loop:
    angle = angle + angleStep;

    // reverse the direction of the moving at the ends of the angle:
    if (angle >= maxAngle)
    {
      angleStep = -angleStep;
    }

    if (angle <= minAngle)
    {
      angleStep = -angleStep;
    }

    Myservo.write(angle); // move the servo to desired angle
    Serial.print("Moved to: ");
    Serial.print(angle);   // print the angle
    Serial.println(" degree");
    delay(200); // waits for the servo to get there
}

So far in this topic you have had 3 different projects as far as I can see

  • move a servo when a button is pressed
  • move a servo when a button is pressed or on startup
  • move a servo only on startup (the latest)

How exactly should the servo move ?

As asked

What should be the exact movements you want to see ?

The movement of the servo motor the is now when I press the button:
const int minAngle = 45;
const int maxAngle = 50;

a short movement from 45 to 50 degrees (i just need to push a button).
But the script from johnwasser is working just that It plays on loop (continuously) and I need for the action to happen just once (when I plug in the Arduino).

This is not an answwer to the question

If you don't answer on questions you have been asked for you start to erode the will to help you
best regards Stefan

This will do the sequence just once:

#include <Servo.h>

Servo Myservo;  // create servo object to control a servo
const byte ServoPin = 3;

const int minAngle = 45;
const int maxAngle = 50;
int angle = minAngle;   // initial angle
int angleStep = 10;

void setup()
{
  // Servo button demo by Robojax.com
  Serial.begin(9600);          //  setup serial

  Myservo.attach(ServoPin);  // attaches the servo on pin 3 to the servo object
  Serial.println("Robojax Servo Button ");
  Myservo.write(angle); //initial position
  
  angle += angleStep;
  Myservo.write(angle);
  Serial.print("Moved to: ");
  Serial.print(angle);   // print the angle
  Serial.println(" degree");
  delay(200); // waits for the servo to get there
  
  angle -= angleStep;
  Myservo.write(angle);
  Serial.print("Moved to: ");
  Serial.print(angle);   // print the angle
  Serial.println(" degree");
  delay(200); // waits for the servo to get there
}

void loop() {}

Here is the more complex version in case you ever want to change minAngle, maxAngle, or angleStep:

#include <Servo.h>

Servo Myservo;  // create servo object to control a servo
const byte ServoPin = 3; //~

const int minAngle = 45;
const int maxAngle = 50;

int angle = minAngle;   // initial angle  for servo (beteen 1 and 179)
int angleStep = 10;

void setup()
{
  // Servo button demo by Robojax.com
  Serial.begin(9600);          //  setup serial
  Myservo.attach(ServoPin);

  Serial.println("Robojax Servo Button ");
  Myservo.write(angle);//initial position

  bool running = true;
  while (running)
  {
    // change the angle for next time through the loop:
    angle = angle + angleStep;
    
    // reverse the direction of the moving at the ends of the angle:
    if (angle >= maxAngle)
    {
      angleStep = -angleStep;
    }

    if (angle <= minAngle)
    {
      running = false;
    }
   
    Myservo.write(angle); // move the servo to desired angle
    
    Serial.print("Moved to: ");
    Serial.print(angle);   // print the angle
    Serial.println(" degree");

    delay(200); // waits for the servo to get there
  }
}

void loop() {}
1 Like

johnwasser , you're my hero, it works! Thank you so much and God bless you!

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