user34614: const int ledPin = 12;

void setup(){
    pinMode(ledPin, OUTPUT);
    Serial.begin(9600);
}

void loop(){
    if (Serial.available()) {
        light(Serial.read() – '0');
    }
    delay(500);
}

void light(int n){
    for (int i = 0; i < n; i++) {
        digitalWrite(ledPin, HIGH);
        delay(100);
        digitalWrite(ledPin, LOW);
        delay(100);
    }
}

by Oscar Liang
awesome little piece of code which taught me how to setup connection by serial. yet one part I don't understand fully. Why does this line: 

light(Serial.read() – '0');

make the code run once,
and this line an infinite times.

light(Serial.read());

has this something to do with binary? I don't get why subtracting the string 0 would run the code once. 
Thanks in Advance!

user31105: Serial.read() -'0' 
Such code is often used to convert a character to a digit (e.g. int).
Like if the character is '2', than '2' - '0' = ascii 50 - ascii 48 = 2
However, if you use 
Serial.read()

the value will be 50 instead of 2.... maybe looping 50 times (or 48 to 58, for values 0 to 9) will give you the impression it runs infinite?

34614: <pre><code>const int ledPin = 12;

void setup(){
    pinMode(ledPin, OUTPUT);
    Serial.begin(9600);
}

void loop(){
    if (Serial.available()) {
        light(Serial.read() – '0');
    }
    delay(500);
}

void light(int n){
    for (int i = 0; i &lt; n; i++) {
        digitalWrite(ledPin, HIGH);
        delay(100);
        digitalWrite(ledPin, LOW);
        delay(100);
    }
}
</code></pre>

<p>by <a href="https://oscarliang.com/connect-raspberry-pi-and-arduino-usb-cable/" rel="nofollow noreferrer">Oscar Liang</a></p>

<p>awesome little piece of code which taught me how to setup connection by serial. yet one part I don't understand fully. Why does this line: </p>

<blockquote>
  <p>light(Serial.read() – '0');</p>
</blockquote>

<p>make the code run once,
and this line an infinite times.</p>

<blockquote>
  <p>light(Serial.read());</p>
</blockquote>

<p>has this something to do with binary? I don't get why subtracting the string 0 would run the code once. </p>

<p>Thanks in Advance!</p>
 31105: <p>Serial.read() -'0' </p>

<p>Such code is often used to convert a character to a digit (e.g. int).</p>

<p>Like if the character is '2', than '2' - '0' = ascii 50 - ascii 48 = 2</p>

<p>However, if you use </p>

<pre><code>Serial.read()
</code></pre>

<p>the value will be 50 instead of 2.... maybe looping 50 times (or 48 to 58, for values 0 to 9) will give you the impression it runs infinite?</p>
