ASCII into a number

Hello,

i have written a Code to control a Dc Motor, which you can write the number of revolutions in the serial monitor from arduino. My problem is, that the serial monitor give the number, which i can choose, in a ASCII-Number.
For example i write the number 255 (160 rpm). They change the number into a ASCII-Code, but i need the number for pwm controll. How can i change the ASCII-Code, which i write in serial monitor from arduino into a number.

Here is the Code

#define MOTOR_IN1 9

#define MOTOR_IN2 10

uint8_t drehzahl;

uint8_t pwm = 0;

void setup() {

Serial.begin(9600);

pinMode(MOTOR_IN1, OUTPUT);

pinMode(MOTOR_IN2, OUTPUT);

while (! Serial); // warten bis System bereit ist

Serial.println("Drehzahl eingeben: ");

}

void loop() {

if (Serial.available() > 0) {

pwm = Serial.read();

// int i = atoi (pwm);

// Serial.print (i);

//if (pwm >=0 && pwm <=255){

//}

}

Serial.print(pwm);

digitalWrite(MOTOR_IN1, LOW);

analogWrite (MOTOR_IN2, pwm);

delay (50);

}
:confused:

You need a way of signaling the end of a number, and collect characters from serial into a buffer until you know you have it all, and only then call atoi(). You can use whitespace or commas or both for this purpose quite naturally.

The complication is that the code receiving characters one by one has to check for whitespace/commas, and
manage a buffer of characters.

You can also avoid using a character buffer by directly converting on the fly with code such as

  value = 10*value + ch-'0';

whenever a digit comes in.

MuzoSanchez:
How can i change the ASCII-Code, which i write in serial monitor from arduino into a number.

Example:
1. In the Serial Monitor, choose the option of 'newline' in the Line ending tab.
SerialMonitor.png

2. In the InputBox of Serial Monitor enter the number 165 (for 165 rpm: an example) and click on the Send button. The Serial Monitor sends 4 ASCII frames composed of: 0x31 (ASCII code of 1), 0x36 (ASCII code of 6), 0x35 (ASCII code of 5), and 0x0A (ASCII code of newline: \n).

**3.**Catch the string of Step-2 at the receiver and re-compute the original integer number (165).

char myData[10] = "";
void loop()
{
   byte n = Serial.available();
   if(n !=0)
   {
       Serial.readBytesUntil('\n', myData, 10);   //save the string in null-terminated array
       int x = atoi(myData);
       Serial.println(x, DEC);      //show 165
       //--- do your job with the received data---
   }
}

SerialMonitor.png

Thank you so much for your answers.

i found a commando "SerialparseInt()". This change my ASCII into int.

But now the problem is, that the DC Motor rotate only for 1 sec and if i enter a number, what is higher than 255, the DC Motor rotate again. I think, 256 is 0 and 510 = 255 again etc.

#define MOTOR_IN1 9

#define MOTOR_IN2 10

uint8_t drehzahl;

uint8_t pwm = 0;

char ADstring [4];

int cntr;

char old_ch, new_ch;

void setup() {

Serial.begin(9600);

pinMode(MOTOR_IN1, OUTPUT);

pinMode(MOTOR_IN2, OUTPUT);

while (! Serial); // warten bis System bereit ist

Serial.println("Drehzahl eingeben: ");

}

void loop () {

while (Serial.available() == 0) ; // Wait here until input buffer has a character

{

pwm = Serial.parseInt ();

{

analogWrite (MOTOR_IN1, pwm);

digitalWrite (MOTOR_IN2, LOW);

}

}

Please use [ code ] tags. The forum software eats some of your code if you don’t.

Serial.parseInt() will wait up to 1 second for input. If it gets nothing then it returns zero. It is a blunt instrument and is never used in serious Arduino programs. Try Golam’s suggestion.

{Why} {is} {your} {code} {littered} {with} {useless} {curly} {braces} {?}

Okey i will try.

Thank youu

Okey now the code ist finished. Thanks for our help.

  #define MOTOR_IN1 9
#define MOTOR_IN2 10
uint8_t drehzahl;
uint16_t pwm = 0;
char ADstring [4];
int cntr;
char old_ch, new_ch;
void setup() {
  Serial.begin(9600);
  pinMode(MOTOR_IN1, OUTPUT);
  pinMode(MOTOR_IN2, OUTPUT);
  //
  Serial.println("Drehzahl eingeben: ");
}

void loop () {

  while (Serial.available() == 0) ; // Wait here until input buffer has a character
  {
    
    pwm = Serial.parseInt ();

    int drehzahl = pwm*1.59375;
    if (Serial.read() == '\n') {
      if (drehzahl <= 0 || drehzahl > 255) {
        drehzahl = 0;
        digitalWrite (MOTOR_IN2, LOW);
        digitalWrite (MOTOR_IN1, LOW);
        Serial.print ('\n');
        Serial.print ("Motor stoppt; Bitte Wert zwischen 0 und 160 eingeben");
      }
      else {
      analogWrite (MOTOR_IN1, drehzahl);
      digitalWrite (MOTOR_IN2, LOW);
      Serial.print('\n');
      Serial.print(pwm);
    }
    }
  }
}

while (Serial.available() == 0) ; // Wait here until input buffer has a character

Your comment is wrong. It should be:

//make the Arduino completely unresponsive to all inputs but Serial so it cannot do anything useful. This wastes 16 million processor cycles per second.