Beginner trying to learn

The brushless motor doesn't respond to the pot but just randomly stops and starts. It seems arduino get confused with the 2 int:

int Speed;
int pos = 0; // variable to store the servo position

because brushless works with potentiometer without the servo sketch that I copy-pasted. How do I code them them such that they will be separately recognized by the servo and BLDC. I'm very new to arduino and have no prior programming experience. :smiley: Thanks a lot.

Here is the code

#include <Servo.h>
Servo myservo;  // create servo object to control a servo
Servo ESC; 

int Speed; 
int pos = 0;    // variable to store the servo position


void setup(){
ESC.attach(9,1000,2000);
myservo.attach(10);  // attaches the servo on pin 9 to the servo object
myservo.write(10);

}

void loop(){
  Speed = analogRead(A0);
  Speed = map(Speed, 0, 500, 0, 180);
  ESC.write(Speed);

for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);  
  }

}

The behavior you see is what I would expect from the code you write.

When you do an analogRead everything stops for about 10mS.
Then nothing else can happen until the servo has made a 0 to 180˚ move and then back again to 0˚

Also because you have a fixed delay to allow the servo to reach its position, with faster speeds this will not be enough.

Given your lack of programming skills it will be difficult, you will have to implement a state machine. A simple example is the blink without delay example code in the IDE.
Also see the many examples on this site that people have put forward over time. This is a question that gets asked at least once a day.

Hi @gansiiiiiiiii ,

welcome to the arduino-forum.

Well done posting code as a code-section in your very first posting.

The function delay() should have the name

freeze_microcontroller_COMPLETELY_stop_ALL_code_execution_until_freezing_time_is_over()

You have two for-loops() that repeat 180 times and wait 15 milliseconds in each iteration.
This means executing the two for-loops needs

2 * 180 * 0.015 Seconds = 5.4 seconds.
So only once and for a very hsort moment your code

gets executed and then your code is "pausing" again for 5.4 seconds.
This is called blocking code

Here is a link that visualises how your code works and how the cod emust be modified to make the loop non-blocking.

And you have to combine this with non-blocking timing

analogRead() will return 0 to 1023 inclusive.

Your mapping only handles 0 to 500. map() will happily extrapolate, so you'll get values greater than 180 when the reading is above 500.

Use the constrain() macro in there too to keep the values you get between 0 and 180.

The analogRead() won't take 10 milliseconds, but your two loops do keep the loop from looping, so the ESC will only see a change to its setting once every few seconds was it 5 something?

You allow 15 milliseconds per degree in your sweep, most servos will keep up with that. However you handle unblocking the motion code, as long as you are aiming for the end angle the worst that woukd happen is that the servo might not actually finish when the code makes it look like it shou,d have long arrived. But left to itself, the servo will catch up to the latest angle you send it.

a7

This example code was written with hobby servos in mind, I think, not ESCs and brushless motors. The delay is used here so the servo doesn't just race from 0 -180 (then back again in the subsequent for loop), to give a more controlled "panning" motion, if you will. It's perfectly fine to use the library the way you have to control both the motor and servo, I just think it's important to highlight why the example sketch had the delays in the first place.

In your application, which seems to be something like controlling a brushless motor and servo as we would in an RC car, the delay is unnecessary at best, detrimental at worst, as other forum members are indicating. You will want your motor and servo to be snappy in response to the control inputs and delay is antithetical to that.

Also, you attach the servo to pin 10 here, yet the comment says pin 9. Your servo is on pin 10, right?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.