problems with Serial.available and Serial.parseInt()

Ok. So i have my program, and in the setup i have it where it asks me for two values. One is the amount of steps to take, and the second is the speed involved with a separate function my code. when i start everything up, it asks for the first value and it saves it and it works. But the 2nd value doesn’t wait for me to enter a value and automatically goes to 0. Which it shouldn’t.

Also at the bottom speedButton() i have a switch case that allows me to edit the speed value that was originally set up at the start of the program. for some reason i have to enter the value twice before it saves it and runs it. After the first time i enter it, it prints the correct value, but the speed doesn’t match that value. I have to enter it a 2nd time before the speed matches the value. and sometimes it doesn’t give me the option to enter a value, and enters a 0 by itself, like it does during the setup. It’s like completely ignoring the command, while (Serial.available()==0) { }

below is the code to see what i’m working with. I included a Serial.print for the values once i enter it so i can see it on my monitor for future use.

I thought it could have been a communication error and have tried 3 separate USB cables from 3 different USB ports and it doesn’t make a difference and have restarted my computer. could there be a glitch in the arduino IDE program? I’m running 1.8.8 and it was installed about a month ago.

I also thought it could have been within the code itself, so i rearranged the order of the input requests. And the first saves normally, but the 2nd one still enters a 0 by itself. and also on the manual input for

any ideas? thanks

#include <Stepper.h>

                    
Stepper stepper1(6400, 4, 3);
Stepper stepper2(3200, 12, 11);

                  
   
   const int dirPin  = 3;      // stepper1 direction pin 
   const int stepPin = 4;      // stepper1 step pin
   const int enPin = 5;        // stepper1 enable pin
   const int enPin2 =13;         // stepper2 enable pin
   
   int home_Switch = 10;          // homing switch
   const byte back_Switch = 7;     // backStep switch
   const byte cutting_Switch = 8;   // cutting switch
   const byte front_Switch = 9;    // frontStep switch
   
   byte switch1State;              // cutting switch state
   byte switch2State;             // frontStep switch state
   byte switch3State;            // homeSwitch state
   byte switch4State;           // backStep state

   byte lastSwitch2State=HIGH;
   byte lastSwitch4State=HIGH;
      
   int long toothStep;    // steps to next tooth 
   int stepSpeed;
 
   int long stepCount = 0; 

   
   

void setup() {
 
  Serial.begin(115200);
  
  pinMode(enPin, OUTPUT);                // stepper1 enable output
  pinMode(enPin2, OUTPUT);               // stepper2 enable output
  pinMode( home_Switch, INPUT_PULLUP);   // home switch mode
  pinMode( cutting_Switch, INPUT_PULLUP);     // cutting switch mode
  pinMode( front_Switch, INPUT_PULLUP);     // frontStep switch mode
  pinMode( back_Switch, INPUT_PULLUP);  // backStep switch mode
  digitalWrite(enPin, HIGH);             // stepper1 enable = high
  digitalWrite(enPin2, HIGH);            // stepper2 enable = high

  
 
   while (digitalRead(home_Switch)) {     //CCW rotation
                                          // homing code
       stepper1.step(1);
       delayMicroseconds(2000);
          
    }
    
  Serial.println("You are at home");      //letting me know you are at home
  delay(1000);        // delay after homing before able to push any buttons

       Serial.println(" ");
       Serial.println(" How many steps is the tooth?");
     while (Serial.available()==0) {   }
      toothStep = Serial.parseInt();
      Serial.print(toothStep);
       
       Serial.println(" ");
       Serial.println(" How fast are you going? Fast-Slow: 1-200");
     while (Serial.available()==0) {   }
       stepSpeed = Serial.parseInt();
       Serial.print(stepSpeed);



          
}
   


void loop() {
  
  checkButtons();              // read buttons status
  cutting();                   // cutting profile
  frontStep();                 // step tooth forward
  backStep();                  // step tooth backward
  speedButton();
  
 
}

