If else is not working

#include <AFMotor.h>
#define MAX_SPEED 190
#define MAX_SPEED_OFFSET 20
AF_DCMotor motor1(1,MOTOR12_1KHZ);
AF_DCMotor motor2(3,MOTOR12_1KHZ);
int speedSet = 0;
String input;

void setup() {
Serial.begin(9600);
  }
void loop() {
Serial.println(input);
if(input=="go"){
  motor1.run(FORWARD);
  motor2.run(FORWARD);
 for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2)
 {
  motor1.setSpeed(speedSet);
  motor2.setSpeed(speedSet+MAX_SPEED_OFFSET);
  delay(5);
 }
}
input="";
}

:frowning:

if(input=="go")

By what magic are you expecting this to become true?
The only place that changes input (or rather keeps it the same) is

input="";

else? What else? All I see is an if with a for loop inside it.

oqibidipo:

if(input=="go")

By what magic are you expecting this to become true?

Black magic (woman) :smiley:

input can be "go" by Serial.println(input); statement

But you never read the serial port. The line you give prints the input.

Still not working

#include <AFMotor.h>
#define MAX_SPEED 190
#define MAX_SPEED_OFFSET 20
AF_DCMotor motor1(1,MOTOR12_1KHZ);
AF_DCMotor motor2(3,MOTOR12_1KHZ);
int speedSet = 0;
String voice;

void setup() {
  Serial.begin(9600);
}

void loop() {
  if(Serial.available()) {
    delay(10);
    char c=Serial.read();
    if(c=='#'){
    voice += c;
}
  }
  Serial.println(voice);
  if (voice == "go"){
  motor1.run(FORWARD);
  motor2.run(FORWARD);
 for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2)
 {
  motor1.setSpeed(speedSet);
  motor2.setSpeed(speedSet+MAX_SPEED_OFFSET);
  delay(5);
   }
  
voice="";
}
}
    char c = Serial.read();
    if (c == '#')
    {
      voice += c;
    }

You only add characters to voice if the received character is #
Why ?

Hi Jashman,

I think that you'd like to send command like "go" or "stop" to your arduino so that your dc motor will move. Correct me if I've misunderstood you.

If what I think is correct then you must read you serial port. Your code doesn't do that.

input can be "go" by Serial.println(input); statement

Serial.println() function doesn't make it an input but an output instead.

Have a look and try the code below to read the serial data.

String command;
bool stringComplete = false;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  if(checkSerial()){
    Serial.print(command);
    
    //do your condition below inside the commented dashes
    //----------------------------------------------------
    
    if(command == "go"){
    
    }
    
    //----------------------------------------------------
    
    //reset the string and the boolean
    command = "";
    stringComplete = false;
  }
}

boolean checkSerial(){
  while(Serial.available()){
    //Read a character as it comes in:
    char c = (char)Serial.read();
    
    //add it to the command string:
    command += c;
    
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (c == '\n') {
      stringComplete = true;
      break;
    }
  }
  return stringComplete;
}

You have to use the serial monitor to send your command to the arduino. Take note on the serial baud rate and newline.

cheers,
joven

still not working

#include <AFMotor.h>
#define MAX_SPEED 190
#define MAX_SPEED_OFFSET 20
AF_DCMotor motor1(1,MOTOR12_1KHZ);
AF_DCMotor motor2(3,MOTOR12_1KHZ);
int speedSet = 0;
String command;
bool stringComplete = false;
void setup() {Serial.begin(9600);}
boolean checkSerial(){
  while(Serial.available()){
    //Read a character as it comes in:
    char c = (char)Serial.read();
    
    //add it to the command string:
    command += c;
    
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (c == '\n') {
      stringComplete = true;
      break;
    }
  }
  return stringComplete;
} 
void loop() {
  if(checkSerial()){
    Serial.print(command);
    
    //do your condition below inside the commented dashes
    //----------------------------------------------------
    
    if(command == "go"){
    motor1.run(FORWARD);
  motor2.run(FORWARD);
 for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2)
 {
  motor1.setSpeed(speedSet);
  motor2.setSpeed(speedSet+MAX_SPEED_OFFSET);
  delay(5);
    }
    
    //----------------------------------------------------
    
    //reset the string and the boolean
    command = "";
    stringComplete = false;
  }
}
}

You are adding the newline to the String. Could that be a problem ?
Try using

  if(checkSerial()){
  Serial.print(">");  
  Serial.print(command);
  Serial.println("<");

What do you see ?

not working

Jashman:
not working

Are you likely to expend any effort in rectifying that situation?

Try this.

String readString;

void loop() {
  while (Serial.available()) {
    delay(3);
    char c = Serial.read();              // When I type something in my mobile phone the bluetooth will read it and print it on the LCD
    readString += c;
  }

    if (readString == "go") {
      .....
}

    if (readString == "stop") {
      ...
}

AWOL:
Are you likely to expend any effort in rectifying that situation?

I am asking for help after trying my best

Jashman:
not working

Not helpful
Do you see the > and < ?

If not then your program is never reaching that part of the code.

Then your best isn't really that much, is it.... If you edit your code, for whatever reason, post it here again.

And never ever say "(it's) not working". Explain it into detail because we can't look over your shoulder. For all we now your computer isn't plugged in...

So, help others to help you.
Just repeating "not working", without any observations of what it is doing, helps no-one.

akatchi:
Try this.

String readString;

void loop() {
  while (Serial.available()) {
  delay(3);
  char c = Serial.read(); // When I type something in my mobile phone the bluetooth will read it and print it on the LCD
  readString += c;
  }

if (readString == "go") {
  .....
}

if (readString == "stop") {
  ...
}

It is working Thank you so much
It was my project for Science Exhibition in my school tomorrow and it wasn't working

Jashman:
It was my project for Science Exhibition in my school tomorrow and it wasn't working

Then lesson learned, start earlier :wink: