Hello,
I'm a total beginer, I don't know hot to write any code. But I got this code from some tutorial online which is moving my servo motor the way I need after pressing the button.
Can you please tell me, how should the code be changed in order for the servo action to be executed once also when I plug in Arduino to power supply (like it's executed now when I push the button). Now when I connect the power supply does nothing, the motor only works when I push the button.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
#define servoPin 3 //~
#define pushButtonPin 2
int angle =45; // initial angle for servo (beteen 1 and 179)
int angleStep =10;
const int minAngle = 45;
const int maxAngle = 50;
const int type =2;//watch video for details. Link is at the top of this code (robojax)
int buttonPushed =0;
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
pinMode(pushButtonPin,INPUT_PULLUP);
Serial.println("Robojax Servo Button ");
myservo.write(angle);//initial position
}
void loop() {
if(digitalRead(pushButtonPin) == LOW){
buttonPushed = 1;
}
if( buttonPushed ){
// 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(type ==1)
{
buttonPushed =0;
}
}
if (angle <= minAngle) {
angleStep = -angleStep;
if(type ==2)
{
buttonPushed =0;
}
}
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
}
}
Now my servo motor runs whenever I press a button. How can I make it run once also when I plug in Arduino to the power supply?
Thank you!
#include <Servo.h>
Servo myservo; // create servo object to control a servo
#define servoPin 3 //~
#define pushButtonPin 2
int angle =45; // initial angle for servo (beteen 1 and 179)
int angleStep =10;
const int minAngle = 45;
const int maxAngle = 50;
const int type =2;//watch video for details. Link is at the top of this code (robojax)
int buttonPushed =0;
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
pinMode(pushButtonPin,INPUT_PULLUP);
Serial.println("Robojax Servo Button ");
myservo.write(angle);//initial position
}
void loop() {
if(digitalRead(pushButtonPin) == LOW){
buttonPushed = 1;
}
if( buttonPushed ){
// 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(type ==1)
{
buttonPushed =0;
}
}
if (angle <= minAngle) {
angleStep = -angleStep;
if(type ==2)
{
buttonPushed =0;
}
}
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
}
}
add a Boolean variable runOncethat you set to true initially and modify the button test to also consider this variable. and set it to false for the future
int angle =45; // initial angle for servo (beteen 1 and 179)
int angleStep =10;
const int minAngle = 45;
const int maxAngle = 50;
const int type =2;//watch video for details. Link is at the top of this code (robojax)
bool runOnce = true; // run upon boot
...
void loop() {
if ((digitalRead(pushButtonPin) == LOW) || runOnce) {
runOnce = false;
...
if you only want to set the servo to a given angle, just write that at the end of the setup
Cross-posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.
Repeated cross-posting will result in a timeout from the forum.
In the future, please take some time to pick the forum board that best suits the topic of your question and then only post once to that forum board. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum board. It contains a lot of other useful information. Please read it.
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.
Put the code to be executed in a function and call the function whenever you want to move the servo. ie from setup() or as a reaction to a button press
it works but does not do what you want, that's another issue
the reason you don't see the behaviour is because you depend on if( buttonPushed ){
Which is kinda useless since if it is pushed you know it by the previous test: if(digitalRead(pushButtonPin) == LOW)
I have this Arduino code I found in a tutorial which moves the servo motor when I power on the Arduino and whenever I press the button. How can I reduce the code to eliminate the button part and just execute the motor command once when I plug the Arduino in?
I'm a very beginer so I don't know anything about writing or reading code, I jut copy-pasted that code from a tutorial.
Thank you!
/*
Controlling a servo with Push button with Arduino
this is code #1 to control servo with push button by Robojax (Robojax.com )
code #1 when push button the servo goes from 0° to 180° (or set your angle) and returns back
Watch video for code #1: https://youtu.be/fPrPRZlGdvA
*/
#include <Servo.h>
Servo myservo; // create servo object to control a servo
#define servoPin 3 //~
#define pushButtonPin 2
int angle =45; // initial angle for servo (beteen 1 and 179)
int angleStep =10;
const int minAngle = 45;
const int maxAngle = 50;
const int type =2;//watch video for details. Link is at the top of this code (robojax)
int buttonPushed =1;
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
pinMode(pushButtonPin,INPUT_PULLUP);
Serial.println("Robojax Servo Button ");
myservo.write(angle);//initial position
}
void loop() {
if(digitalRead(pushButtonPin) == LOW){
buttonPushed = 1;
}
if( buttonPushed ){
// 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(type ==1)
{
buttonPushed =0;
}
}
if (angle <= minAngle) {
angleStep = -angleStep;
if(type ==2)
{
buttonPushed =0;
}
}
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
}
}
Cross-posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.
Repeated cross-posting will result in a timeout from the forum.
In the future, please take some time to pick the forum board that best suits the topic of your question and then only post once to that forum board. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum board. It contains a lot of other useful information. Please read it.
/*
Controlling a servo with Push button with Arduino
this is code #1 to control servo with push button by Robojax (Robojax.com )
code #1 when push button the servo goes from 0° to 180° (or set your angle) and returns back
Watch video for code #1: https://youtu.be/fPrPRZlGdvA
*/
#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
}