void checkButtons() {
  
  switch1State = digitalRead(cutting_Switch);   // reading stepUp button
  switch2State = digitalRead(front_Switch);  // reading frontStep button
  switch3State = digitalRead(home_Switch);  //reading home_switch button
  switch4State = digitalRead(back_Switch);  //reading backStep button
}

void cutting() {       //CW rotation step counter

      
      if (switch1State == LOW){              // if cutting_Switch activated

          
   while (digitalRead(home_Switch)){ 
         
       
         stepper1.step(1);
         stepCount++;
         Serial.println(stepCount);
         delay(stepSpeed);
   }
         Serial.print("Steps: ");
         Serial.println(stepCount);
         
         stepper1.setSpeed(50);
         stepper1.step(-stepCount);
         delay(500);
         stepCount=0;
   }
 }



void frontStep(){   // center of next tooth profile

  if (switch2State == LOW){                           // if front_Switch button activated
     
    stepper1.setSpeed(50);
    stepper1.step(toothStep);
    
        stepCount=0;

       }
        
 }




void backStep(){         //CCW rotation step counting

   if (switch4State==LOW)  {     // if back_Switch button activated

 while (digitalRead(home_Switch)){

        stepper1.step(-1);
        stepCount--;
        Serial.println(stepCount);
        delay(stepSpeed);
         

      }
    
    Serial.print("backSteps: ");
    Serial.println(stepCount);
    
        stepper1.setSpeed(50);
        stepper1.step(-stepCount);
        delay(500);
        stepCount=0;
    } 
          
} 

void speedButton(){

  if (switch3State==LOW && switch4State==LOW){

       Serial.println(" ");
       Serial.println(" How fast are you going? Fast-Slow: 1-200");
       while (Serial.available()==0) {   }
       stepSpeed = Serial.parseInt();
       Serial.print(stepSpeed);

   }
}

Try to flush the serial after having read the number of steps.

lesept:
Try to flush the serial after having read the number of steps.

How does that work?

I'd be looking at line endings

so would i Serial.flush() between the two questions? or before each of them? or after?

wildcat99s:
so would i Serial.flush() between the two questions? or before each of them? or after?

Not at all.

I would flush immediately after having read the first value. Remember it's input from the user's keyboard.

All Serial.flush does is to ensure that outgoing data from, for example, Serial.print, has been sent to the serial line.

I don't see how that helps in this case.

There is a simple User Input example in Planning and Implementing a Program

...R

I tried including the Serial.flush() in between the 2 values and unfortunately it didn't make any difference. and it still skips input request and puts a 0 in it's place.

I'll check out the Planning and Implementing a Program page again. that's where i got the basics for the program itself and see if i can get it working because i plan on installing many more push buttons for different inputs and they need to work

thanks

I suspect AWOL nailed it for you already. What are you using for line ending in the IDE's serial monitor?

wildbill:
I suspect AWOL nailed it for you already. What are you using for line ending in the IDE's serial monitor?

The line ending is what is causing your problem. The Serial.parseInt is reading the integer, but leaving the line feed in the serial buffer, so your next Serial.parseInt() sees the blank line with a line ending and returns 0.

Try adding this after each Serial.parseInt() to get rid of any extraneous characters left in the buffer:

while (Serial.available() != 0 ) {
    Serial.read();
  }

david_2018:
The line ending is what is causing your problem. The Serial.parseInt is reading the integer, but leaving the line feed in the serial buffer, so your next Serial.parseInt() sees the blank line with a line ending and returns 0.

Try adding this after each Serial.parseInt() to get rid of any extraneous characters left in the buffer:

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

Serial.read();
  }

That fixed the problem. I noticed in the manual input, after the setup runs, it seemed like it was saving the previous value and i had to double enter it before it accepted it. But it works good.

thanks

Thinking about it a little more, this code might work better if someone typed in a lot of extra characters and the complete line isn't in the buffer yet:

if (Serial.available() != 0 ) {
  while (Serial.read() != '\n') { //read characters until the end of line (line feed character)
    while (Serial.available() == 0) {}  //wait for next character to be received if not end of line
  }
}