How to stop to read the value from serial monitor? [SOLVED]

Hi guys, I’m newbie to this, I’ve got a small project:
I’ve got:

  1. Arduino Mega 2560
  2. Adafruit Motor Shield connected to Arduino board
  3. A toy motor and the 9V Battery connect.
    My project is to control the motor speed and spinning mode (forward, backward and release) by the computer. Here is my code:

#include <AFMotor.h>
AF_DCMotor motor(4);
char m; //m is the value for “mode”
int spd; //spd is for “speed”
void setup()
{
Serial.begin(9600);
Serial.println(“Control system ready!”);
//first condition setup
motor.setSpeed(200);
motor.run(RELEASE);
}
void loop()
{

if (Serial.available()==0) // check if the serial is input
{
Serial.println(“Initialize run mode:”);
char mode=Serial.read();} //adopting “m” from serial monitor

if (Serial.available()==0)
{
Serial.println(“Set speed”);
int spd=Serial.read();} //adopting “spd” from serial monitor
motor.setSpeed(spd); //set the motor speed
switch (mode){ //set the rotating state…
case ‘F’:
motor.run(FORWARD);
case ‘B’:
motor.run(BACKWARD);
default:
motor.run(RELEASE);
}
}

The program did not run the way I want, I want it to print “Initialize mode” and wait for me to input the keyword for “mode” and then ask me again to input the speed and then run the motor, but it continuing to print only “Initialize run mode” over and over again and don’t react with any value I input.
If anyone could solve this problem or know where is the infomation about this, please help me!
Thank you all.

if (Serial.available()>0)
{
  Serial.println("Initialize run mode:");
  char mode=Serial.read();  // <<<THIS "mode" . . . 
}
   
if (Serial.available()>0)
{
  Serial.println("Set speed");
  int spd=Serial.read();}
  motor.setSpeed(spd);
  switch (mode)           // <<< IS NOT THE SAME AS THIS "mode"
  {
    case 'F':
      motor.run(FORWARD);
    case 'B':
      motor.run(BACKWARD);
    default:
      motor.run(RELEASE);
  }
}

Please, please use code tags.

 if (Serial.available()>0)
  {
    Serial.println("Initialize run mode:");
    char mode=Serial.read();
  }

This code does not wait for any input.

Also the first line implies that the answer is available before the question is asked.

And, because the variable mode is defined within the braces it is lost as soon as the code exists the braces.

...R

ductruong253: If anyone could solve this problem or know where is the infomation about this, please help me!

you can do something like this:

long mySpeed;
boolean done, printed;
int state;

void setup() 
{
  Serial.begin(9600); 
}
void loop() 
{
  if (state == 0)
  {
    if (printed == false)
    {
      Serial.println("Enter Speed");
      printed = true;
    }
    if (Serial.available())
    { 
      mySpeed = Serial.parseInt();
      done = true;
    }
    if (done)
    {
      Serial.print(mySpeed);
      printed = false;
      done = false;
      state++;
    }
  }
  if (state == 1)
  {
    //process your stuff here
    //state = 0;
  }
}

To have the input parsing happen without holding up loop() requires a state-machine approach as shown - google "state machine" or checkout Nick Gammon's explanation here: http://www.gammon.com.au/forum/?id=12316

Thank you all, and sorry AWOL about that. I'll use code tags next time :)

I'll use code tags next time

You can use it this time. Click the modify tag on the top right hand corner of your post. Edit it and then save.

Grumpy_Mike: You can use it this time. Click the modify tag on the top right hand corner of your post. Edit it and then save.

Oh, ok, I did what you said :D

ductruong253:

Grumpy_Mike:
You can use it this time. Click the modify tag on the top right hand corner of your post. Edit it and then save.

Oh, ok, I did what you said :smiley:

?

BulldogLowell:
you can do something like this:

long mySpeed;

boolean done, printed;
int state;

void setup()
{
  Serial.begin(9600);
}
void loop()
{
  if (state == 0)
  {
    if (printed == false)
    {
      Serial.println(“Enter Speed”);
      printed = true;
    }
    if (Serial.available())
    {
      mySpeed = Serial.parseInt();
      done = true;
    }
    if (done)
    {
      Serial.print(mySpeed);
      printed = false;
      done = false;
      state++;
    }
  }
  if (state == 1)
  {
    //process your stuff here
    //state = 0;
  }
}

