1

I am trying to read a value between 1000 and 2000 from my Serial Monitor and use it to drive some motors through ESC.

The code works when I use a potentiometer mapped for 1000-2000. I am now trying to use a Serial Monitor, but the "val" keeps going from the value I enter (between 1000 and 2000) and quickly returns to 0.

Why is this?

Here is the code for reference:

#include <Servo.h>//Using servo library to control ESC
Servo esc1; //Creating a servo class with name as esc1
Servo esc2; //Creating a servo class with name as esc2
Servo esc3; //Creating a servo class with name as esc3
Servo esc4; //Creating a servo class with name as esc4
void setup()
{
  esc1.attach(7); //Specify the esc signal pin,Here as D10
  esc2.attach(6); //Specify the esc signal pin,Here as D9
  esc3.attach(5); //Specify the esc signal pin,Here as D8
  esc4.attach(4); //Specify the esc signal pin,Here as D7
  esc1.writeMicroseconds(1000); //initialize the signal to 1000
  esc2.writeMicroseconds(1000); //initialize the signal to 1000
  esc3.writeMicroseconds(1000); //initialize the signal to 1000
  esc4.writeMicroseconds(1000); //initialize the signal to 1000
  Serial.begin(9600);
}
void loop()
{
  int val; //Creating a variable val
  Serial.println("Input ESC Speed between 1000 and 2000:");
  while (Serial.available() > 0) {
    val = Serial.parseInt();
    Serial.println(val);
  }//Checks for a character in the serial monitor
  Serial.println(val);
  esc1.writeMicroseconds(val); //using val as the signal to esc
  esc2.writeMicroseconds(val); //using val as the signal to esc
  esc3.writeMicroseconds(val); //using val as the signal to esc
  esc4.writeMicroseconds(val); //using val as the signal to esc
}
4
  • 1
    you use very annoying comment style in your code. repeating the same thing over and over only makes the code less readable.
    – jsotola
    Jan 7 '18 at 19:52
  • quickly returns to 0 ... how long does it take to return to zero?
    – jsotola
    Jan 7 '18 at 20:02
  • jsotola it takes about a second
    – Alex Breau
    Jan 7 '18 at 20:05
  • its almost as if Serial.parseInt is not going to my val integer...
    – Alex Breau
    Jan 7 '18 at 20:41
5

This code will work IF you set the Serial Monitor to "No Line Ending" (OSX 10.6.8. IDE Version 1.0.6.2). parseInt() doesn't like the NL + CR chars.

unsigned int val;

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

void loop(){
  Serial.println("Input ESC Speed between 1000 and 2000:");
  while(Serial.available() > 0){
    val = Serial.parseInt();
  }
  if(val > 0){Serial.println(val);}
  delay(1000);
}

There is another way to get the integer value using this code:

char inputBuffer[16];
unsigned int val;

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

void loop(){
  Serial.println("Input ESC Speed between 1000 and 2000:");
  while(Serial.available() > 0){

    // A function that reads characters from the serial port into a buffer.
    Serial.readBytes(inputBuffer, sizeof(inputBuffer));

    // Convert string to integer
    // cplusplus.com/reference/cstdlib/atoi/
    val = atoi(inputBuffer);

    // memset clears buffer and updates string length so strlen(inputBuffer) is accurate.
    memset(inputBuffer, 0, sizeof(inputBuffer));
  }
  if(val > 0){Serial.println(val);}
  delay(1000);
}

The Binary sketch size is 94 bytes less than the first example using parseInt. The second sketch will work with ANY Serial Monitor Line Ending settings.

4
  • Thank you very much VE7JRO, care to explain the second one? This one works, the first one not so much...even with the serial monitor set to "No Line Ending" I think i understand my mistake now, i had int val; in my main program, resetting its value each loop around!!
    – Alex Breau
    Jan 7 '18 at 22:10
  • Which O/S are you using? I'm wondering if the Serial Monitor behaves differently on different O/Ses. I'm using a very old version of OSX, 10.6.8.
    – VE7JRO
    Jan 7 '18 at 22:20
  • I've add some comments to the while loop section of the second sketch.
    – VE7JRO
    Jan 7 '18 at 22:37
  • i am using Windows 7 x64. thanks so much for the comments, very helpful!
    – Alex Breau
    Jan 8 '18 at 0:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.