Skip to main content
Arduino

Return to Question

edited tags
Link
Michel Keijzers
  • 13k
  • 7
  • 41
  • 58
Code formatting + a few typos.
Source Link
Edgar Bonet
  • 45.1k
  • 4
  • 42
  • 81
const int ledPin = 12;
void setup(){
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop(){
if (Serial.available()) {
light(Serial.read() – ‘0’'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 thaughttaught me how to setup connection by serial. yet one part I dontdon't understand fully. Why does this line:

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

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

light(Serial.read());

has this something to do with binary? I dontdon't get why substractingsubtracting the string 0 would run the code once.

Thanks in Advance!

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 thaught me how to setup connection by serial. yet one part I dont 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 dont get why substracting the string 0 would run the code once.

Thanks in Advance!

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!

Source Link

Diffrence between running once and infinite

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 thaught me how to setup connection by serial. yet one part I dont 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 dont get why substracting the string 0 would run the code once.

Thanks in Advance!

lang-cpp

AltStyle によって変換されたページ (->オリジナル) /