1

first of all im new here and im really sorry if there's some mistakes when im making this question. So im trying to make a program to calculate using serial read, and im going to input a lot of values.

These are the codes that im trying to upload, but it wont shows the input.

int PpmNeed;
int Ppmpertank;
int nyala;
int tangki;
int sensor;
int mlkeluar;
int perlakuan;
void setup() {
  Serial.begin(115200);
  Serial.setTimeout(10);
}

void loop() {
    Serial.println("Besar Tangki");
    while(Serial.available() == 0){}
    tangki = Serial.parseInt();
    
    Serial.println("Kadar Dari Sensor");
    while(Serial.available() == 0){}
    sensor = Serial.parseInt();
    
    Serial.println("Data Perlakuan");
    while(Serial.available() == 0){}
    perlakuan = Serial.parseInt();
   
    Serial.print("Besar Tangki : ");
    Serial.println(tangki);
    Serial.print("Kadar Dari Sensor : ");
    Serial.println(sensor);
    Serial.print("Data Perlakuan : ");
    Serial.println(perlakuan);
}
1
  • What data are your writing for the program to parse?
    – Kingsley
    May 24 at 4:40

3 Answers 3

1

While your way to read the input from user is sub-optimal, to make it work you have to read the new line characters Serial Monitor sends (if line ending are selected in the drop down box).

The new line characters are available on Serial after the digits, so they make

while(Serial.available() == 0){}

to end immediately.

You can patch it up by reading the trailing line ending characters.

int PpmNeed;
int Ppmpertank;
int nyala;
int tangki;
int sensor;
int mlkeluar;
int perlakuan;
void setup() {
  Serial.begin(115200);
  Serial.setTimeout(10);
}

void loop() {
    Serial.println("Besar Tangki");
    while (Serial.available()) {
      Serial.read();
    }
    while(Serial.available() == 0){}
    tangki = Serial.parseInt();
    
    Serial.println("Kadar Dari Sensor");
    while (Serial.available()) {
      Serial.read();
    }
    while(Serial.available() == 0){}
    sensor = Serial.parseInt();
    
    Serial.println("Data Perlakuan");
    while (Serial.available()) {
      Serial.read();
    }
    while(Serial.available() == 0){}
    perlakuan = Serial.parseInt();
   
    Serial.print("Besar Tangki : ");
    Serial.println(tangki);
    Serial.print("Kadar Dari Sensor : ");
    Serial.println(sensor);
    Serial.print("Data Perlakuan : ");
    Serial.println(perlakuan);
}
1
  • Thankyou, it works but i change it a little bit to make it looks more neat
    – Ghoond
    Jun 8 at 0:38
3

Your problem is that humans are slow. Very slow.

Functions like Serial.parseInt() etc are designed for reading data from sensors. As such they expect data to arrive fast. They have a timeout whereby they will abort if data doesn't arrive in a timely fashion.

That's not conducive to interacting with a human that likes to take their time typing in numbers.

Instead you will need to write a routine (or find one online, such as the one I show here) which stops and waits for input to be provided before then parsing the given data and continuing.

If you're interacting with a serial terminal rather than the Arduino IDE's serial "monitor" then it can also be good to echo back the characters as they are received so the person typing can see what they have typed. Line editing (or at least basic backspace handling) would also be useful in that situation too.

4
  • but the Serial Monitor sends the line at once and there is while(Serial.available() == 0){}
    – Juraj
    May 24 at 6:54
  • @Juraj I didn't mention the fact that parseInt() doesn't consume line endings...
    – Majenko
    May 24 at 7:59
  • but it "Looks for the next valid integer in the incoming serial"
    – Juraj
    May 24 at 9:03
  • I tested it. \n causes while(Serial.available() == 0){} skip
    – Juraj
    May 24 at 15:50
0

a short function to read user Inputs. do not wort if user sends multiple numbers at once

USE ONLY FOR HUMAN INPUT.

#define NOP __asm__ __volatile__ ("nop\n\t")


//Read next number
int nextNumber() {
  int input = 0;

  //wait for next Byte
  while (Serial.available() == 0) {NOP;}
  
  /read number
  input = Serial.parseInt();
  
  //Give some time for bytes to get over Serial bus, then remove it
  delay(1);
  while (Serial.available() > 0) {
    int incomingByte = Serial.read();
  }
  return input;
}
1
  • 2
    There is no use for the NOP. May 24 at 11:24

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.