Hello. Can you please help me with my code ?
Im doing an arduino project with some leds , positional servo 180(i wasnt able to get a continious which i need) and a pushbutton.
I just started learning arduino bymself and im stuck on this problem.
The idea is to use the pushbutton as a toggle which works and when you press it to turn on the leds and the servo , and when you press it again to turn off the leds and the servo, so everything works fine but when i get into the for loop for my servo , because its not continious i have to make a for loop to endlessly go from 0 to 180 and back so it rotates all the time, and when i try to break; the for loop with commands like "while" or "if" i fail because it dosent stop until it finishes the loop.
So if u guys can help me out i will appreciate it really much .
I deleted my attempts at breaking the for loop so i dont confuse anybody with my low knowledge.
//
//
#include <Servo.h>
#define zelena 4
#define zolta 2
#define crvena 3
#define kopce1 6
Servo myservo;
int LEDstate=0;
int buttonNew;
int buttonOld=1;
int del=100;
int pos = 0;
void setup()
{
Serial.begin(9600);
myservo.attach(9);
pinMode(zelena, OUTPUT);
pinMode(zolta, OUTPUT);
pinMode(crvena, OUTPUT);
pinMode(kopce1, INPUT);
}
void loop()
{
buttonNew=digitalRead(kopce1);
if(buttonOld==0 && buttonNew ==1){
if(LEDstate==0){
digitalWrite(zelena,HIGH);
digitalWrite(zolta,HIGH);
digitalWrite(crvena,HIGH);
servo();
LEDstate=1;
}
else {
digitalWrite(zelena,LOW);
digitalWrite(zolta,LOW);
digitalWrite(crvena,LOW);
LEDstate=0;}
}
buttonOld=buttonNew;
delay(del);
}
void servo()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(200); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(200); // waits 15ms for the servo to reach the position
}
}
Hi and welcome to the forum. Before you post again, please read the forum guide in the sticky post, then edit your post above and correct how you posted your code. Right now it's breaking forum rules. Thanks.
Generally try to not use "break" at all. It's like "goto" in former BASIC programs.
If you see the need of breaking out a loop then use another type of loop. A "for .. next" is intended to run completely every iteration until its end. But something like this:
int i = 0;
while ( i < maxIter )
{
// loop body
// ...
i++;
}
can be used to exit the loop at any time by setting
I noticed your servo() function is infinitely recursive. That means it calls itself, without any conditions.
Normally I would expect this to cause the Arduino to run out of ram memory and behave badly/strangely. Perhaps the compiler is clever enough to figure out that this is 'tail-recursion' and change it into another loop. If that happens, once servo() is called, it can never escape and will be stuck forever.
Thank you for the help. I had no knowledge about that, ill fix that now.
But how am i supposed to make the positional servo keep spinning and not stoping when the loop is finished , without calling itself.
Thanks for correcting your post and using code tags correctly.
I notice the code is now updated and the infinite recursion problem is fixed, and there are other changes also.
Thanks also for posting a wiring diagram. It's not great. There is a reason breadboards have all those holes, you know. Please use them! A proper schematic would be far preferable to this type of diagram.
The problem is the way that the servo() function is written. It is "blocking code". Although the delay() used is always short (200ms), it is inside a loop which repeats 180 times, so the loop cannot end for 180 x 200 = 36000ms or 36 seconds. Then there is another loop which also takes 36 seconds. During this time, the Arduino is not checking the button to see if it has been pressed. This blocking code will have to be re-written.
Hello
Thanks for replying
No, in short words i want to make it:
1.If(buttonispressed) Servo keeps rotating (0to180 and back without ending ) + led pattern
2. When the button is pressed once again then stop the servo and turn off the leds
Hopefully i explained it better.
Ohh, so to fix this problem i need to lower the delay time or do i need to rewrite the whole servo() function?
I tried fixing my circuit hopefully its better visually.
Hello martin5644
The best way is to avoid the delay() function inall. This function blocks the execution of the sketch. Instead a timer function based on the BLINKWITHOUTDELAY example to be found in the IDE will bring a smart solution.
Have a nice day and enjoy coding in C++.
Дайте миру шанс!
p.s. Do you have experience coding using array, structs and other powerfull C++ instructions?
Thanks for taking your time and replying .
I will try your method later if i dont find a soluition for the " for" loop.
But how i will be able to make the servo go 0>180 and back with a while loop.
Hello @paulpaulson ,
Thanks for replying, i will avoid the delay function, i had no idea it costs so many problems.
I dont have any experience using array and structs.
I only started learning c++ and have small knowledge from watching tutorials and learning from forums.
Have a nice day yourself!
Make a new variable to hold the direction that the servo moves:
int pos = 0, dir = 1;
Then use this in servo() so that the for-loops are not needed:
void servo()
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(200); // waits 15ms for the servo to reach the position
pos += dir;
if (pos == 179 && dir == 1 || pos == 0 && dir == -1) dir = -dir;
}
Thank you for getting rid of the for functions and finding another way .
With your code the function has some problems, when i press the button it goes to 180* and stops there.
When i press it repedeatly it slowly goes back with every press.
And i cant stop the Servo while it on its way to 180.
I am able to share my tinkercad project to check it your self if you dont understand me.
I am waiting on your confrimation if you want to ?