Wait for input from Serial

Hello All,

I am having trouble with my code, I am trying to control two motors with a serial monitor input, however, for some reason, the Arduino does not wait for any input from the monitor and ignores all the “IF” statements. and just continues to run through the code. not sure what is going on.

please help!

Thank you in advance,

Gijo

gauge_alignment_two_axis.ino (3.61 KB)

This:

    if (ch == 'p' || 'P')

should be:

    if (ch == 'p' || ch== 'P')

Same error again a few lines below.

gvarghese:
I am having trouble with my code,

I am having trouble with your code - I can't see it.

Wildbill,

now its returning an error "Error compiling for board Arduino/Genuino UNO" does this not work for the UNO?

AWOL,

I have an attachment attached, I am not sure how to add the line of code directly into the thread.

Thank you

Gijo

Always post the latest version of your code when you make changes.

How to properly post code. Post the complete error message. Use the "copy error message" button and paste the error message into a reply.

Arduino: 1.8.7 (Windows Store 1.8.15.0) (Windows 10), Board: "Arduino/Genuino Uno"

C:\Users\gvarghese\Google Drive\Google OFS\Tension Gauge\PID_control_Trial3\tensin_gauge_alignment_two_axis\gauge_alignment_two_axis.ino:1:5: error: redefinition of 'int PULC'

 int PULC=7; //define Pulse pin

     ^

C:\Users\gvarghese\Google Drive\Google OFS\Tension Gauge\PID_control_Trial3\tensin_gauge_alignment_two_axis\tensin_gauge_alignment_two_axis.ino:1:5: note: 'int PULC' previously defined here

 int PULC=7; //define Pulse pin

     ^

C:\Users\gvarghese\Google Drive\Google OFS\Tension Gauge\PID_control_Trial3\tensin_gauge_alignment_two_axis\gauge_alignment_two_axis.ino:2:5: error: redefinition of 'int DIRC'

 int DIRC=6; //define Direction pin

     ^

C:\Users\gvarghese\Google Drive\Google OFS\Tension Gauge\PID_control_Trial3\tensin_gauge_alignment_two_axis\tensin_gauge_alignment_two_axis.ino:2:5: note: 'int DIRC' previously defined here

 int DIRC=6; //define Direction pin

     ^

C:\Users\gvarghese\Google Drive\Google OFS\Tension Gauge\PID_control_Trial3\tensin_gauge_alignment_two_axis\gauge_alignment_two_axis.ino:3:5: error: redefinition of 'int ENAC'

 int ENAC=5; //define Enable 

     ^

C:\Users\gvarghese\Google Drive\Google OFS\Tension Gauge\PID_control_Trial3\tensin_gauge_alignment_two_axis\tensin_gauge_alignment_two_axis.ino:3:5: note: 'int ENAC' previously defined here

 int ENAC=5; //define Enable 

     ^

C:\Users\gvarghese\Google Drive\Google OFS\Tension Gauge\PID_control_Trial3\tensin_gauge_alignment_two_axis\gauge_alignment_two_axis.ino:4:5: error: redefinition of 'int nC'

 int nC = 0;

     ^

C:\Users\gvarghese\Google Drive\Google OFS\Tension Gauge\PID_control_Trial3\tensin_gauge_alignment_two_axis\tensin_gauge_alignment_two_axis.ino:4:5: note: 'int nC' previously defined here

 int nC = 0;

     ^

C:\Users\gvarghese\Google Drive\Google OFS\Tension Gauge\PID_control_Trial3\tensin_gauge_alignment_two_axis\gauge_alignment_two_axis.ino:6:5: error: redefinition of 'int PULP'

 int PULP=2; //define Pulse pin

     ^

C:\Users\gvarghese\Google Drive\Google OFS\Tension Gauge\PID_control_Trial3\tensin_gauge_alignment_two_axis\tensin_gauge_alignment_two_axis.ino:6:5: note: 'int PULP' previously defined here

 int PULP=2; //define Pulse pin

     ^

