Getting two digit numbers

Robin2:
Why not write a short program to investigate rather than confusing the OP's Thread?

It is done in Step-4 of Post#18.

(It is the beauty of a Forum that a thread digresses to different semi-relevant branches once a while and the Moderator brings it back on the rail when it goes too far.)

I am still at the side to accept that the declarations of Step-1 are equivalent.

Don't be, and don't mislead others.

If you got the same print results from both statements, it is a pure coincidence. It is just blind luck that a null was somehow present in the memory location after {'A', 'B', 'C'}. The compiler gives you what you ask for, no more.

GolamMostafa:
I am still at the side to accept that the declarations of Step-1 are equivalent.

don't be fooled by the fact that you have by luck a trailing NULL char for the first (separate chars) definition.

TheMemberFormerlyKnownAsAWOL:
Don't be, and don't mislead others.

One knows his intention very well!

J-M-L:
don't be fooled by the fact that you have by luck a trailing NULL char for the first (separate chars) definition.

Cool! :slight_smile:

aarg:
If you got the same print results from both statements, it is a pure coincidence. It is just blind luck that a null was somehow present in the memory location after {'A', 'B', 'C'}. The compiler gives you what you ask for, no more.

Also, cool! :slight_smile:

Try this code to see how many bytes got allocated

char myArray1[] = {'A', 'B', 'C'};
char myArray2[] = "ABC";

void setup() {
  Serial.begin(115200);
  Serial.println(sizeof(myArray1));
  Serial.println(sizeof(myArray2));
}

void loop() {}

You'll see 3 for the first one and 4 for the second one. the delta is the trailing NULL char that a cString imposes.

You can read also about string literals for an explanation

Notes
The null character ('\0', L'\0', char16_t(), etc) is always appended to the string literal: thus, a string literal "Hello" is a const char[6] holding the characters 'H', 'e', 'l', 'l', 'o', and '\0'.

Moreover, the execution of Serial.print(myArray[3]) for the TOP declaration of Step-1 shows 0 which is essentially a NULL byte.

A better test would have been to use the sizeof operator.

Edit: Pipped at the post!

TheMemberFormerlyKnownAsAWOL:
A better test would have been to use the sizeof operator.

Edit: Pipped at the post!

:slight_smile:

J-M-L:
Try this code to see how many bytes got allocated

char myArray1[] = {'A', 'B', 'C'};

char myArray2[] = "ABC";

void setup() {
 Serial.begin(115200);
 Serial.println(sizeof(myArray1));
 Serial.println(sizeof(myArray2));
}

void loop() {}




You'll see 3 for the first one and 4 for the second one.

I have got 3 and 4 as you have predicted (Fig-1). I am now taught based on experimental fact that the following two declarations are not equivalent.

char myArray[] = {'A', 'B', 'C'}; 
char myArray[] = "ABC";

SMlx.png
Figure-1:

(Good intention helps to design simple example and that helps the mind-set ignorance to have converted into knowledge.)

SMlx.png

Good intention helps to design simple example

For every problem, there is a solution that is simple, elegant and wrong.

GolamMostafa:
I am now taught based on experimental fact that...

the good thing is that the experimental part matches the language specification for string literal and Array declaration

amazing how stars can be aligned sometimes! :slight_smile:

TheMemberFormerlyKnownAsAWOL:
For every problem, there is a solution that is simple, elegant and wrong.

"For every problem, there is a solution that is simple, elegant and could be wrong."

GolamMostafa:
7. The parseIn() method will also terminate if it finds a non-digit character (outside 0 to 9). The return value will be 0 if the very first character so read is a non-digit character. The return value will be non-zero if a non-digit character is encountered after reading one or more valid charcaters (0 to 9).

The method is ".parseInt()", not ".parseIn()";
Characters before the first negative sign or digit are ignored. You can test it with this sketch:

void setup() {Serial.begin(115200);}
void loop()
{
  if (Serial.available())
    Serial.println(Serial.parseInt());
}

johnwasser:
The method is ".parseInt()", not ".parseIn()";
Characters before the first negative sign or digit are ignored. You can test it with this sketch:

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

void loop()
{
  if (Serial.available())
    Serial.println(Serial.parseInt());
}

parseInt() is good

TheUNOGuy:
parseInt() is good

Except that it blocks the Arduino from doing other things while it waits for the number.

...R
Serial Input Basics - simple reliable non-blocking ways to receive data.

Robin2:
Except that it blocks the Arduino from doing other things while it waits for the number.

Does "global interrupt enable bit" remain disabled during the execution of parseIn() method? If so, the MCU must not be responding to any interrupt. The following experiment shows that both parseInt() method and TOV1 interrupt are working simultaneously. In the sketch of the said experiment, the built-in L (LED) of MEGA blinks at 1-sec interval on interrupt basis (where the timeDelay has been created using TCNT1). Rather, I would say that parseInt() method remains suspended when TOV1 interrupt occurs as TOV1 interrupt has higher priority over UART interrupt.

volatile bool flag = false;

void setup()
{
  Serial.begin(9600);
  pinMode(13, OUTPUT);    //DPin direction is output
  TCCR1A = 0x0000;        //up counter mode
  TCCR1B = 0x0000;        //TC1 is STOP
  TCNT1 = 0xC2F7;         //preset value for 1 sec timeDelay
  TCCR1B = 0x05;          //Start TC1 with division factor 1024
  //---------------------------------------------------
  bitSet(TIMSK1, TOIE1);    //local part of interrupt logic is enable; 
  interrupts();//bitSet(SREG, 7);           //global part (I) of interrupt logic is enabled; 
  //---------------------------------------------------
  digitalWrite(13, HIGH);  //initially L is ON
  Serial.setTimeout(2000);   //test purpose
}

void loop()
{
  byte n = Serial.available();
  if(n != 0)
  {
    int x = Serial.parseInt();
    Serial.println(x);
  }
}

//ISRTOV1 Rooutine
ISR(TIMER1_OVF_vect)   //ISR Routine for TOV1 interrupt
{
  //bitSet(TIFR1, TOV1); this code is not needed; the TOV1 is auto cleared
  TCNT1 = 0xC2F7;  //re-load preset value for 1-sec timeDelay
  if (flag == false)    //1-sec has gone; OFF L 
  {
    digitalWrite(13, LOW);   //L is OFF from ON condition
    flag = true;
  }
  else    //toggle L from OFF to ON state
  {
    digitalWrite(13, HIGH);  //L is ON from OFF
    flag = false;
  }
}

GolamMostafa:
Does "global interrupt enable bit" remain disabled during the execution of parseIn() method?

That is a silly question and it is absolutely no help to the OP. Please pursue your own education in your own Thread.

...R

I think @Golam wanted to say that the arduino can do other things like handling interrupts, so it's not totally "blocked from doing other things". He could also have tested PWM and find that it works too..

but that's kinda obvious since receiving data on the Serial port requires interrupts anyway and of course the point was that any meaningful part of the rest of the sketch is on hold until the number is received or timeout occurs.

in some scenario it's OK to just wait, if the code has nothing else to do.