Hello everyone, I'm not sure this topic is where it belongs to, but I don't know where it belongs here. My question I'm trying to use my keyboard as an input in the terminal in the IDE, as a way for me to change the value of a servo angle. I'm trying to use a simple switch case to increment/decrement the angle with a certain value when I send that particular character in the terminal. My problem is I'm really not familiar with Arduino's input using the terminal, where when I asked it to wait for the character, it doesn't wait that long and even if I can input there's some delay and difficulty in synching them. If people have already done this before, you just guide me on how you did it. Thank you.
please attach your code. You can simply implement this by checking if there is any data in the serial buffer, reading that data until a certain character(you can use the new line character for that), compare the data, and do whatever you want with the result.
Hello samuxd123890
You are looking for a command line parser.
Firstly, you need to specify the commands and then use a switch/case statement to evaluate the command.
The very basics
uint8_t servoPos;
void setup()
{
Serial.begin(115200);
Serial.println(F("Use + and - to adjust servo angle"));
}
void loop()
{
if (Serial.available())
{
char ch = Serial.read();
switch (ch)
{
case '+':
if (servoPos <= 170)
{
servoPos += 10;
Serial.println(servoPos);
}
break;
case '-':
if (servoPos >= 10)
{
servoPos -= 10;
Serial.println(servoPos);
}
break;
default:
Serial.print(F("Unsupported character 0x"));
if (ch < 0x10) Serial.print("0");
Serial.println(ch, HEX);
break;
}
}
}
The above will read one character if a character is available and depending on the character increments or decrements the servoPos or prints a message.
The IDE has a setting to determine if a <CR> (carriage return) and/or <LF> (linefeed) has to be added after the characters that you typed.
I think the default is Both NL * CR which means when you type one character and send it, you will get three characters
Use + and - to adjust servo angle
10
Unsupported character 0x0D
Unsupported character 0x0A
You can change the setting to No line ending.
If you want to send more than a single character, I suggest that you read Serial Input Basics - updated to get ideas how to approach.
That's probably because you are using higher level functions like readBytes, readStringUntil that timeout and you do not check if you got data.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.