C:\Users\gvarghese\Google Drive\Google OFS\Tension Gauge\PID_control_Trial3\tensin_gauge_alignment_two_axis\gauge_alignment_two_axis.ino:7:5: error: redefinition of 'int DIRP'

 int DIRP=3; //define Direction pin

     ^

C:\Users\gvarghese\Google Drive\Google OFS\Tension Gauge\PID_control_Trial3\tensin_gauge_alignment_two_axis\tensin_gauge_alignment_two_axis.ino:7:5: note: 'int DIRP' previously defined here

 int DIRP=3; //define Direction pin

     ^

C:\Users\gvarghese\Google Drive\Google OFS\Tension Gauge\PID_control_Trial3\tensin_gauge_alignment_two_axis\gauge_alignment_two_axis.ino:8:5: error: redefinition of 'int ENAP'

 int ENAP=4; //define Enable 

     ^

C:\Users\gvarghese\Google Drive\Google OFS\Tension Gauge\PID_control_Trial3\tensin_gauge_alignment_two_axis\tensin_gauge_alignment_two_axis.ino:8:5: note: 'int ENAP' previously defined here

 int ENAP=4; //define Enable 

     ^

C:\Users\gvarghese\Google Drive\Google OFS\Tension Gauge\PID_control_Trial3\tensin_gauge_alignment_two_axis\gauge_alignment_two_axis.ino:9:5: error: redefinition of 'int nP'

 int nP = 5;

     ^

C:\Users\gvarghese\Google Drive\Google OFS\Tension Gauge\PID_control_Trial3\tensin_gauge_alignment_two_axis\tensin_gauge_alignment_two_axis.ino:9:5: note: 'int nP' previously defined here

 int nP = 5;

     ^

C:\Users\gvarghese\Google Drive\Google OFS\Tension Gauge\PID_control_Trial3\tensin_gauge_alignment_two_axis\gauge_alignment_two_axis.ino:11:8: error: redefinition of 'String inString'

 String inString = "";    // string to hold input

        ^

C:\Users\gvarghese\Google Drive\Google OFS\Tension Gauge\PID_control_Trial3\tensin_gauge_alignment_two_axis\tensin_gauge_alignment_two_axis.ino:11:8: note: 'String inString' previously declared here

 String inString = "";    // string to hold input

        ^

C:\Users\gvarghese\Google Drive\Google OFS\Tension Gauge\PID_control_Trial3\tensin_gauge_alignment_two_axis\gauge_alignment_two_axis.ino:13:5: error: redefinition of 'int pos'

 int pos;

     ^

C:\Users\gvarghese\Google Drive\Google OFS\Tension Gauge\PID_control_Trial3\tensin_gauge_alignment_two_axis\tensin_gauge_alignment_two_axis.ino:13:5: note: 'int pos' previously declared here

 int pos;

     ^

C:\Users\gvarghese\Google Drive\Google OFS\Tension Gauge\PID_control_Trial3\tensin_gauge_alignment_two_axis\gauge_alignment_two_axis.ino: In function 'void setup()':

C:\Users\gvarghese\Google Drive\Google OFS\Tension Gauge\PID_control_Trial3\tensin_gauge_alignment_two_axis\gauge_alignment_two_axis.ino:15:6: error: redefinition of 'void setup()'

 void setup() 

      ^

C:\Users\gvarghese\Google Drive\Google OFS\Tension Gauge\PID_control_Trial3\tensin_gauge_alignment_two_axis\tensin_gauge_alignment_two_axis.ino:15:6: note: 'void setup()' previously defined here

 void setup() 

      ^

C:\Users\gvarghese\Google Drive\Google OFS\Tension Gauge\PID_control_Trial3\tensin_gauge_alignment_two_axis\gauge_alignment_two_axis.ino: In function 'void loop()':

