int A = 0;
char HELLO[6] = {'h','e','l','l','o'};
char REALLY[7] = {'r','e','a','l','l','y'};
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
A = Serial.read();
}
if( A == HELLO[1]){
Serial.read();
Serial.println("USER: K");
delay(2000);
}
if( A == REALLY[1]){
Serial.read();
Serial.println("P");
delay(2000);
}
}
Originally when I would run the program before I added the REALLY string, I would input, hello, get back USER: K. When I added the REALLY string it runs all the if statements at once.
so If I input hello I will now get:
USER: K
P
If anyone knows how to fix this It would be much appreciated. I heard switch/case statements do a much better job of managing this but cannot store a string.
The way your code is written it only reads a single character - that's what A = Serial.read(); does
But your test is if ( A == HELLO[1]){ which is checking whether the single character matches the second character in the word Hello. And similarly, the second character in the word Really - which is also an 'e'
The test should be if (A == HELLO[0]) { - and the same for REALLY
And, of course, if you actually want to receive the entire word your code needs a lot of changes.
There are several issues you might want to think about. First, just type in the letter 'e' and nothing else and click the Send button. What happens? More importantly, is it what you want to happen? Try the same thing with the letter 'h' and explain what happens.
Does the first element of a C array begin with 1 or 0?
Finally, what happens if you write the loop block as:
void loop() {
if (Serial.available() > 0) {
A = Serial.read();
if (A == HELLO[1]) {
Serial.read();
Serial.println("USER: K");
delay(2000);
}
if ( A == REALLY[1]) {
Serial.read();
Serial.println("P");
delay(2000);
}
}
}
Does it perform the same as you wrote it and, if not, why not?
Thanks for the help, So I was wondering if I were put REALLY[1-7] or REALLY[0,1,2,3] could I check certain parts of the word or whole words that way? Or would I have to make much bigger changes?
Okay so I am getting closer to coding for multiple bits of Serial input, but I don't really understand an issue I ran into.
int A = 0;
int B = 0;
int C = 0;
char REALLY[7] = {'r','e','a','l','l','y'};
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
A = Serial.read();
}
if( A == REALLY[0] ){
B = Serial.read();
Serial.read();
if( B == REALLY[1]){
Serial.println("TEST LINE");
}
}
}
If upload this code, open the serial monitor and enter re I get "TEST LINE" back. However when I added in a third part I couldn't get any output.
int A = 0;
int B = 0;
int C = 0;
char REALLY[7] = {'r','e','a','l','l','y'};
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
A = Serial.read();
}
if( A == REALLY[0] ){
B = Serial.read();
Serial.read();
if( B == REALLY[1]){
C = Serial.read();
Serial.read();
if( C == REALLY[2]){
Serial.println("TEST LINE");
}
}
}
}
When I upload this I don't get any errors, however when I type in "rea" in the serial monitor and send it I get no line of output from the board. So does anyone know how I would fix this or why its not working? granted stuffing if statements into each other is a bad idea to begin with
So what I aim to do is code for larger inputs through the serial monitor, for example sending T will yield no results or different results then when TS is entered.
A very basic example of capturing a string of characters sent to the arduino, then evaluating the captured characters for a desired result.
// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later
//A very simple example of sending a string of characters
//from the serial monitor, capturing the individual
//characters into a String, then evaluating the contents
//of the String to possibly perform an action (on/off board LED).
int ledPin = 13;
String readString;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
Serial.println("serial on/off test 0021"); // so I can keep track
}
void loop() {
while (Serial.available()) {
delay(3);
char c = Serial.read();
readString += c;
}
if (readString.length() >0) {
Serial.println(readString);
if(readString.indexOf("on") >=0)
{
digitalWrite(ledPin, HIGH);
}
if(readString.indexOf("off") >=0)
{
digitalWrite(ledPin, LOW);
}
readString="";
}
}
int A = 0;
char REALLY[7] = {'r','e','a','l','l','y'};
void setup() {
Serial.begin(9600);
}
int currentPosition=0;
void loop() {
if (Serial.available() > 0)
{
A = Serial.read();
if(A==REALLY[currentPosition])
currentPosition++;
else
currentPosition=0;
}
if (currentPosition>5)
{
Serial.print("TADA! you get the prize");
currentPosition=0;
}
}