I am having difficulty programming my Arduino MEGA 2560 to control the PWM signal sent to my ESC to control my 770Kv motor. So far I have been using serial monitor and commanding different PWM values but after I command a certain value, the PWM returns to 0 and the throttle is not sustained, I have tried building an array of all commanded PWM values and if the serial signal is not available the code would refer back to the most recent PWM value and send it to the ESC but that was not a reliable method. Any help would be greatly appreciated. The current code is below.
ESC.attach(9,1000,2000); //PWM Pin Connection Port on Arduino
ESC.write(0); //Activates ESC
delay(5000); //Wait time before commanding throttle values
while(Serial.available() > 0)
int val = Serial.parseInt(); //reads integer vals from serial monitor input
Serial.println(val); //Print throttle value
ESC.write(val); //Write throttle value to ESC