C:\Users\gvarghese\Google Drive\Google OFS\Tension Gauge\PID_control_Trial3\tensin_gauge_alignment_two_axis\gauge_alignment_two_axis.ino:29:6: error: redefinition of 'void loop()'

 void loop()

      ^

C:\Users\gvarghese\Google Drive\Google OFS\Tension Gauge\PID_control_Trial3\tensin_gauge_alignment_two_axis\tensin_gauge_alignment_two_axis.ino:29:6: note: 'void loop()' previously defined here

 void loop()

      ^

C:\Users\gvarghese\Google Drive\Google OFS\Tension Gauge\PID_control_Trial3\tensin_gauge_alignment_two_axis\gauge_alignment_two_axis.ino: In function 'void moveincenter()':

C:\Users\gvarghese\Google Drive\Google OFS\Tension Gauge\PID_control_Trial3\tensin_gauge_alignment_two_axis\gauge_alignment_two_axis.ino:60:6: error: redefinition of 'void moveincenter()'

 void moveincenter()

      ^

C:\Users\gvarghese\Google Drive\Google OFS\Tension Gauge\PID_control_Trial3\tensin_gauge_alignment_two_axis\tensin_gauge_alignment_two_axis.ino:55:6: note: 'void moveincenter()' previously defined here

 void moveincenter()

      ^

C:\Users\gvarghese\Google Drive\Google OFS\Tension Gauge\PID_control_Trial3\tensin_gauge_alignment_two_axis\gauge_alignment_two_axis.ino: In function 'void moveinpower()':

C:\Users\gvarghese\Google Drive\Google OFS\Tension Gauge\PID_control_Trial3\tensin_gauge_alignment_two_axis\gauge_alignment_two_axis.ino:109:6: error: redefinition of 'void moveinpower()'

 void moveinpower()

      ^

C:\Users\gvarghese\Google Drive\Google OFS\Tension Gauge\PID_control_Trial3\tensin_gauge_alignment_two_axis\tensin_gauge_alignment_two_axis.ino:104:6: note: 'void moveinpower()' previously defined here

 void moveinpower()

      ^

exit status 1
Error compiling for board Arduino/Genuino Uno.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

It looks like your changes were more involved than the couple of lines I suggested. If I compile a version of your code with those changes, all I get is two similar warnings:

warning: suggest parentheses around assignment used as truth value [-Wparentheses]
   if (pos = 0)

Which is telling you cryptically to use == where you have used =

int PULC = 7; //define Pulse pin
int DIRC = 6; //define Direction pin
int ENAC = 5; //define Enable 
int nC = 0;

int PULP = 2; //define Pulse pin
int DIRP = 3; //define Direction pin
int ENAP = 4; //define Enable 
int nP = 5;

String inString = "";    // string to hold input

int pos;

void setup() 
{
  Serial.begin(9600);
//  portOne.begin
  
  pinMode (PULC, OUTPUT);
  pinMode (DIRC, OUTPUT);
  pinMode (ENAC, OUTPUT);

  pinMode (PULP, OUTPUT);
  pinMode (DIRP, OUTPUT);
  pinMode (ENAP, OUTPUT);

}
void loop()
{
    if (Serial.available()>0)
    {
      Serial.print("Which direction are you trying to move ?");
      Serial.println(" Left/Right(P) OR Front/Back(C)? ");
      char ch = Serial.read();
      delay(50);        // some delay 
      Serial.print("Your have entered ");
      Serial.println(ch);       // for debugging
      if (ch == 'p'|| ch == 'P')
      {
        moveinpower();
      }
      if (ch == 'c'|| ch == 'C')
      {
        moveincenter();
      }
      else
      {
        Serial.print("INCORRECT INPUT!, YOU CAN ONLY CHOOSE BETWEEN 'c' OR 'P'");
        delay(50);
      }
    }           
}

