I've seen the Send button on the right side of the Serial Monitor so I figured I would send some data.
I made up this sketch for a DC motor:
int motorpin = 8;
bool myFlag;
void setup(){
pinMode(motorpin, OUTPUT);
Serial.begin(9600);
}
void loop() {
if (Serial.available()>0) {
myFlag = Serial.read();
Serial.println(myFlag);
if (myFlag) {
Serial.println("myFlag is true");
//digitalWrite(motorpin, HIGH);
} else {
Serial.println("myFlag is false");
//digitalWrite(motorpin, LOW);
}
}
}
According to me, every loop it:
Reads data from the Serial Monitor
Sets it to myFlag variable
Prints the flag
If myFlag is true, print true, else print false
But when I upload it and enter 'true', it prints out 1 and myFlag is true. But no matter how many times i enter 'false' in the monitor, it keeps printing 1 and myFlag is true. And its interesting that it prints it 5 times after each enter.
No it doesn't. It does the same thing for "true" and "false". It will always print those lines once for each character that you type. So if you type "foo", it will do it three times. So it will do it four times for true, and five times for false. It's because Serial.read() only reads one character at a time. It's not able to read strings like "true". Even if it did, those strings would not be seen as true/false values in your program.
Please edit your post so the ending code tag doesn't include your ending comments. Or nobody will see your question.
Because the serial monitor sends text: "t", "r', "u", "e", CR, LF
The CR and LF are dependent on the settings at the bottom of the serial monitor.
All of those characters are non-zero, which means they are all true. You can't actually send character zero using the serial monitor, although it is a valid character, sometimes called "null" or '\n'.
Marciokoko:
Ok so I would have to work with 1 and 0, correct?
If you want to stick with single characters, they can be anything you like, but you have to test for them as characters. For example, you could use 0 and 1, and test like this:
if (inChar == '0') ...
but you could use any character and test for it in a similar way, for example t and f:
if (inChar == 't') ...
I avoided using myChar, because the programmer knows whose char it is, and it's difficult to steal.
No, you don't have to. You could also make it work with the strings "true" or "false".
int motorpin = 8;
int index;
char buffer[6];
void setup(){
pinMode(motorpin, OUTPUT);
Serial.begin(9600);
}
void loop() {
if (Serial.available()>0) {
index = Serial.readBytesUntil('\n', buffer, 5); // End on a newline character or 5 chars max
buffer[index] = '\0'; // Now it's a string
Serial.println(buffer);
if (strcmp(buffer, "true") == 0) {
Serial.println("myFlag is true");
//digitalWrite(motorpin, HIGH);
} else {
Serial.println("myFlag is false");
//digitalWrite(motorpin, LOW);
}
}
}
The only significant change is the call to readBytesUntil(), which continues to read bytes until either a newline character ('\n') is read or a max of 5 characters are read. Because index tells how many characters were received, we write the string termination character, null ('\0'), into buffer[] so we can now treat it as a string. The call to strcmp() sees whether it is true or not.
Simple servo code that captures the command value sent from the serial monitor, converts the value into a number for use as a servo position command.
// zoomkat 7-30-10 serial servo test
// type servo position 0 to 180 in serial monitor
// for writeMicroseconds, use a value like 1500
// Powering a servo from the arduino usually *DOES NOT WORK*.
String readString;
#include <Servo.h>
Servo myservo; // create servo object to control a servo
void setup() {
Serial.begin(9600);
myservo.attach(9);
}
void loop() {
while (Serial.available()) {
if (Serial.available() >0) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the string readString
delay(3);
}
}
if (readString.length() >0) {
Serial.println(readString);
int n = readString.toInt();
Serial.println(n);
myservo.writeMicroseconds(n);
//myservo.write(n);
readString="";
}
}