Control several motors by entering a number of two digits by keyboard

Hello, I need help, I want to control several motors by entering a number of two digits by keyboard, the problem is that I cannot enter the number '11' for example, on the other hand if I enter the numbers between '1' and '9' there is no problem

if(key'1') {Motor-On} :white_check_mark:
if(key'13') {Motor-On}:x: (i want to pilote my motor with enter 13)

Bonjour, j'ai besoin d'aide, je veux piloter plusieurs moteurs en entrant un nombre de deux chiffres par clavier, le probleme est que je ne peux pas entre le nombre '11' par exemple, par contre si je fais entrer les nombres entre '1' et '9' il n' y a aucun probleme

Hello,
can you please post your sketch?

a common approach to handling simple commands with values if you don't have some line terminator is to precede the cmd with the values. the cmd could be a single letter.

so a val is created from multiple digits by multiplying the value by 10 and adding the digit entered. the cmd then processes the accumulated value

Ah, classic serial read issue. Might I recommend starting off with not just reading the serial directly, but saving it a string for manipulation later?

Instead of just "Serial.read()", instead, something like the following for 2 digits:

String result = "";
if(Serial.available) {
  result = String(Serial.read()) + String(Serial.read());
}

or if you need it of arbitrary length ending in a newline character (Like the serial monitor does by default):

String result = "";
char c = Serial.read();
while(c != '\n') {
  result += String(c);
  c = Serial.read();
}

Not very efficient code mind you, and using Strings is looked down upon, but if you are starting out, that can help you a bit. Serial communications can be a tad tricky, but keep at it, and you'll figure it out.

Best option is to use a char array if you know the input will only ever be a set length (In your case, 2 digits)

if you need to read something serially up to a terminator, consider readBytesUntil()

if you process a series of single letter cmds, you can have a single string with multiple #cmds

I found that function somewhere but don't know the original link. That works perfectly for me and is also stable if the single chars are transmitted slowly, because it always checks for the terminators:

void recvWithStartEndMarkers() {
	static boolean recvInProgress = false;
	static byte ndx = 0;
	char startMarker = '<';
	char endMarker = '>';
	char rc;

	while (Serial.available() > 0 && SerialNewData == false) {
		rc = Serial.read();

		if (recvInProgress == true) {
			if (rc != endMarker) {
				receivedChars[ndx] = rc;
				ndx++;
				if (ndx >= payloadChars) {
					ndx = payloadChars - 1;
				}
			}
			else {
				receivedChars[ndx] = '\0'; // terminate the string
				recvInProgress = false;
				ndx = 0;
				SerialNewData = true;
			}
		}

		else if (rc == startMarker) {
			recvInProgress = true;
		}
	}
}

Greetings
Raphael

you may be interested in looking at the source

size_t Stream::readBytesUntil(char terminator, char *buffer, size_t length)
{
  size_t index = 0;
  while (index < length) {
    int c = timedRead();
    if (c < 0 || c == terminator) break;
    *buffer++ = (char)c;
    index++;
  }
  return index; // return number of characters, not including null terminator
}

//CLAVIER
#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns

char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {2, 3, 4, 5};
byte colPins[COLS] = {6, 7, 8};

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

//MOTEUR
const int M1=30;
const int M2=31;

//AUTRES
int ordre;

void setup() {
pinMode(M1,OUTPUT);
pinMode(M2,OUTPUT);

}

void loop() {
char key = keypad.getKey();

if(key=='2'){

ordre=1;}

//ROTATION DU MOTEUR+DETECTION IR+ARRET DU MOTEUR+SORTIE DE LA BOUCLE
switch(ordre){
case 1:
ON(5000);
delay(2000);
break;
}
}
void ON(int Speed)
{
analogWrite(M1,Speed);
analogWrite(M2,0);
}

what about something like following

int val = 0;

void loop() {
    char c = keypad.getKey();

    switch (c)  {
    case '0':
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
    case '8':
    case '9':
        val = 10 * val + (c - '0');
        break;

    case '*':
        ON(0);
        break;

    case '#':
        ON(5000);
        break;
    }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.