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.)
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.
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.
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'.
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());
}
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;
}
}
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.