Problem between ESC and Serial

Dear all,

My code is below:

#include <Servo.h>
Servo ESC;
boolean serialed = false;

void setup(){
  delay(4000);
  ESC.attach(0);
  armESC(); delay(1500);
}

void loop(){
  ESC.write(50);delay(3000);
  if(!serialed){serialed=true;Serial.begin(38400);while(!Serial);Serial.println("initializing");}
}

void armESC(){
  for(int i=0; i<1000; i++)
  {
    ESC.write(45);
    delay(10);
  }
}

When I upload the code, ESC (motor) arms as well, moreover starts with low speed. The problem is when I started to initial the Serial then motor stops. The board is UNO. My aim is to get values from user by Serial and apply to motor. But I simplyfied it just running the Serial that’s all to meet the problem. Anyway, there is no for Serial, there is a problem on ESC that’s what I understand. Could you help?

void setup(){
  delay(4000);
  ESC.attach(0);
  armESC(); delay(1500);
}

Why are you waiting 4 seconds before the ESC.attach()?
Why are you waiting 1.5 seconds after armESC?

void armESC(){
  for(int i=0; i<1000; i++)
  {
    ESC.write(45);
    delay(10);
  }

I realize you probably want to wait for the motor (you said motor, so I assume 45 is not a servo angle) to get up to speed, but you only need to send the speed once. You set it 1000 times, taking 10 seconds to do so.

void loop(){
  ESC.write(50);delay(3000);
  if(!serialed){serialed=true;Serial.begin(38400);while(!Serial);Serial.println("initializing");}
}

Now you set another speed (or angle?), wait 3 more seconds, then initialize Serial. It will always Initialize the first time through the loop. You don’t have any code to stop it.

Are you saying that the motor stops at that point? Is it actually a motor or is it a limited rotation servo?

What happens if you initialize Serial BEFORE arming and writing to ESC, like if you put the Serial.begin()? in setup()?

Servo test code you can use for sending various servo commands to your ESC via the serial monitor.

// zoomkat 10-22-11 serial servo test
// type servo position 0 to 180 in serial monitor
// or for writeMicroseconds, use a value like 1500
// for IDE 0022 and later
// Powering a servo from the arduino usually *DOES NOT WORK*.

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

void setup() {
  Serial.begin(9600);
  myservo.writeMicroseconds(1500); //set initial servo position if desired
  myservo.attach(7, 500, 2500);  //the pin for the servo control, and range if desired
  Serial.println("servo-test-22-dual-input"); // so I can keep track of what is loaded
}

void loop() {
  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the string readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured string 
    int n = readString.toInt();  //convert readString into a number

    // auto select appropriate value, copied from someone elses code.
    if(n >= 500)
    {
      Serial.print("writing Microseconds: ");
      Serial.println(n);
      myservo.writeMicroseconds(n);
    }
    else
    {   
      Serial.print("writing Angle: ");
      Serial.println(n);
      myservo.write(n);
    }

    readString=""; //empty for next input
  } 
}

@zoomkat thank you so much that properly working code. I am running on your code. Thanks a lot...
@lar3ry I tried your asked ways. But it was pointless. Anyway, thank you because of your focus.

  ESC.attach(0);

Attaching the servo to pin 0 which is used by serial is not a very good idea.

Mark