CODE
#include <Servo.h>
byte servoPin = 6; // signal pin for the ESC.
byte potentiometerPin = A0; // analog input pin for the potentiometer.
Servo servo;
// Define the number of samples to keep track of. The higher the number, the
// more the readings will be smoothed, but the slower the output will respond to
// the input. Using a constant rather than a normal variable lets us use this
// value to determine the size of the readings array.
const int numReadings = 10;
int readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
int inputPin = A0;
const int led =7; // GPIO 7 for the LED
void setup() {
servo.attach(servoPin);
servo.writeMicroseconds(1500); // send "stop" signal to ESC. Also necessary to arm the ESC.
// initialize serial communication with computer:
Serial.begin(9600);
pinMode(led,OUTPUT); // define LED as an output
// initialize all the readings to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
}
void loop() {
// subtract the last reading:
total = total - readings[readIndex];
// read from the sensor:
readings[readIndex] = analogRead(inputPin);
// add the reading to the total:
total = total + readings[readIndex];
// advance to the next position in the array:
readIndex = readIndex + 1;
// if we're at the end of the array...
if (readIndex >= numReadings) {
// ...wrap around to the beginning:
readIndex = 0;
}
// calculate the average:
average = total / numReadings;
// send it to the computer as ASCII digits
int val = map(average, 0, 1023, 1000, 2000);
Serial.println(val);
delay(1); // delay in between reads for stability
servo.writeMicroseconds(val); // Send signal to ESC.
if (val>=1450&&val<=1500) { // and the status flag is LOW
digitalWrite(led,HIGH); // and turn on the LED
}
else {
digitalWrite(led,LOW);
}
}
Problem
The flow cannot go from brake to reverse. I need to do from brake (1500 ) to max reverse (1000) to brake (1500) again, after then the motor can do reverse (1450 to 1000).
So the problem of my flow right now cannot go reverse from brake. Need to turn potentiometer to maximum reverse first then back to brake, after that can run reverse smoothly.
Brake > Max Reverse (motor not running) > Brake > reverse to max (reverse can run)
I want to do Brake > Reverse (motor can run). Anyone know how to solve it?
Position of potentiometer
1500 - 2000 = Forward rotation
1500-1450 = Brake
1450 - 1000 = Reverse rotation