void moveincenter()
{
  if (Serial.available()>0)
    {
       Serial.print("What is your center value ?");
       pos=Serial.parseInt();
       Serial.println(pos);   // for debugging   
    }

    // n=800;
    nP = map(pos, -50, 50, -2000, 2000);
    nP = abs(nP);
    Serial.print("nP: ");  
    Serial.println(nP);        // for debugging
   
    if(pos<0)
    {
      for (int i=0; i<nP; i++)    //Forward 5000 steps
          {
              digitalWrite(DIRC,HIGH);
              digitalWrite(ENAC,HIGH);
              digitalWrite(PULC,HIGH);
              delayMicroseconds(50);
              digitalWrite(PULC,LOW);
              digitalWrite(ENAC,LOW);
              delayMicroseconds(50);
          }
    }
     if(pos>0)
    {
      for (int i=0; i<nP; i++)    //Forward 5000 steps
          {
              digitalWrite(DIRC,LOW);
              digitalWrite(ENAC,HIGH);
              digitalWrite(PULC,HIGH);
              delayMicroseconds(50);
              digitalWrite(PULC,LOW);
              digitalWrite(ENAC,LOW);
              delayMicroseconds(50);
          }
    }
   if (pos=0)
       {
        digitalWrite(ENAC,LOW);
        digitalWrite(PULC, LOW);
       }
  delay(500);
}

void moveinpower()
{
  if (Serial.available()>0)
    {
       Serial.print("What is your power value ?");
       pos=Serial.parseInt();
       Serial.println(pos);   // for debugging   
    }

    // n=800;
    nP = map(pos, -50, 50, -2000, 2000);
    nP = abs(nP);
    Serial.print("nC: ");  
    Serial.println(nP);        // for debugging
   
    if(pos<0)
    {
      for (int i=0; i<nP; i++)    //Forward 5000 steps
          {
              digitalWrite(DIRP,HIGH);
              digitalWrite(ENAP,HIGH);
              digitalWrite(PULP,HIGH);
              delayMicroseconds(50);
              digitalWrite(PULP,LOW);
              digitalWrite(ENAP,LOW);
              delayMicroseconds(50);
          }
    }
     if(pos>0)
    {
      for (int i=0; i<nP; i++)    //Forward 5000 steps
          {
              digitalWrite(DIRP,LOW);
              digitalWrite(ENAP,HIGH);
              digitalWrite(PULP,HIGH);
              delayMicroseconds(50);
              digitalWrite(PULP,LOW);
              digitalWrite(ENAP,LOW);
              delayMicroseconds(50);
          }
    }
   if (pos=0)
       {
        digitalWrite(ENAP,LOW);
        digitalWrite(PULP, LOW);
       }
  delay(500);
}

So now everything is uploading, however, the logic isn’t quite working the way I want it too. after it goes into either one of the void functions, it still does not wait for the user to enter in a value. it immediately jumps out of the function and back to the void loop(), and executes the else statement.

Hope I am describing my issue properly…

Thank you

Gijo

after it goes into either one of the void functions, it still does not wait for the user to enter in a value

First ALL the functions in your code are void functions, so "either" isn't really appropriate.

Second, there isn't any code in your move functions to cause the code to wait for input.

Please remember to use code tags when posting code.

I thought the Serial.read(); would tell the Arduino to stop and wait for an input from the serial?

gvarghese:
I thought the Serial.read(); would tell the Arduino to stop and wait for an input from the serial?

No, the reference is quite clear - the function returns the next character from the input buffer, or -1.

AWOL:
No, the reference is quite clear - the function returns the next character from the input buffer, or -1.

Because you almost never want to stop the whole processor just in anticipation of a serial - or any other - event.

If you really must stop the processor from anything while you wait for input (if your code is designed properly this is normally not necessary; doing it like this is a sign of poor coding), use:

while (Serial.available() == 0) {}