I am making a program by combining servo and dc motor at the same time. if I use delay it will delay everything. I want the servo to move to 180 degrees at the beginning and the dc motor to run with increasing rpm slowly, but I also want the servo to position 0 when the dc motor is at peak rpm, then the dc motor rpm decreases slowly. like this my code
I moved your topic to an appropriate forum category @ismail_1605.
In the future, when creating a topic please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.
This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.
Thanks in advance for your cooperation.
True. Study this tutorial to learn how to avoid that (replace "blink LED" with "send servo command"):
https://www.baldengineer.com/blink-without-delay-explained.html
can anyone help me to change my code according to my wishes above. sorry if my english is not understandable
#include <ESP32Servo.h>
Servo myservo;
int pos = 0;
int R_PWM = 13;
int L_PWM = 27;
int R_EN = 14;
int L_EN = 12;
void setup() {
myservo.attach(26);
pinMode(R_PWM, OUTPUT);
pinMode(L_PWM, OUTPUT);
pinMode(R_EN, OUTPUT);
pinMode(L_EN, OUTPUT);
digitalWrite(R_EN, HIGH);
digitalWrite(L_EN, HIGH);
}
void loop() {
kasihMakan();
}
void kasihMakan() {
myservo.write(180);
int speed;
for (speed = 0; speed <= 150; speed = speed + 3) {
analogWrite(R_PWM, speed);
delay(200);
}
delay(1000);
myservo.write(0);
for (speed = 150; speed >= 0; speed = speed - 3) {
analogWrite(R_PWM, speed);
delay(200);
}
delay(1000);
}
sorry i just asked here, so i don't know anything yet. thanks
After studying the tutorial, can you rewrite the sample program you included? In addition, you request something to happen when the motor reaches some top speed, but your code does not include any RPM measurements.
but your code does not include any RPM measurements.
Sorry, speed = RPM
I've tried making it using millis(), but it doesn't work the way I want it to.
you said you want the servo to move to 0 when the DC peaks.
do you mean you want the servo to move slowly as the DC motor speeds up and down?
I guess you will actually have to measure it. Your simple program is getting more complicated as you progress. Back to the drawing board! But include the blink without delay logic.
I want when the dc motor starts rotating to a speed of 150, the servo also moves to 180 degrees. when the dc motor has reached a speed of 150, the servo returns to 0 degrees
actually it's not that simple. i set it according to the hour, for example at 9 o'clock, kasihMakan() will work. so blink without delay only works once. that's just part of the project code that i'm building
isn't that what the code you posted does,
- the servo moves to 180
- the motor speeds up to 150
- the servo moves to zero.
why do you have a delay before moving the servo to 0?
i don't know
The point is i want my code to run like this.
can anyone give me a solution?
You have been given the basis for a solution several times, but nothing changed.
@Paul_KD7HB, isn't that what the code he posted is doing? what am i not understanding?
It's my code that's wrong, that's why I need help to create a code with the conditions I gave.
Stop saying this.
How are you measuring this and how is that measurement sensed by the Arduino?
setServoAngle = map(motorSpeed, 0, 150, 180, 0);
I apologize for that. It's because of my lack of English.
okay, i'll try this later
The code you posted in post #4 does exactly what you want.
So what is not working?
instead of what you want the code to do, try explaining what it is not doing
the code below
- moves the servo to 180 just before it
- starts speeding up the motor to 150
i