HOW TO INPUT NUMERICAL VALUE FOR PC KEYBOARD

HELLO
My problem is certainly easy to solve but I did not find myself the solution; how do you input a value on your PC keyboard between 0 and 180 to apply it as a parameter into the servo function; the purpose is to be able to find the correct instructions to send to a brushless controller to start running a motor (the controller requires some moves of the lever of the emitter before starting _ that I try to simulate)
Thank you for your help

Why not post what you have tried up to now?
(please use code tags)
You will find others more eager to help if you show your attempts beforehand.

You send the chars from the keyboard (AND NOTE:- IT IS A STRING OF CHARS NOT A NUMBER) to the Arduino via serial and then you translate the value to a number and apply it via analogWrite().

Mark

this is among some attempts what I have tried:
#include <Servo.h>
int valeur;
int b;
char a;
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created

int pos = 0; // variable to store the servo position

void setup()
{

Serial.begin(9600);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}

void loop() {
if (Serial.available() > 0) {
// read the oldest byte in the serial buffer:

valeur = Serial.read();}
a = char(valeur);
Serial.println(a);
delay(500);
b = int(a);
Serial.print(b);
// in steps of 1 degree
myservo.write(b); // tell servo to go to position in variable 'pos'
delay(5000);
myservo.write(90);
// waits 15ms for the servo to reach the position

}

// myservo.write(180); // tell servo to go to position in variable 'pos'
// delay(10000);
// myservo.write(90); // tell servo to go to position in variable 'pos'
// delay(10000);
// delay(10000); // waits 15ms for the servo to reach the position

//}

and apply it via analogWrite().

Servo.write is preferable.

Thank you. I tried to convert the chars into a value with the program as above, but what was displayed was not what I inputted

chars are NOT numbers there chars '1' does not have the value 1.

Try this

char ch ='3';
int i = 0'
i = ch-'0';
// now print them in different ways and see what you get.
// print() converts numbers to strings of chars

Servo.write is preferable. True heading for specsavers ...

Mark

This question comes up about once a week.
When you type "90", what is sent to your Arduino are characters with the values 0x39 and 0x30.
These can be converted on-the-fly by subtracting the value '0' and multiplying by their decimal weight, or by forming them into a C string and using something like "atoi".

Thank you for your help. I will try that.

but what was displayed was not what I inputted

What was "inputted" and what was displayed?

You are only reading one character, so converting that to an int is NOT done using int(). That stupid function should never have been provided. It is a crutch for people that don't understand casts.

@ludafran

As AWOL, holmes4 and PaulS explained, the PC sends ASCII characters whose values have nothing to do with the actual values you want.

You have two options:

  1. If you are using a Serial terminal then in your sketch you will need to convert the ASCII values to the values you want using the method AWOL described.

  2. Write an app on the PC that sends numeric values ( 0 - 180) to your sketch, in which case you can take those values directly and use them for your servo/s.

the PC sends ASCII characters whose values have nothing to do with the actual values you want.

Not quite true otherwise you would not be able to convert them. I think it would be better to say that the PC sends ASCII values that represent the values entered rather than the actual values themselves.

Fair enough, wrong choice of words.