Hi there.
I am having problems with my sketch. When I disconnect my android app or the bluetooth connection my motors go to full speed without turning of. I have switch the battery of for it to stop. Does anyone have any ideas for me please. Here is my sketch and thanks for the help.
#define MOTOR_PIN 9
int _pulseTime = 0; //microseconds
boolean _highSpeed = true;
void setup()
{
Serial.begin(9600);
pinMode(9, OUTPUT);
digitalWrite(9, LOW);
Serial.println("Enter 0-8 to adjust motor speed.");
Serial.println("Enter h or l to change the speed range.");
}
void loop()
{
static int timing[] = {1000, 1050, 1100, 1150, 1200, 1250, 1300, 1350, 1400, 1450, 1500, 1550, 1600, 1650, 1700, 1750, 1800, 1850, 1900, 1950, 2000 };
// Pulse servo
digitalWrite(9, HIGH);
delayMicroseconds(_pulseTime);
digitalWrite(9, LOW);
int incomingByte = 0;
if (Serial.available() > 0)
{
incomingByte = Serial.read();
int index = incomingByte - 48;
if (index >= 0 && index < sizeof(timing) / sizeof(int))
{
setTimingParams(timing[index]);
Serial.print("Throttle: ");
Serial.print((_pulseTime - 1100) / 8);
Serial.println("%");
}
else
{
if (incomingByte == 'h')
{
_highSpeed = true;
}
else if (incomingByte == 'l')
{
_highSpeed = false;
}
}
}
// OFF time
delay(20);
}
void setTimingParams(int newPulseTimeVal) {
if (_highSpeed) {
_pulseTime = newPulseTimeVal;
}
else {
_pulseTime = 1025 + (newPulseTimeVal / 10);
}
}