Go Down

Topic: Any code not works for me (Read 487 times) previous topic - next topic

Rekimo

Motor: Rocket F540 V2 4370KV
ESC: Readytosky Waterproof 60A(https://www.aliexpress.com/item/Readytosky-Waterproof-60A-Brushless-ESC-Electric-Speed-Controller-with-5-5V-3A-BEC-for-1-10/32864211143.html)

Code: [Select]
#include <Servo.h>
#define MAX_SIGNAL 2000
#define MIN_SIGNAL 700
#define MOTOR_PIN 9
int value = 0;

Servo motor;

void setup() {
  Serial.begin(9600);
  Serial.println("START");
  Serial.println("ESC is calibrating");

  motor.attach(MOTOR_PIN);
  motor.writeMicroseconds(MAX_SIGNAL);
 
  while (!Serial.available());
  Serial.read();
  motor.writeMicroseconds(MIN_SIGNAL);
  Serial.println("ESC calibrated");
  Serial.println("Speed: ");

}

void loop() { 
    value = Serial.read();
    motor.writeMicroseconds(value);

}


and motor does not move

larryd

Look at one of the examples that come with the library.

No technical PMs.
The last thing you did is where you should start looking.

groundFungus

Code: [Select]
void loop() {
    value = Serial.read();
    motor.writeMicroseconds(value);
}


You are reading from the serial input buffer even if there is no data available so most of the values returned are -1.  To see how to read a number from Serial, reliably, see example #4 of the serial input basics tutorial.   To change a number that is received as text to a number that can be used with servo.write, use the atoi() function.

Rekimo

Code: [Select]
#include <Servo.h>

#define MAX_SIGNAL 2000
#define MIN_SIGNAL 700
#define MOTOR_PIN A0

int value = 0;

const byte numChars = 32;
char receivedChars[numChars];

boolean newData = false;

Servo motor;

void setup() {
  Serial.begin(9600);
  Serial.println("START");
  Serial.println("ESC is calibrating");

  motor.attach(MOTOR_PIN);
  motor.writeMicroseconds(MAX_SIGNAL);
  motor.writeMicroseconds(MIN_SIGNAL);
 
  Serial.println("ESC calibrated");
  Serial.println("Speed: ");
}

void loop() {
    recvWithEndMarker();

    value = atoi(receivedChars);
    motor.writeMicroseconds(value);

    Serial.println(value);
}

void recvWithEndMarker() {
    static byte ndx = 0;
    char endMarker = '\n';
    char rc;
   
    if (Serial.available() > 0) {
        rc = Serial.read();

        if (rc != endMarker) {
            receivedChars[ndx] = rc;
            ndx++;
            if (ndx >= numChars) {
                ndx = numChars - 1;
            }
        }
        else {
            receivedChars[ndx] = '\0'; // terminate the string
            ndx = 0;
            newData = true;
        }
    }
}


still motor not started

groundFungus

#4
Jan 24, 2019, 03:06 pm Last Edit: Jan 24, 2019, 03:10 pm by groundFungus
Try this code and post the serial monitor output, please.
Code: [Select]
#include <Servo.h>

#define MAX_SIGNAL 2000
#define MIN_SIGNAL 700
#define MOTOR_PIN A0

int value = 0;

const byte numChars = 32;
char receivedChars[numChars];

boolean newData = false;

Servo motor;

void setup()
{
   Serial.begin(9600);
   Serial.println("START");
   Serial.println("ESC is calibrating");

   motor.attach(MOTOR_PIN);
   motor.writeMicroseconds(MAX_SIGNAL);
   motor.writeMicroseconds(MIN_SIGNAL);

   Serial.println("ESC calibrated");
   Serial.println("Speed: ");
}

void loop()
{
   recvWithEndMarker();
   if(newData == true)
   {
   Serial.print("received characters  ");
   Serial.println(receivedChars);
   value = atoi(receivedChars);
   motor.writeMicroseconds(value);
   Serial.print("converted value  ");
   Serial.println(value);
   newData = false;
   }
}

void recvWithEndMarker()
{
   static byte ndx = 0;
   char endMarker = '\n';
   char rc;

   if (Serial.available() > 0)
   {
      rc = Serial.read();

      if (rc != endMarker)
      {
         receivedChars[ndx] = rc;
         ndx++;
         if (ndx >= numChars)
         {
            ndx = numChars - 1;
         }
      }
      else
      {
         receivedChars[ndx] = '\0'; // terminate the string
         ndx = 0;
         newData = true;
      }
   }
}


Make sure that line endings is set to newline in serial monitor.


Rekimo

sth is maybe wrong or idk ... motor running after time ... 1 min or more and he stops after that ... and running again after seconds and stops again and not running anymore

Rekimo

unfortunately, nothing is happening right now :smiley-confuse: yt
i want only left .. right and braking

groundFungus

Are you sure that you are performing a proper ESC arming sequence?

Rekimo

No now i'm not sure... I tested many code and anyone not works

Rekimo

1500 is minimum... 1600 = start
https://youtu.be/1MP-YU6dDDc

groundFungus

Do you have a manual for the ESC?  I searched but can't find one. 

slipstick

sth is maybe wrong or idk ... motor running after time ... 1 min or more and he stops after that ... and running again after seconds and stops again and not running anymore
How is everything powered? That sounds like a bad battery or one that needs charging.

And your arming sequence looks more like a way of putting the ESC into programming mode,which is not what you want. What sounds do you hear from the ESC/motor?

Can you try it out using the Knob example program to see what values it really does respond to?

Steve

Rekimo


Rekimo

https://s15.directupload.net/images/190124/45lx3w6o.jpg

https://s15.directupload.net/images/190124/rbqd9y33.jpg

https://s15.directupload.net/images/190124/5bqblzt5.jpg

Rekimo

Battery: zippy compact 2200mah 3s 25c lipo pack

Go Up