Hi, so, I am new with Arduino and I have problem with this code. I need the first servo to move when the first button is clicked. After clicking the second button, the second servo should move.
The first servo is moving. As it should. But the second servo does not respond to the button. There is probably a small error in the code, maybe I separated the sections wrong?
I am giving you a link to the simulation with the code. I hope someone will be able to help me. Probably only a small code modification is needed, but I don't know what exactly. Thanks for the anwers. Here is link, just start a simulation and click at buttons. You can view the code in the link or below:
#include <Servo.h>
// constant variables used to set servo angles, in degrees
const int straight = 90;
const int divergent = 110;
// constant variables holding the ids of the pins we are using
const int buttonpinA = 8;
const int servopinA = 9;
const int buttonpinB = 6;
const int servopinB = 7;
// servo movement step delay, in milliseconds
const int step_delay = 70;
// create a servo object
Servo myservoA;
Servo myservoB;
// global variables to store servo position
int pos = straight; // current
int old_pos = pos; // previous
void setup()
{
// set the mode for the digital pins in use
pinMode(buttonpinA, INPUT);
pinMode(buttonpinB, INPUT);
// setup the servo
myservoA.attach(servopinA); // attach to the servo on pin 9
myservoA.write(pos); // set the initial servo position
myservoB.attach(servopinB); // attach to the servo on pin 7
myservoB.write(pos); // set the initial servo position
}
void loop()
{
int button_state = digitalRead(buttonpinA);
if(button_state == HIGH){
old_pos = pos;
pos = pos == straight ? divergent: straight;
if(old_pos < pos){
for(int i = old_pos + 1; i <= pos; i++){
myservoA.write(i);
delay(step_delay);
}
} else {
for(int i = old_pos - 1; i >= pos; i--){
myservoA.write(i);
delay(step_delay);
}
}
int button_state = digitalRead(buttonpinB);
if(button_state == HIGH){
old_pos = pos;
pos = pos == straight ? divergent: straight;
if(old_pos < pos){
for(int i = old_pos + 1; i <= pos; i++){
myservoB.write(i);
delay(step_delay);
}
} else {
for(int i = old_pos - 1; i >= pos; i--){
myservoB.write(i);
delay(step_delay);
}
}
}
}}// end of loop
Try making different variable names for each section of the code rather than reusing them.
button_state changes to button_a_state and button_b_state, old_pos becomes old_pos_servo1 and old_pos_servo2, etc.
welcome to the forum.
Running your simulation requires registering at autodesk.
I will definitely not register at autodesk.
autodesk is a company that has a very aggressive marketing strategy to pour into every edge of schools and DIY-projects by offering "get used to our products for free"
There is another simulator that is opensource.
It is called wokwi
here is the WOKWI simulation of your code with a own set of variables for servo A and another set of variables for servo B
Though I guess this is still not that what you want.
I try to write down a precise description of what you probably want
As long as button A is pressed move servo A. If button A is released servo A shall stop at what ever position it is.
at the same time = in parallel
As long as button B is pressed move servo B. If button B is released servo B shall stop at what ever position it is.
This is medium advanced programming by using non-blocking timing
Non blocking timing means
there is only one big loop which is void loop() itself
There are zero function-calls to delay() and there are zero for-loops and zero while-loops
because all three
delay()
for-loops
while-loops
are blocking.
Which means as long as the delay / for / while is executing nothing else can happen.
They way to go there is structured programming
build your own functions
as pseude-code
void moveServoA()
// all the code that makes the servo move back and forth slowly
// but with non-blocking timing
void moveServoB()
// all the code that makes the servo move back and forth slowly
// but with non-blocking timing
if (buttonA is pressed) {
moveServoA()
}
if (buttonB is pressed) {
moveServoB()
}
To understand non-blocking timing requires a prelimary exercise to understand it
the basic principle is to store snapshots of time and check how much time has passed by since storing the snapshot of time
Hi, thank you for telling me about Wokwi. As I said, I am pretty new in this community.
My explanation of the problem was poorly written. Sorry about that. Here is a more understandable version:
In Wokwi, I made a diagram for moving one servo using a button. When you press the button, the servo will move a little. When you press the button again, the servo will return to the previous position. When you run the simulation, the code does exactly what I need (here is link).
All I need is how to do the code if I need to have multiple servos with their own button powered by one Arduino.
For example: If I press button A, servo A will move. If I press button B, servo B will move regardless of what position servo A is in.
I want to use a breadboard for power and grounding (as on picture).
remember: loop() is looping at high speed all the time
This means the sub-function is called over an over again through void loop()
instead of a for-loop
Now changing the servo-position slowly requires non-blocking timing
which is explained here
When I added your code (I hope I have permission?) I got this error message. I don't know Arduino well, so I don't know exactly what that means. I am giving a link to the simulation here.
i simply posted a portion of your code properly formatted so that you could see that the 2nd button code was inside the condition for the first.
i had hoped you would see that this explains your problem and would know how to correct it by moving it out from inside the 1st button condition. do you need help?
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.
All right, there is my last try. I didn't understand the code too much. So, now I will read tutorials and try to understand programming better. If someone could show me how to make my code look correct, that would be very helpful. However, thank you for your time and have a nice day.
@gcjr
have you ever considered for a few seconds that a code with that stage of sophistication is already wayyy above the head of a real beginner?
have you ever considered that presenting a to heavy to understand code can be frustrating?
have you ever considered that with this really poor and short comments this code
IS
hard to understand for a real beginner??!!
Is your main aim to chase away all young people that are interested in learning coding
except the real nerdy ones that love most to read the hardest to understand code?
If you aren't aware of that you are that forum-member that has the biggest
your last attempt to make it work is missing the void setup-function
and has two functions void loop()
each and every Arduino-Sketch requires to have
exact one function
void setup() {
}
and exact one function
void loop() {
}
You made a good decision to learn more about the basics.
I modifyed your WOKWI-code so that it is working now.
It works in a blocking manner
as long as the code is executing a for-loop the code outside this for-loop is not eecuted
of course because you are in a for-loop which runs down from start to finish
Alright guys, I have to thank you for your help. You helped me a lot, especially @StefanL38. I promise to read all the tutorials and try to get better at programming. I still have a lot to learn, but I believe I will make progress. And also thanks for making the code workable, now I can look for my mistakes.
Have a great day and thank you again.