Skip to main content
added 150 characters in body
Source Link

I have started learning about Arduino and am well versed in Java and Python. In an Arduino program, I need a character input via Serial to be stored in a String variable. From my previous programming experience, I knew that a code like the one below would give an error as char can't be converted into String like this.

void loop() {
  if(Serial.available()) {
     String s=(char)Serial.read();
  }
}

As expected, the above code gives an error on compiling. But when I first defined s and then assigned its value in a separate step in void loop(), I got no error.

void loop() {
  if(Serial.available()) {
     String s;
     s=(char)Serial.read();
  }
}

The above code yields no error despite the fact that it is almost similar to the first code. What I suspect is that there is some different concept of instance variables hereit has something to do with String being a class. Can one tell what is going on?

I have started learning about Arduino and am well versed in Java and Python. In an Arduino program, I need a character input via Serial to be stored in a String variable. From my previous programming experience, I knew that a code like the one below would give an error as char can't be converted into String like this.

void loop() {
  if(Serial.available()) {
     String s=(char)Serial.read();
  }
}

As expected, the above code gives an error on compiling. But when I first defined s and then assigned its value in a separate step in void loop(), I got no error.

void loop() {
  if(Serial.available()) {
     String s;
     s=(char)Serial.read();
  }
}

The above code yields no error despite the fact that it is almost similar to the first code. What I suspect is that there is some different concept of instance variables here. Can one tell what is going on?

I have started learning about Arduino and am well versed in Java and Python. In an Arduino program, I need a character input via Serial to be stored in a String variable. From my previous programming experience, I knew that a code like the one below would give an error as char can't be converted into String like this.

void loop() {
  if(Serial.available()) {
     String s=(char)Serial.read();
  }
}

As expected, the above code gives an error on compiling. But when I first defined s and then assigned its value in a separate step in void loop(), I got no error.

void loop() {
  if(Serial.available()) {
     String s;
     s=(char)Serial.read();
  }
}

The above code yields no error despite the fact that it is almost similar to the first code. What I suspect is that it has something to do with String being a class. Can one tell what is going on?

added 150 characters in body
Source Link

I have started learning about Arduino and am well versed in Java and Python. In an Arduino program, I need a character input via Serial to be stored in a String variable. From my previous programming experience, I knew that a code like the one below would give an error as char can't be converted into String like this.

void loop() {
  if(Serial.available()) {
     String s=(char)Serial.read();
  }
}

As expected, the above code gives an error on compiling. But when I first defined s as an instance variable and simplythen assigned its value in a separate step in void loop(), I got no error.

String s;
void loop() {
  if(Serial.available()) {
     String s;
     s=(char)Serial.read();
  }
}

The above code yields no error despite the fact that it is almost similar to the first code. What I suspect is that there is some different concept of instance variables here. Can one tell what is going on?

I have started learning about Arduino and am well versed in Java and Python. In an Arduino program, I need a character input via Serial to be stored in a String variable. From my previous programming experience, I knew that a code like the one below would give an error as char can't be converted into String like this.

void loop() {
  if(Serial.available()) {
     String s=(char)Serial.read();
  }
}

But when I defined s as an instance variable and simply assigned its value in void loop(), I got no error.

String s;
void loop() {
  if(Serial.available()) {
     s=(char)Serial.read();
  }
}

What I suspect is that there is some different concept of instance variables here. Can one tell what is going on?

I have started learning about Arduino and am well versed in Java and Python. In an Arduino program, I need a character input via Serial to be stored in a String variable. From my previous programming experience, I knew that a code like the one below would give an error as char can't be converted into String like this.

void loop() {
  if(Serial.available()) {
     String s=(char)Serial.read();
  }
}

As expected, the above code gives an error on compiling. But when I first defined s and then assigned its value in a separate step in void loop(), I got no error.

void loop() {
  if(Serial.available()) {
     String s;
     s=(char)Serial.read();
  }
}

The above code yields no error despite the fact that it is almost similar to the first code. What I suspect is that there is some different concept of instance variables here. Can one tell what is going on?

deleted 5 characters in body
Source Link
dda
  • 1.6k
  • 1
  • 12
  • 18

I have started learning about Arduino and isam well versed in JAVAJava and Python. In an Arduino program, I requiredneed a character input via Serial to be stored in a String variable. From my previous programming experiencesexperience, I knew that a code like the one below would give an error as char can't be converted into String like this.

void loop()
  {
  if(Serial.available())
  {
     String s=(char)Serial.read();
  }
}

But when I defined s as an instance variable and simply assigned its value in void loop(), I got no error.

String s;
void loop()
  {
  if(Serial.available())
  {
     s=(char)Serial.read();
  }
}

What I suspect is that there is some different concept of instance variables here. Can one tell what is going on?

I have started learning about Arduino and is well versed in JAVA and Python. In an Arduino program, I required a character input via Serial to be stored in a String variable. From my previous programming experiences, I knew that a code like the one below would give an error as char can't be converted into String like this.

void loop()
 {
  if(Serial.available())
  {
     String s=(char)Serial.read();
  }
}

But when I defined s as an instance variable and simply assigned its value in void loop(), I got no error.

String s;
void loop()
 {
  if(Serial.available())
  {
     s=(char)Serial.read();
  }
}

What I suspect is that there is some different concept of instance variables here. Can one tell what is going on?

I have started learning about Arduino and am well versed in Java and Python. In an Arduino program, I need a character input via Serial to be stored in a String variable. From my previous programming experience, I knew that a code like the one below would give an error as char can't be converted into String like this.

void loop() {
  if(Serial.available()) {
     String s=(char)Serial.read();
  }
}

But when I defined s as an instance variable and simply assigned its value in void loop(), I got no error.

String s;
void loop() {
  if(Serial.available()) {
     s=(char)Serial.read();
  }
}

What I suspect is that there is some different concept of instance variables here. Can one tell what is going on?

Source Link
Loading