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.
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
}
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).
#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() {}