Thanks about your code, your code worked good! But I can only set one of state “SPEED” or “MODE”, I’ve try to got something base on yours but it’s still not work, here it is:

#include <AFMotor.h>
AF_DCMotor motor(4);
char m; //"m" is mode
int spd;
long mySpeed;
boolean doneM, doneS, printedM, printedS; //like "done" of BulldogLowell code, but one for mode, one for speed
int state;
void setup()
{
  Serial.begin(9600);
  Serial.println("Control system ready!");
  motor.setSpeed(200);
  motor.run(RELEASE);
}
void loop()
{
   if (state==0)
   {
     if (printedM==false)
     {
       Serial.println("Enter rotation mode:");
       printedM=true;
     }
     if (Serial.available())
     {
       m=Serial.parseInt();
       doneM=true;
     }
     if (doneM)
     {
       Serial.print(m);
       printedM=false;
       doneM=false;
       state++;
     }
     if (printedS==false)
     {
       Serial.println("Enter rotation speed:");
       printedS=true;
     }
     if (Serial.available())
     {
       spd=Serial.parseInt();
       doneS=true;
     }
     if (doneS)
     {
       Serial.print(spd);
       printedS=false;
       doneS=false;
       state++;
     }
     if (state==2)
     {
        motor.setSpeed(spd);
        switch (m){
        case 'F':
        {motor.run(FORWARD);}
        case 'B':
        {motor.run(BACKWARD);}
        default:
        {motor.run(RELEASE);}}
        state=0;
      }
  }
}

I still not really under stand your code so I can not explain anything except something on the top of the program.

make “Rotation Mode” and “speed” different states…

(uncompiled, untested)

#include <AFMotor.h>
AF_DCMotor motor(4);
char m; //"m" is mode
int spd;
long mySpeed;
boolean doneM, doneS, printedM, printedS; //like "done" of BulldogLowell code, but one for mode, one for speed
int state;
//
void setup()
{
  Serial.begin(9600);
  Serial.println("Control system ready!");
  motor.setSpeed(200);
  motor.run(RELEASE);
}
//
void loop()
{
  if (state==0)
  {
    if (printedM==false)
    {
      Serial.println("Enter rotation mode:");
      printedM=true;
    }
    if (Serial.available())
    {
      m=Serial.parseInt();
      doneM=true;
    }
    if (doneM)
    {
      Serial.print(m);
      printedM=false;
      doneM=false;
      state++;
    }
  }
  if (state == 1)
  {
    if (printedS==false)
    {
      Serial.println("Enter rotation speed:");
      printedS=true;
    }
    if (Serial.available())
    {
      spd=Serial.parseInt();
      doneS=true;
    }
    if (doneS)
    {
      Serial.print(spd);
      printedS=false;
      doneS=false;
      state++;
    }
  }
  if (state==2)
  {
    motor.setSpeed(spd);
    switch (m)
    {
    case 'F':
      {
        motor.run(FORWARD);
      }
    case 'B':
      {
        motor.run(BACKWARD);
      }
    default:
      {
        motor.run(RELEASE);
      }
    }
    state=0;
  }
}

I’ve got the way, thanks Mr. BulldogLowell a lots, the code is base on his, here it is:

#include <AFMotor.h>
AF_DCMotor motor(4);
long spd;
boolean done, printed;
int state;

void setup()      //setup first conditions
{
  Serial.begin(9600);
  motor.setSpeed(200);
  motor.run(RELEASE); 
}
void loop() 
{
  if (state == 0)
  {
    if (printed == false)
    {
      Serial.println("Enter Speed");
      printed = true;
    }
    if (Serial.available())
    { 
      spd = Serial.parseInt();   //input spd (speed of the motor)
      done = true;
    }
    if (done)
    {
      Serial.println(spd);   //output the value of spd
      printed = false;
      done = false;
      state++;
    }
  }
if (state == 1)
  {
    if (spd > 0)     // if the input number is higher than 0, the motor will spin clockwise
    {
      motor.setSpeed(abs(spd)); //set speed
      motor.run(FORWARD);
    }
    else if (spd < 0)  // if the input number is lower than 0, the motor will spin anticlockwise
    {
      motor.setSpeed(abs(spd)); //set speed
      motor.run(BACKWARD);
    }
    else motor.run(RELEASE);
    state = 0;
  }
}

Although it’s solved the problem, but it’s not the way as mr BulldogLowell said, however, thanks to everyone!