Hi I am making an Android App to control a robot using arduino mega.
the problem i am facing is when I touch the button, robot only moves for a second or so. I have to repetitively touch the button for it to continuously move.
Is there a command I need to use for it to continuously move as long as I am touching the button?
Arduino Code I am using is
//MOTOR pins definition
#define E1 2 // Enable Pin for motor 1
#define E2 7 // Enable Pin for motor 2
#define I1 3 // Control pin 1 for motor 1
#define I2 4 // Control pin 2 for motor 1
#define I3 5 // Control pin 1 for motor 2
#define I4 6 // Control pin 2 for motor 2
//Motor Pins definition ends here
// LED pin attached to Arduino D13
int LED = 13;
char command = 'S';
char prevCommand = 'A';
int velocity = 0;
unsigned long timer0 = 2000; //Stores the time (in millis since execution started)
unsigned long timer1 = 0; //Stores the time when the last command was received from the phone
void setup()
{
Serial.begin(9600); //Set the baud rate to that of your Bluetooth module.
pinMode(LED , OUTPUT);
//Setting input/output for Motor Pins
pinMode(E1, OUTPUT);
pinMode(E2, OUTPUT);
pinMode(I1, OUTPUT);
pinMode(I2, OUTPUT);
pinMode(I3, OUTPUT);
pinMode(I4, OUTPUT);
//Setting input/output for Motor Pins ends here
}
void loop(){
if(Serial.available() > 0){
timer1 = millis();
prevCommand = command;
command = Serial.read();
//Change pin mode only if new command is different from previous.
if(command!=prevCommand){
//Serial.println(command);
switch(command){
case 'F':
GoForward();
break;
case 'B':
GoBack();
break;
case 'L':
TurnLeft();
break;
case 'R':
TurnRight();
break;
case 'S':
StopMotor();
break;
case 'I': //FR
ForwardRight();
break;
case 'J': //BR
BackRight();
break;
case 'G': //FL
ForwardLeft();
break;
case 'H': //BL
ForwardRight();
break;
case 'W': //Light ON
digitalWrite(LED, HIGH);
break;
case 'w': //Font OFF
digitalWrite(Light, LOW);
break;
}
}
}
else{
timer0 = millis(); //Get the current time (millis since execution started).
//Check if it has been 500ms since we received last command.
if((timer0 - timer1)>500){
//More tan 500ms have passed since last command received, car is out of range.
//Therefore stop the car and turn lights off.
digitalWrite(LED, LOW);
StopMotor();
}
}
}
void GoBack()
{
digitalWrite(E1, HIGH);
digitalWrite(E2, HIGH);
digitalWrite(I1, LOW);
digitalWrite(I2, HIGH); //This will set the circuitry of the H-bridge appropriately (e.g. the gates)
digitalWrite(I3, LOW);
digitalWrite(I4, HIGH);
}
void GoForward()
{
analogWrite(E1, 255);
analogWrite(E2, 255);
digitalWrite(I1, HIGH);
digitalWrite(I2, LOW); //This will set the circuitry of the H-bridge appropriately (e.g. the gates)
digitalWrite(I3, HIGH);
digitalWrite(I4, LOW);
}
void TurnLeft()
{
digitalWrite(E1, HIGH);
digitalWrite(E2, HIGH);
digitalWrite(I1, HIGH);
digitalWrite(I2, LOW); //This will set the circuitry of the H-bridge appropriately (e.g. the gates)
digitalWrite(I3, LOW);
digitalWrite(I4, LOW);
}
void ForwardLeft()
{
analogWrite(E1, 255);
analogWrite(E2, 255);
digitalWrite(I1, HIGH);
digitalWrite(I2, LOW); //This will set the circuitry of the H-bridge appropriately (e.g. the gates)
digitalWrite(I3, LOW);
digitalWrite(I4, HIGH);
}
void BackLeft()
{
analogWrite(E1, 255);
analogWrite(E2, 255);
digitalWrite(I1, LOW);
digitalWrite(I2, HIGH); //This will set the circuitry of the H-bridge appropriately (e.g. the gates)
digitalWrite(I3, HIGH);
digitalWrite(I4, LOW);
}
void TurnRight()
{
digitalWrite(E1, HIGH );
digitalWrite(E2, HIGH);
digitalWrite(I1, LOW);
digitalWrite(I2, LOW); //This will set the circuitry of the H-bridge appropriately (e.g. the gates)
digitalWrite(I3, HIGH);
digitalWrite(I4, LOW);
}
void ForwardRight()
{
analogWrite(E1, 255);
analogWrite(E2, 255);
digitalWrite(I1, LOW);
digitalWrite(I2, HIGH); //This will set the circuitry of the H-bridge appropriately (e.g. the gates)
digitalWrite(I3, HIGH);
digitalWrite(I4, LOW);
}
void BackRight()
{
analogWrite(E1, 255);
analogWrite(E2, 255);
digitalWrite(I1, HIGH);
digitalWrite(I2, LOW); //This will set the circuitry of the H-bridge appropriately (e.g. the gates)
digitalWrite(I3, LOW);
digitalWrite(I4, HIGH);
}
void StopMotor()
{
digitalWrite(E1, HIGH );
digitalWrite(E2, HIGH);
digitalWrite(I1, LOW);
digitalWrite(I2, LOW); //This will set the circuitry of the H-bridge appropriately (e.g. the gates)
digitalWrite(I3, LOW);
digitalWrite(I4, LOW);
}
Any help is much appreciated!!