1

I want to write below code but I don't know where I doing wrong, so please help me solve this issue.

Serially read some text like number then I want to serially parse integer value into some variable and then my next task is if I wrote as text then serially read string and should save in some other variable...

char c;
String s;
long num1=0;
long num2=0;
void setup() 
{
  Serial.begin(9600);
  Serial.println("Let's start: ");
}
void loop() 
{
  if(Serial.available()>0)
  {
    delay(5);
    c = Serial.read();
    s += c;
  }
  if(s.length()>0)
  {
    if(s=="n")
    {
        if(num1==0)
        {
        Serial.println("write number: "); //serially print
        num1=Serial.parseInt(); // not serially print
        if(num1>0)
        Serial.print(num1);
        }
        if(num1>0)
        {
        Serial.println("write second number: ");
        num2=Serial.parseInt();
        if(num2>0)
        Serial.print(num2);
        }
    }
    s="";
  }
}
  • And what's your problem? The sketch works fine : You send n12 33 and the sketch detects two numbers 12 and 33. – DataFiddler Oct 12 at 11:08
  • Once num1 != 0 it never goes back to 0 so you'll see "write number" only once after reset – DataFiddler Oct 12 at 11:23
  • There is serially print as "Let's Start".. done. After gives input "n", Print as "write number". But after not read any integer value(parseInt). – Mehul Oct 12 at 11:54
  • There's no dialog. After entering only an "n" your logic is broken: The "n" is removed from s and your code works as programmed. – DataFiddler Oct 12 at 12:41

Your Answer

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

Browse other questions tagged or ask your own question.