ESC works in reverse with Potentiometer but not with manual Code

Hi everyone...So I got this reversable ESC. Using it to run a Brushless motor. Would like to use the motor in a washing machine like contraption that spins back and forth and cleans parts in water. Thus the motor should move forward and backward.

Here is the issue: When I use a potentiometer the mid-value of 90 makes the motor standstill, while values above make it move forward and number below backward. With the following code, everything works as expected, but as soon as I type in values the motor does not reverse. Actually, it does not change direction unless I use a potentiometer.

I don't see a reason why a value from (0-180) from the Potentiometer reverses the motor's direction just fine, but when I type in the values it does not change direction.

Code that works fine with Potentiometer and reverses the direction:

> #include <Servo.h> 
> Servo ESC;   
> 
> int value; 
> void setup()
> {
>   Serial.begin(9600);
>   ESC.attach(3);
> }
> 
> void loop() { 
>   value=analogRead(A0);
>   value=map(value, 0, 1023, 0, 180); 
>   ESC.write(value);                   . 
>   Serial.println(value);
>   }
> 
>


But this manually input code does not reverse the motor:



> #include <Servo.h>
> Servo ESC;
> int i;
> void setup(){
> Serial.begin(9600);
> ESC.attach(3);
> }
> 
> void loop() {
>   
>   for (i=90;i<=100;i++){
>        ESC.write(i);
>          Serial.println(i);
>        delay(1000);
>   }
>   for (i=100;i>=90;i--){
>        ESC.write(i);
>          Serial.println(i);
>        delay(1000);
>   }
>    for (i=90;i>=80;i--){
>        ESC.write(i);
>          Serial.println(i);
>        delay(1000);
>    }
>      for (i=80;i<=90;i++){
>        ESC.write(i);
>          Serial.println(i);
>        delay(1000);
> }
> 
> }

Does anybody have a clue?Preformatted text

One difference is the delay(1000)... One second. Why? There's no delay in the working pot version.

That the speed at wich it counts up or down...

In the pot version You loop on and on sending the mapped pot value.
I guess that in the typed, manual version You need to perform that ESC.write over and over during that second.
Make a functioin that loops for a second ESC.writing the value sent to the function. Call that function instead of doing ESC.write.

What does it do if instead of those very slow for loops you do simple write(100), write(90), write(80) with delays as needed?

Steve

The motor just spins at a constant speed.

WOW! Thank you this tottally worked. The ESC does need a constant stream of values being sent to work. I will have to figure out how to clean up this code, but this is great start. Here is code is used to quickly test the theory:

> 
> #include <Servo.h>
> Servo ESC;
> 
> void setup(){
> delay(2000);
> Serial.begin(9600);
> ESC.attach(3);
> delay(3000);
> 
> }
> 
> void loop() {
>   
>  
> 
>        for (int i=0;i<=2000;i++){ 
>      ESC.write(90);
>      Serial.println(90);
>      delay(1);
>        }
>        for (int i=0;i<=2000;i++){ 
>      ESC.write(89);
>      Serial.println(89);
>      delay(1);
>        }
>        for (int i=0;i<=2000;i++){ 
>      ESC.write(88);
>      Serial.println(88);
>      delay(1);
>        }
>        for (int i=0;i<=2000;i++){ 
>      ESC.write(87);
>      Serial.println(87);
>      delay(1);
>        }
>        for (int i=0;i<=3000;i++){ 
>      ESC.write(86);
>      Serial.println(86);
>      delay(1);
>        }
>        for (int i=0;i<=2000;i++){ 
>      ESC.write(85);
>      Serial.println(85);
>      delay(1);
>        }
>        for (int i=0;i<=2000;i++){ 
>      ESC.write(84);
>      Serial.println(84);
>      delay(1);
>        }
>        
>        for (int i=0;i<=2000;i++){ 
>      ESC.write(90);
>      Serial.println(90);
>      delay(1);
>        }
>        
>       for (int i=0;i<=2000;i++){ 
>      ESC.write(96);
>      Serial.println(96);
>      delay(1);
>        }
>        for (int i=0;i<=2000;i++){ 
>      ESC.write(95);
>      Serial.println(95);
>      delay(1);
>        }
>        for (int i=0;i<=2000;i++){ 
>      ESC.write(94);
>      Serial.println(94);
>      delay(1);
>        }
>        for (int i=0;i<=2000;i++){ 
>      ESC.write(93);
>      Serial.println(93);
>      delay(1);
>        }
>        for (int i=0;i<=2000;i++){ 
>      ESC.write(92);
>      Serial.println(92);
>      delay(1);
>        }
>        for (int i=0;i<=2000;i++){ 
>      ESC.write(91);
>      Serial.println(91);
>      delay(1);
>        }
> }

Great! Read my previous reply again. You can do the same using much less of code. Call this function from Your test for loops instead of the ESCwrite.

function kickESP(int val){ //val is the value sent to the ESP
unsigned long time = millis();// register the starting time of the one second timing.
while( millis() < time + 1000){
ESP.write(val);
delay(1);
}
}

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