admin管理员组文章数量:1320661
I am using the MIT App Inventor to create multiple light switches. Whenever I send a text from the Switch, the serial monitor prints each letter of the word on different lines. How can I change it to a whole text e.g. "1on" before showing a new line of text?
const int led1 = 2;
const int led2 = 3;
const int led3 = 4;
char v = 0;
void setup() {
Serial.begin(9600);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
}
void loop() {
if (Serial.available() > 0) {
v = Serial.read();
Serial.println(v);
// Serial.print('\n');
if (v == 'a') {
digitalWrite(led1, HIGH);
} else if (v == 'b') {
digitalWrite(led1, LOW);
}
if (v == 'c') {
digitalWrite(led2, HIGH);
} else if (v == 'd') {
digitalWrite(led2, LOW);
}
if (v == 'e') {
digitalWrite(led3, HIGH);
} else if (v == 'f') {
digitalWrite(led3, LOW);
}
}
}
I am using the MIT App Inventor to create multiple light switches. Whenever I send a text from the Switch, the serial monitor prints each letter of the word on different lines. How can I change it to a whole text e.g. "1on" before showing a new line of text?
const int led1 = 2;
const int led2 = 3;
const int led3 = 4;
char v = 0;
void setup() {
Serial.begin(9600);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
}
void loop() {
if (Serial.available() > 0) {
v = Serial.read();
Serial.println(v);
// Serial.print('\n');
if (v == 'a') {
digitalWrite(led1, HIGH);
} else if (v == 'b') {
digitalWrite(led1, LOW);
}
if (v == 'c') {
digitalWrite(led2, HIGH);
} else if (v == 'd') {
digitalWrite(led2, LOW);
}
if (v == 'e') {
digitalWrite(led3, HIGH);
} else if (v == 'f') {
digitalWrite(led3, LOW);
}
}
}
Share
Improve this question
edited Jan 18 at 17:15
Dirk
asked Jan 18 at 16:25
DirkDirk
213 bronze badges
4
- What's your question? – robertklep Commented Jan 18 at 16:30
- That is correct& Your sketch works exactly as you described. If you do not need each letter on new line replace println() with print() – Mike Petrichenko Commented Jan 18 at 16:34
- Sorry, edited the message in. Instead of one character per line, I need the whole word before breaking to a new line. – Dirk Commented Jan 18 at 16:40
- @Dirk Below is the correct answer. – Mike Petrichenko Commented Jan 18 at 19:08
1 Answer
Reset to default 0use String or char array in line 5 and use readString() function to read all in line 16. Link
const int led1 = 2;
const int led2 = 3;
const int led3 = 4;
String v = 0; /* use String or char array */
void setup() {
Serial.begin(9600);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
}
void loop() {
if (Serial.available() > 0) {
v = Serial.readString(); /* use readString() function to read all */
Serial.println(v);
// Serial.print('\n');
if (v == "1on") {
digitalWrite(led1, HIGH);
} else if (v == "1off") {
digitalWrite(led1, LOW);
}
else if (v == "2on") {
digitalWrite(led2, HIGH);
} else if (v == "2off") {
digitalWrite(led2, LOW);
}
else if (v == "3on") {
digitalWrite(led3, HIGH);
} else if (v == "3on") {
digitalWrite(led3, LOW);
}
}
}
本文标签: arduinoHC05 Serial PrintStack Overflow
版权声明:本文标题:arduino - HC-05 Serial Print - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742064849a2418773.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论