Buffer error

Hi, I have a quick question for you guys… so below is the code for my Arduino Uno. It is supposed to get the time and speed from the user, and then run at the certain speed for a certain amount of time.

All that is happening in output screen is this:

WheelSet
���͕�enter the time:Please enter the .�

So I think it has something to do with the buffer but I have no idea how to fix it. Any ideas?

#include <Servo.h>
using namespace std;

Servo servoLeft;
Servo servoRight;

String readString;
void sendInfo(int time, int speed);
void Setup();
void loop();

void setup()
{

}
void loop()
{
Serial.begin(9600);
servoLeft.writeMicroseconds(1500);
servoRight.writeMicroseconds(1500);
servoLeft.attach(13);
servoRight.attach(12);
Serial.println(“WheelSet”);
int time;
int speed;

//Gets the time
Serial.print(“Please enter the time:”);
while (Serial.available())
{
char k = Serial.read(); //gets one byte from serial buffer
readString += k; //makes the string readString
delay(2);
}
if (readString.length() > 0)
{
Serial.println(readString); //so you can see the captured string
time = readString.toInt(); //convert readString into a number
time = time * 1000;
Serial.print("Time: ");
Serial.println(time);
Serial.println(“ms”);
}

//Gets the speed
speed=0;
Serial.println(“Please enter the speed:”);
while (Serial.available())
{
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the string readString
delay(2); //slow looping to allow buffer to fill with next character
}

if (readString.length() > 0)
{
//Serial.println(readString); //so you can see the captured string
speed = readString.toInt(); //convert readString into a number
}

//Sends the info
// auto select appropriate value, copied from someone elses code.
if(speed>=200)
{
Serial.print("writing Microseconds: ");
Serial.println(speed);
servoLeft.writeMicroseconds(speed);
servoRight.writeMicroseconds(speed);
}

delay(time);
servoLeft.detach();
servoRight.detach();
}

Why is Serial.begin in loop()?

Where are the code tags?

I'm brand new to using arudino....I'm not familiar with how it works. What do you see that is blatantly wrong?

Normally, you'd put that stuff in setup(), like it shows you in all the example code that comes with the IDE.

#include <Servo.h>
using namespace std;

Servo servoLeft;
Servo servoRight;

String readString;
void sendInfo(int time, int speed);
void Setup();
void loop();

void setup()
{
Serial.begin(9600);
servoLeft.writeMicroseconds(1500);
servoRight.writeMicroseconds(1500);
servoLeft.attach(13);
servoRight.attach(12);
}
void loop()
{
Serial.println(“WheelSet”);
int time;
int speed;

//Gets the time
Serial.print(“Please enter the time:”);
while (Serial.available())
{
char k = Serial.read(); //gets one byte from serial buffer
readString += k; //makes the string readString
delay(2);
}
if (readString.length() > 0)
{
Serial.println(readString); //so you can see the captured string
time = readString.toInt(); //convert readString into a number
time = time * 1000;
Serial.print("Time: ");
Serial.println(time);
Serial.println(“ms”);
}

//Gets the speed
speed=0;
Serial.println(“Please enter the speed:”);
while (Serial.available())
{
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the string readString
delay(2); //slow looping to allow buffer to fill with next character
}

if (readString.length() > 0)
{
//Serial.println(readString); //so you can see the captured string
speed = readString.toInt(); //convert readString into a number
}

//Sends the info
// auto select appropriate value, copied from someone elses code.
if(speed>=200)
{
Serial.print("writing Microseconds: ");
Serial.println(speed);
servoLeft.writeMicroseconds(speed);
servoRight.writeMicroseconds(speed);
}

delay(time);
servoLeft.detach();
servoRight.detach();
}

Now it’s doing this:

WheelSet
Please enter the time:Please enter the speed:

and loops through that repeating those lines over and over until I time in a number then it does this(all I typed into it was the number 10 followed by the number 15 and it did this:

Please enter the time:10
Time: 10000
ms
Please enter the speed:
WheelSet
Please enter the time:10
Time: 10000
ms
Please enter the speed:
WheelSet
Please enter the time:1015
Time: 31960
ms
Please enter the speed:
writing Microseconds: 1015

Please enter the time:1015
Time: 31960
ms
Please enter the speed:
writing Microseconds: 1015
WheelSet
Please enter the time:1015
Time: 31960
ms
Please enter the speed:
writing Microseconds: 1015
WheelSet
Please enter the time:1015
Time: 31960
ms
Please enter the speed:
writing Microseconds: 1015

and is still going…any ideas?

Serial.print("Please enter the time:");
 while (Serial.available())

Tell us about that code - what do you think it does?

Please also tell us why you choose not to use code tags.

Please delete

void Setup();
void loop();

not sure why you put those in.

You might want to use Serial.readString(), which is included in Arduino Stream.readString() - Arduino Reference

Here's a basic example of what this does:

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

String s;

void loop() {
  Serial.println("Wating for a string...");
  s = Serial.readString();
  if (s.length() > 0) {
    Serial.println("here's what I got:");
    Serial.println(s);
  }
}