If statement issues

So I am trying to figure out this program:

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.

This is a very funny problem.

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.

See the demo here

...R

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?

Or would I have to make much bigger changes?

Yes, you do. Inventing syntax worked for Kerrigin and Ritchie, but hasn't worked for many people since then.

PaulS:
Yes, you do. Inventing syntax worked for Kerrigin and Ritchie, but hasn't worked for many people since then.

and inventing surnames never worked for anyone except Douglas Adams . . .sp. "Kernighan". :wink:

Kerrigin

Isn't that some sort of soup?

RED9000:
So I am trying to figure out this program:

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);
     }
 
}

So am I, to be honest.

[quote author=Nick Gammon link=msg=1947577 date=1415083680]
Isn't that some sort of soup?
[/quote]Kerrigin? Sounds more like some Irish moonshine brew :slight_smile:

and inventing surnames never worked for anyone except Douglas Adams . . .sp. "Kernighan".

My apologies. My copy of their book is at work so I couldn't confirm the spelling (and I was in too much of a hurry to google).

RED9000:
Or would I have to make much bigger changes?

That's what I said in Reply #1 AND gave you a link to suitable code.

...R

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

Looks like the usual problem of assuming that knowing that you've got one character to read, it's safe to read all three of them.

sorry I do not quite understand what you trying to say, would you mind explaining?

if  (Serial.available() > 0) {

		A = Serial.read();
       
  }

Here you know you've got at least one character to read, so you read it.
All cool so far.

Now, knowing only that you've probably only got one character to read, and you've read it, you go ahead and read two more characters.

Do not collect $200, do not pass GO.

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

Perhaps you should explain just what you are trying to.

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="";
  } 
}

You already have another Thread about this subject. Why are you splitting the subject and wasting everyone's time?

I already answered the other Thread and gave a link to demo code in Reply #1 of the other Thread.

...R

See if this gets you any closer

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;
  }
}